1

I'm using the node module node-http-proxy to redirect specific virtual hosts to Apache for a couple of WordPress sites. My Node 'server' listens on port 80, and redirects to SOME_OTHER_PORT which Apache is listening on.

It works fine (when I change the WordPress settings to account for SOME_OTHER_PORT as per suggestions from users). But the port number is always included in the URL for every page on the WordPress sites. Is there any other way of doing this such that I don't have to see these ugly port numbers?

I can't imagine this would be possible without a considerable change in my setup, but I thought it would be worth asking.

Here's the code I use for proxying in node:

// Module dependancies
var httpProxy = require('http-proxy'),
express = require('express')(),
connect = require('connect');

// Http proxy-server
httpProxy.createServer(function (req, res, proxy) {

    // Host Names and Ports
    var hosts = [
        'www.some-website.co.uk'
    ];

    // Ports

    var ports = [

        8000

    ];

    var host = req.headers.host;
    var port = hosts.indexOf(host) > -1 ? ports[hosts.indexOf(host)] : 9000;

    // Now proxy the request
    proxy.proxyRequest(req, res, {
        host: host,
        port: port
    });
})
.listen(80);

Port 80 is where all my traffic comes. Port 8000 is a potential WordPress site sat behind an Apache server. Port 9000 is a potential Node app listening elsewhere.

Any insight as to whether this is a shoddy way to do it would also be welcome.

shennan
  • 10,798
  • 5
  • 44
  • 79
  • Are you redirecting or reverse-proxying? If you just proxy requests from port 80 to port xxxx you should not need the port to be visible in any url or setting. – Andreas Hultgren Aug 01 '13 at 10:50
  • @AndreasHultgren I've updated the question with the specific proxy code I use in node. I'm not sure if this is reverse proxy or normal. Are you saying a reverse proxy will actually result in a redirect on the browser? Elaboration on the differences between reverse and standard proxy would be up-votable. :-) – shennan Aug 01 '13 at 10:59
  • The only difference is what you consider the target you want to connect to, but I guess it's more confusing than relevant for a comment here ([but this SO answer is better than anything I could hope to write](http://stackoverflow.com/questions/224664/difference-between-proxy-server-and-reverse-proxy-server)). What was unclear was whether you proxy or redirect the requests. You should do the former, since a redirect would change the url in the browser. If it doesn't (you still see port 80) for all intents and purposes both your browser and WP should behave like it's running on port 80. – Andreas Hultgren Aug 01 '13 at 11:13
  • And from looking at your code it seems to me you are proxying just like you should. – Andreas Hultgren Aug 01 '13 at 11:14
  • @AndreasHultgren No, the port is currently displaying 8000 on the WordPress sites (which is the port I have randomly decided to assign to `www.some-website.co.uk`). At no point is port 80 being used other than to traffic incoming requests in the right direction. Once it forwards the request on to port 8000 for the WordPress site, the URL address throws `:8000` into the mix - which is my problem. The site works, but the URL is ugly, not-to-mention revealing. – shennan Aug 01 '13 at 11:30

1 Answers1

1

I've now got this working in a much simpler way. It turns out my custom routing with my hosts and ports arrays was overkill. I'm not entirely sure why this makes a difference, and whether it is because the following code is reverse-proxy as opposed to a forward proxy, but I went ahead and implemented one of the examples on node-http-proxy's GitHub page which wields the desired results:

var options = {
  hostnameOnly: true,
  router: {
    'www.some-apache-website.co.uk': '127.0.0.1:9000',
    'www.some-node-website.co.uk': '127.0.0.1:8000'
  }
};

var proxyServer = httpProxy.createServer(options);

proxyServer.listen(80);

This effectively (and with much less code) reverse-proxies the sites based on the host name. The result is that my WordPress site (sitting behind the Apache server) no longer needs to know which port it is on, as the port is now hiding behind the reverse proxy.

Why this node-http-proxy method is different from my previous one still alludes me. Any further understanding would be a welcome addition to this post.

shennan
  • 10,798
  • 5
  • 44
  • 79