Skip to content
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

ReverseProxy to another ReverseProxy server #837

Closed
danazkari opened this issue Jun 10, 2015 · 4 comments
Closed

ReverseProxy to another ReverseProxy server #837

danazkari opened this issue Jun 10, 2015 · 4 comments

Comments

@danazkari
Copy link

The situation

Hi everyone, I have an app running on heroku and I made the HUGE mistake of hardcoding the heroku URL to the app. Now I'm migrating my API to another server with another URL.

What I'd like to do

Since I can't be sure that all of my users WILL actually update their up when I roll out the update, I'd like to use node-http-proxy to do a reverse proxy so all traffic goes to my new server.

What I've got so far

I've got to the point where I can actually redirect traffic to the new server, but that server is in a cluster with a bunch of other apps hosted in it, and the URL it resolves to, has a Varnish VirtualHost which uses the URL to resolve to my hosted API. So I'm getting an Error 503 Service Unavailable because the URL that the Varnish Server is getting (presumably) is the one from heroku (or localhost on my testing machine) which doesn't resolve to any valid existing VirtualHost inside Varnish.

This is what I'm using.

var server = http.createServer(function(req, res) {
    proxy.web(req, res, {
        target: 'http://thenew.host.com/',
        toProxy: true,
        autoRewrite: true
    });
});

IMO what I need is a way to preserve the host URL, but not sure if there's a way to do that with node-http-proxy.

Thanks!

@alfonso-presa
Copy link

This has worked for me previously

var http  = require('http'),
    httpProxy = require('http-proxy');

var url = args.url || process.env.PROXY_HOST || 'http://my.proxyed.domain';
var host = args.host || 'localhost';

var proxy = httpProxy.createProxyServer({
    target: url,
    xfwd: false,
    hostRewrite: host,
    autoRewrite: true,
    headers: {
        Host: url.substring(url.indexOf('//') +2),
        Origin: url
    }
});

http.createServer(function(req, res) {
    var newReferer = req.headers.referer ? req.headers.referer.replace(new RegExp("http://"+host), url) : undefined;
    proxy.web(req, res, {
        Referer: newReferer
    });
}).listen(80);

@danazkari
Copy link
Author

Real sorry for not posting a response before, and thanks for your help! I've been dealing with all sorts of craziness this past couple of weeks in other areas of my API, will try your solution today and report back.

Cheers!

@danazkari
Copy link
Author

@alfonso-presa worked like a charm, thanks so much for your help!

FYI, I removed the args.url and args.host, nonetheless, works perfectly.

Closing!

@alfonso-presa
Copy link

I'm glad it helped :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants