-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can't handle www.host #150
Comments
Correction -- it doesn't hang, but for localhost:3000/stores it returns: Invalid URL The requested URL "/stores", is invalid. |
hmm, it looks like you may need to edit the response so that it does not redirect. can your provide more information? I'm not really sure what you are trying to do. |
I'm using it in an Express app. My intention is that 95% of my webapp will do something interesting, but for routes that it doesn't it'll transparently return the original webpage. In this example, that webpage is cb2.com. Imagine the following is require'd at the start of an express app such that it starts responding to http://localhost/stores var httpProxy = require('http-proxy'), module.exports = function(app) { with the www there I get errors. Without the www, the reply from cb2.com is a redirect to www.cb2.com. Testing in a browser, they only redirect when the url is missing the 'www.' prefix. So if http-proxy were to respect the www. prefix properly, the redirect wouldn't be an issue. Alternatively http-proxy could just follow the redirects, but that would still be a less-than-optimal solution since it would add latency to every request. (although it would be great for one-off pages) |
ah, I think that the problem is that http-proxy doesn't know that you are proxying from a url. |
doesn't make any difference, but I don't understand why it would have?
while this gives Invalid URL error: |
okay, I've reproduced this. www.cb2.com is actually responding with this error: <HTML><HEAD>
<TITLE>Invalid URL</TITLE>
</HEAD><BODY>
<H1>Invalid URL</H1>
The requested URL "/", is invalid.<p>
Reference #9.56dd54b8.1320795184.17828ee6
</BODY></HTML> |
Any idea why? You're right, I tried www.amazon.com and do not have such issues ... but if I curl www.cb2.com it works, so ... huh. Thanks for looking into it so quickly, much appreciated! |
barnesandnoble.com does cause the same issue with including www (or redirects without it) |
I'm facing the same issue and I think I know what is going on. It looks like some sites (www.cb2.com, m.yahoo.com, sites hosted on dreamhost etc.) use the "Host" header for internal routing to the correct host and http-proxy does not change the Host header for the outgoing request. In other words, if I send a request to the proxy running on localhost, the target of the proxy also sees the Host header as localhost (instead of target.server.com). There seems to be a changeOrigin option in the code that can be set to true to fix the Host header but:
A one line patch (as proof of concept): outgoing.headers.host = this.target.host + ':' + this.target.port; //around line 198 of http-proxy.js fixes this issue. Would it be possible to have the changeOrigin flag take effect for all proxy requests? Awesome library, BTW. |
good catch @gotwarlost I think that should be the default behavior. I can't think of a reasonable situation where it should be otherwise. |
now www.cb2.com and cb2.com are causing a redirect :( so my request to localhost:3000/stores ends up redirecting me to www.cb2.com/stores instead of just returning the HTML and keeping the user at localhost:3000. Sigh. I guess cb2 sees that I'm using localhost and that triggers the redirect to be sent back? I'll probably just go with a different approach of grabbing the body and then sending it to the user instead of trying to use a proxying package, shouldn't be that hard ... i hope. |
are you on the latest version? I'm able to proxy www.cb2.com. I'm getting a redirect on www.cb2.com/stores . however www.cb2.com/stores/ (with the trailing slash) works correctly. |
THE TRAILING SLASH! Hot damn! Your rock, thank you so much :) This is -wonderful-. |
@dominictarr, you rock! Works perfectly now |
sorry guys, I introduced a bug in another edgecase to fix this one, and in 0.7.6 you will need to pass in an option |
No worries - I was going to ask for this enhancement anyway ;) I noticed that the routing proxy still does not pass this option down to the http-proxy //line 86 in routing-proxy.js
['https', 'enable', 'forward'].forEach(function (key) {
if (options[key] !== false && self[key]) {
options[key] = self[key];
}
}); Could you please include the changeOrigin attribute in this list as well? Thanks. |
Thanks for the update -- any other secret parameters we can pass here? It would help me immensely to be able to drop part of the client's headers. This seems like the opposite behavior you'd want from a proxy, but the problem I'm running into is that the remote url is detecting the request is coming from a mobile device and redirecting rather than just returning the expected content --- some URLs are handled explicitly by the server while anything unspecified just falls through to the proxy. Unfortunately I'm not getting back the desired content because of the mobile-client-detection. If you can suggest an alternative middleware way that'd be fine too. I've been having a hard time using middleware due to GZIP data being returned from the remote server (again, something that I could strip out of the client's proxied-request) |
this is broken again on master -- I receive the following error when accessing servers as described above: Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data. It does, however, have a 200 status codes. To recreate: use the master branch to proxy www.gnc.com and then try, for example, /home/index.jsp I traced it back adn it's due to commit 2061c71 by Cloud9: |
@jmonster can you test this against |
I'm seeing this issue in version 0.8.0, and looking through the code there's no mention of |
Also seeing this in 0.8.0. Is there an update on {changeOrigin: true}? |
I added the functionality in my own fork since the main repo doesn't like it |
Note that in addition to reverting the lines in 2061c71, you also need to handle the |
Yeah, so I'm not sure what happened, but the master branch currently doesn't use the changeOrigin option for proxyRequest. I guess I'll add it again! |
After some poking around in the ProxyServerAddon and adding back the `host` param as the third parameter in here: app.use(function(req, res) { return proxy.web(req, res); }); I started poking around in node-http-proxy and found some earlier issues related to the "Host" header. http-proxy does not seem to change the Host header for outgoing requests, so you have to flag `changeOrigin` to make this happen. I added changeOrigin and this immediately fixes my bugs. So, I'm not sure if this is the correct implementation (perhaps this should be an option sent to the proxy via cmd line or a ./server proxy), but I figured I'd push it so you can see where I am at. Hope it helps. References: * [Use changeOrigin for proxyRequest](http-party/node-http-proxy@cee3e2f) * [Original host not being passed through](http-party/node-http-proxy#621) * [Host header comments](http-party/node-http-proxy#150 (comment))
Suppose I wanted to proxy a site like cb2.com -- visiting this site, you'll see that it immediately redirects you to www.cb2.com. This is a problem, as I'm unable to proxy the content of the site as my browser begins accessing www.cb2.com directly instead of continuing to proxy via localhost.
I've tried simply setting the host to be www.cb2.com instead of cb2.com, but it fails. No errors, the browser just waits.
The text was updated successfully, but these errors were encountered: