1

I am trying to use node-http-proxy to proxy my site to another site eg google.com whenever I try some of the examples they give in the docs on github instead of my site showing the content of google.com it just redirects to google.com this is the code I am using

var proxy = require("http-proxy");
var options = {
   router: {
      "myserver.com" : "google.com"
   }
}
var proxyServer = proxy.createServer(options);
proxyServer.listen(80);

I am doing this wrong and its just for reverse proxy? If it is what in the node-http-proxy allows me to do regular proxies?

powerc9000
  • 2,030
  • 19
  • 30

2 Answers2

3

Because google.com returns a 301 redirect, it'll send the browser to a different location. In this case, you can just use the full url (www.google.com), and it will not send back the 301 redirect to www.google.com. The important thing is that, since google.com is sending back a 301 redirect, you have to use the redirected website to not redirect the client. For example, if you run curl google.com, you'll see a 301 redirect, where if you just run curl www.google.com the redirect is not present.

CodeRarity
  • 532
  • 2
  • 7
1

Trying CodeRarity's answer gave me:

> curl www.google.com    
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">  
<TITLE>302 Moved</TITLE></HEAD><BODY>                                           
<H1>302 Moved</H1>                                                              
The document has moved                                                          
<A HREF="http://www.google.co.uk/">here</A>.                                    
</BODY></HTML> 
>

So as you can see, you might still get a redirection even with www.google.com depending on your location.

Why not try a different web site that is not trying to be as clever?

Julian Knight
  • 4,716
  • 2
  • 29
  • 42
  • Well when I use the proxy if I try another url, I checked the headers and the proxyer does a 301 redirect for external urls. Should it not just load the page? – powerc9000 May 08 '12 at 17:40
  • Hmm, well I'm still not quite convinced by http-proxy myself. It is very immature as a proxy (e.g. you don't seem to be able to use the proxy-table with https). If it is doing a redirect, you will probably have to implement your own "getter" which Node can do very easily with the built-in request library. – Julian Knight May 18 '12 at 11:52