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.

4

1 回答 1

1

我现在以一种更简单的方式进行了这项工作。事实证明,我使用我的hostsports数组的自定义路由是矫枉过正的。我不完全确定为什么这会有所不同,以及是否是因为以下代码是反向代理而不是正向代理,但我继续并在node-http-proxy的 GitHub上实现了一个示例具有所需结果的页面:

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);

这有效地(并且使用更少的代码)根据主机名反向代理站点。结果是我的 WordPress 站点(位于 Apache 服务器后面)不再需要知道它在哪个端口上,因为该端口现在隐藏在反向代理后面。

为什么这种node-http-proxy方法与我以前的方法不同仍然暗示着我。任何进一步的理解都将是这篇文章的一个受欢迎的补充。

于 2013-08-01T12:20:38.017 回答