Hiding port number in WordPress URL when using node

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.


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.

链接地址: http://www.djcxy.com/p/96424.html

上一篇: 如何通过App Server从Web Server连接到数据库服务器

下一篇: 使用节点时隐藏WordPress URL中的端口号