Node-http-proxy: Basic reverse proxy doesn't work

Created on 20 Feb 2016  ·  3Comments  ·  Source: http-party/node-http-proxy

Hi, maybe I'm not understanding the purpose of this library correctly but it does mention that it can be used for reverse proxies. I posted this on SO but didn't get any answers, it must be something I am not fundamentally understanding:

const http = require('http');
const httpProxy = require('http-proxy');

const targetUrl = 'http://www.google.co.uk';

const proxy = httpProxy.createProxyServer({
    target: targetUrl
});

http.createServer(function (req, res) {

    proxy.web(req, res);

}).listen(6622);

For example I would expect http://localhost:6622/images/nav_logo242.png to proxy to http://www.google.co.uk/images/nav_logo242.png instead of returning a 404 not found.

Thanks.

Most helpful comment

@DominicTobias I know this is closed, but there is a setting for that. If you set “changeOrigin” to true it will automatically change the host header to the upstream servers for you.

All 3 comments

It turns out this was failing as it needed a Host header:

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    proxyReq.setHeader('Host','www.google.co.uk');
});

@DominicTobias I know this is closed, but there is a setting for that. If you set “changeOrigin” to true it will automatically change the host header to the upstream servers for you.

@pyper ah that's nice thanks

Was this page helpful?
0 / 5 - 0 ratings