Skip to content

Commit 8e62907

Browse files
davidmeirlevychimurailydell
authored
docs(node17+): dns lookups localhost (chimurai#783)
* update the docs according IPv6 changes on node17+ * rephrase ipv6 issue description * Update README.md * changes after review * docs: update toc and minor improvements * Update README.md Co-authored-by: Simon Lydell <[email protected]> * docs: update toc * lint: fix prettier Co-authored-by: chimurai <[email protected]> Co-authored-by: Simon Lydell <[email protected]>
1 parent 03239c7 commit 8e62907

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ app.use(
4545
app.listen(3000);
4646

4747
// proxy and change the base path from "/api" to "/secret"
48-
// http://localhost:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
48+
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
4949
```
5050

5151
```typescript
@@ -67,7 +67,7 @@ app.use(
6767
app.listen(3000);
6868

6969
// proxy and keep the same base path "/api"
70-
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
70+
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar
7171
```
7272

7373
_All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).
@@ -81,7 +81,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
8181
- [Express Server Example](#express-server-example)
8282
- [app.use(path, proxy)](#appusepath-proxy)
8383
- [Options](#options)
84-
- [`pathFilter` (string, []string, glob, []glob, function)](#pathfilter-string-string-glob-glob-function)
84+
- [`pathFilter` (string, \[\]string, glob, \[\]glob, function)](#pathfilter-string-string-glob-glob-function)
8585
- [`pathRewrite` (object/function)](#pathrewrite-objectfunction)
8686
- [`router` (object/function)](#router-objectfunction)
8787
- [`plugins` (Array)](#plugins-array)
@@ -93,6 +93,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
9393
- [External WebSocket upgrade](#external-websocket-upgrade)
9494
- [Intercept and manipulate requests](#intercept-and-manipulate-requests)
9595
- [Intercept and manipulate responses](#intercept-and-manipulate-responses)
96+
- [Node.js 17+: ECONNREFUSED issue with IPv6 and localhost (#705)](#nodejs-17-econnrefused-issue-with-ipv6-and-localhost-705)
9697
- [Debugging](#debugging)
9798
- [Working examples](#working-examples)
9899
- [Recipes](#recipes)
@@ -257,22 +258,22 @@ Re-target `option.target` for specific requests.
257258
// Use `host` and/or `path` to match requests. First match will be used.
258259
// The order of the configuration matters.
259260
router: {
260-
'integration.localhost:3000' : 'http://localhost:8001', // host only
261-
'staging.localhost:3000' : 'http://localhost:8002', // host only
262-
'localhost:3000/api' : 'http://localhost:8003', // host + path
263-
'/rest' : 'http://localhost:8004' // path only
261+
'integration.localhost:3000' : 'http://127.0.0.1:8001', // host only
262+
'staging.localhost:3000' : 'http://127.0.0.1:8002', // host only
263+
'localhost:3000/api' : 'http://127.0.0.1:8003', // host + path
264+
'/rest' : 'http://127.0.0.1:8004' // path only
264265
}
265266

266267
// Custom router function (string target)
267268
router: function(req) {
268-
return 'http://localhost:8004';
269+
return 'http://127.0.0.1:8004';
269270
}
270271

271272
// Custom router function (target object)
272273
router: function(req) {
273274
return {
274275
protocol: 'https:', // The : is required
275-
host: 'localhost',
276+
host: '127.0.0.1',
276277
port: 8004
277278
};
278279
}
@@ -488,7 +489,7 @@ The following options are provided by the underlying [http-proxy](https://github
488489
req,
489490
res,
490491
{
491-
target: 'http://localhost:4003/',
492+
target: 'http://127.0.0.1:4003/',
492493
buffer: streamify(req.rawBody),
493494
},
494495
next
@@ -573,6 +574,21 @@ const proxy = createProxyMiddleware({
573574

574575
Check out [interception recipes](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/response-interceptor.md#readme) for more examples.
575576

577+
## Node.js 17+: ECONNREFUSED issue with IPv6 and localhost ([#705](https://github.com/chimurai/http-proxy-middleware/issues/705))
578+
579+
Node.js 17+ no longer prefers IPv4 over IPv6 for DNS lookups.
580+
E.g. It's **not** guaranteed that `localhost` will be resolved to `127.0.0.1` – it might just as well be `::1` (or some other IP address).
581+
582+
If your target server only accepts IPv4 connections, trying to proxy to `localhost` will fail if resolved to `::1` (IPv6).
583+
584+
Ways to solve it:
585+
586+
- Change `target: "http://localhost"` to `target: "http://127.0.0.1"` (IPv4).
587+
- Change the target server to (also) accept IPv6 connections.
588+
- Add this flag when running `node`: `node index.js --dns-result-order=ipv4first`. (Not recommended.)
589+
590+
> Note: There’s a thing called [Happy Eyeballs](https://en.wikipedia.org/wiki/Happy_Eyeballs) which means connecting to both IPv4 and IPv6 in parallel, which Node.js doesn’t have, but explains why for example `curl` can connect.
591+
576592
## Debugging
577593

578594
Configure the `DEBUG` environment variable enable debug logging.

0 commit comments

Comments
 (0)