You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use the library to proxy into another service.
not working when upgraded to node 17.
Step-by-step reproduction instructions
1. using node 14.17 - works.
2. replace node 17.x - doesn't work.console error:[HPM] Error occurred while proxying request localhost:3000/aaa/ to http://localhost:3001/ [ECONNREFUSED]
### Expected behavior (be clear and concise)
proxy should work and return the data from another proxied service
### How is http-proxy-middleware used in your project?
```shell
└── http-proxy-middleware@2.0.2
What http-proxy-middleware configuration are you using?
{target,changeOrigin: true,headers: {
tenant,},}
What OS/version and node/version are you seeing the problem?
I think the reason the linked example works is because it doesn’t use localhost.
Note:
require("http").get("http://localhost:3000/", res => console.log(res.statusCode)) works on Node.js 16 but not 17 (ECONNREFUSED).
curl -i http://localhost:3000 works.
EDIT: It seems like if I run the official Node.js HTTP hello world example on port 3000, require("http").get("http://localhost:3000/", res => console.log(res.statusCode)) works just fine. However, when I run a Suave server I run into ECONNREFUSED. So there’s something about what the server is doing as well. I have no reproduction going here.
EDIT2: Figured it out. I was running the Suave server explicitly on 0.0.0.0, which is ipv4. Node.js uses :: by default.
You shouldn't be using localhost but 127.0.0.1 instead. Reason is that name resolution for localhost varies a lot between different systems and implementations of their network stacks.
Reacted by Kaia Rae Peacock, larksoftware, Parker McMullin, coyi_zdy, Sanel Hadžini, wc7086 and Thomas Junghans
You shouldn't be using localhost but 127.0.0.1 instead. Reason is that name resolution for localhost varies a lot between different systems and implementations of their network stacks.
Thank you, this was the issue for me on Node 17 and 18, while the pattern was working on previous node versions
@treysis
that's weird, isn't it? every domain is something that is made up to locate a target IP somewhere for someone.
doesn't matter if it's localhost or google.com. it's either configured in my hosts file, at the ISP, or ISOC.
@krpeacock
Yes, that's why it was mentioned as a possibly breaking change and not introduced in 16, but in the short-term release 17, and also not backported to 16.
@davidmeirlevy
Kind of. I haven't figured out what's the differenc when opening a listening socket and when opening a socket for connecting. However, even if querying localhost returns both IPv4 and IPv6, the host OS/libraries still have a say in ordering the results. E.g. if a host or even just a single interface doesn't have IPv6 aside from the default link-local address, the system might decide that returning ::1 is not feasible and will still give precedence to 127.0.0.1.
For dns lookups, Node.js no longer prefers IPv4 over IPV6. Instead, it will now respect the order that is returned based on the dns entries. For properly configured hosts this should not make a difference but if you have a partially or incorrectly configured IPv6 stack on your hosts you might start seeing problems that were hidden before. The command line option --dns-result-order=ipv4first can be used to revert to the old behavior if necessary.
Looks like Node v17+ changed to way localhost is being looked-up
Yes, partly. It just doesn't reorder the DNS results anymore. That's what we said all the time. The lookup works the same as before. Don't use the DNS flag as it will prevent IPv6-connectivity. Just instead of localhost use 127.0.0.1.
@chimurai the way we're using http-proxy-middleware is through a node utility, so we can't dictate what domain/ip-address they use specifically. In the meantime we have this workaround/hack in place for users on Node 17+:
/** * Workaround for http-proxy-middleware DNS lookup issue with Nodejs 17+ * [See here for details]{@link https://github.com/chimurai/http-proxy-middleware/issues/705#issuecomment-1126925635} **/target: target.replace('http://localhost','http://127.0.0.1'),
Any plans to fix this internally in the http-proxy-middleware library?
@raspy8766 that's a neat solution
unfortunately for me, it won't work since I have a case with multi-tenancy, and there's a difference in dev between loading localhost to 127.0.0.1 to 0.0.0.0 (the URLs in my system apply the relevant tenant to the system and act with different results).
but probably for most cases - your solution is excellent.
I think I will close this bug since it's actually a bug mostly in implementing dev environments with node, and not specifically for this middleware.
for me, node 17 doesn't work because somehow the example code simply doesn't create the proxy from the right base url, it keeps using / to proxy for some reason. Using express, node 17, and used the example code provided.
The result is simply proxy didn't work. I guess I have to use another library, was expecting this work out of box but somehow it just doesn't work no matter what I do.
You shouldn't be using localhost but 127.0.0.1 instead. Reason is that name resolution for localhost varies a lot between different systems and implementations of their network stacks.
Activity
davidmeirlevy commentedon Feb 2, 2022
BTW, working great on node 16.x
chimurai commentedon Feb 7, 2022
This example works in node v17.4.0.
https://github.com/chimurai/http-proxy-middleware/blob/master/examples/express/index.js
Just clone the repo and run
node examples/expressfrom the project root folder.Can you provide a minimal reproducible project with the error? Without one there is little to investigate.
lydell commentedon Apr 14, 2022
This could be caused by: nodejs/node#40702. I don’t understand the DNS stuff though, so I’m not sure about anything.
This seems to reproduce the problem:
I think the reason the linked example works is because it doesn’t use
localhost.Note:
require("http").get("http://localhost:3000/", res => console.log(res.statusCode))works on Node.js 16 but not 17 (ECONNREFUSED).curl -i http://localhost:3000works.EDIT: It seems like if I run the official Node.js HTTP hello world example on port 3000,
require("http").get("http://localhost:3000/", res => console.log(res.statusCode))works just fine. However, when I run a Suave server I run into ECONNREFUSED. So there’s something about what the server is doing as well. I have no reproduction going here.EDIT2: Figured it out. I was running the Suave server explicitly on
0.0.0.0, which is ipv4. Node.js uses::by default.https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback
When setting
hostto0.0.0.0I have a reproduction:Sorry, about the confusion – this is not my area of expertise.
treysis commentedon Apr 14, 2022
You shouldn't be using
localhostbut127.0.0.1instead. Reason is that name resolution for localhost varies a lot between different systems and implementations of their network stacks.krpeacock commentedon Apr 19, 2022
Thank you, this was the issue for me on Node 17 and 18, while the pattern was working on previous node versions
davidmeirlevy commentedon Apr 20, 2022
@treysis
that's weird, isn't it? every domain is something that is made up to locate a target IP somewhere for someone.
doesn't matter if it's localhost or google.com. it's either configured in my hosts file, at the ISP, or ISOC.
treysis commentedon Apr 20, 2022
@krpeacock
Yes, that's why it was mentioned as a possibly breaking change and not introduced in 16, but in the short-term release 17, and also not backported to 16.
@davidmeirlevy
Kind of. I haven't figured out what's the differenc when opening a listening socket and when opening a socket for connecting. However, even if querying localhost returns both IPv4 and IPv6, the host OS/libraries still have a say in ordering the results. E.g. if a host or even just a single interface doesn't have IPv6 aside from the default link-local address, the system might decide that returning
::1is not feasible and will still give precedence to127.0.0.1.chimurai commentedon Apr 21, 2022
Support for node17 will stop in june 2022... Not sure if it worth the effort to investigate the issue for node 17.
Node 18 just got released: https://nodejs.org/en/blog/announcements/v18-release-announce/
Do you have the same issue when you're using node 18?
lydell commentedon May 14, 2022
@chimurai I tested with Node.js 18.1.0 now, and yes, I have the same issue with Node.js 18.
chimurai commentedon May 15, 2022
Looks like Node v17+ changed to way
localhostis being looked-up:dns.lookupoptions nodejs/node#39793Can you try replacing
localhostwith127.0.0.1?There is a
--dns-result-orderflag which you can use to force old behaviour:https://www.redhat.com/en/blog/welcome-nodejs-18
treysis commentedon May 15, 2022
Yes, partly. It just doesn't reorder the DNS results anymore. That's what we said all the time. The lookup works the same as before. Don't use the DNS flag as it will prevent IPv6-connectivity. Just instead of localhost use 127.0.0.1.
raspy8766 commentedon May 25, 2022
@chimurai the way we're using
http-proxy-middlewareis through a node utility, so we can't dictate what domain/ip-address they use specifically. In the meantime we have this workaround/hack in place for users on Node 17+:Any plans to fix this internally in the
http-proxy-middlewarelibrary?davidmeirlevy commentedon May 26, 2022
@raspy8766 that's a neat solution
unfortunately for me, it won't work since I have a case with multi-tenancy, and there's a difference in dev between loading
localhostto127.0.0.1to0.0.0.0(the URLs in my system apply the relevant tenant to the system and act with different results).but probably for most cases - your solution is excellent.
I think I will close this bug since it's actually a bug mostly in implementing dev environments with node, and not specifically for this middleware.
6 remaining items
chimurai commentedon May 26, 2022
Definitely makes sense to update the documentation with a note to new the Node behaviour in node 17+. 👍
davidmeirlevy commentedon May 26, 2022
@chimurai I created a PR for changing the docs accordingly:
#783
pencilcheck commentedon Sep 14, 2022
for me, node 17 doesn't work because somehow the example code simply doesn't create the proxy from the right base url, it keeps using / to proxy for some reason. Using express, node 17, and used the example code provided.
The result is simply proxy didn't work. I guess I have to use another library, was expecting this work out of box but somehow it just doesn't work no matter what I do.
theteladras commentedon Dec 15, 2022
This kind of bugs, have headache symptoms. 😄
Chukwu3meka commentedon Feb 14, 2023
You can't combine IP with subdomain
treysis commentedon Feb 14, 2023
@Chukwu3meka But
localhostisn't a subdomain or what are you referring to?Chukwu3meka commentedon Feb 14, 2023
Something like dev.127.0.0.1:3000 would not work, unlike dev.localhost:3000
treysis commentedon Feb 14, 2023
@Chukwu3meka I understand. Is there any reason for using subdomains of localhost?
chimurai commentedon Feb 14, 2023
Please use the Discussions for project configuration issues. Thanks.
[-][Bug] not working at node 17[/-][+][Bug] not working at node 17 (ECONNREFUSED)[/+]