如何将 nodejs 和 apache 放在同一个端口 80

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11172351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:54:16  来源:igfitidea点击:

how to put nodejs and apache in the same port 80

apachenode.jsredirectport

提问by user1203334

I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application). Thanks a lot

我必须将 nodejs 放在端口 80,但 apache 已经在使用它。如何将(nodejs 和 apache)都放在同一个端口 80 上?我需要它,因为在我的大学中,除了 PORT 80 之外,所有端口都被阻止了。(这是一个带有 nodejs 和 socket.io(websockets)的实时应用程序,另一边是一个 php 应用程序)。非常感谢

采纳答案by meloncholy

I've personally done this the other way round from @liammclennan. Some suggestthat proxying through Apache defeats some of the performance and scalability advantages of Node (don't have experience myself as my server doesn't get that much traffic, but from @liammclennan's link: "Every request that comes in through Apache will cause an Apache thread to wait/block until the response is returned from your Node.js process.", which obviously doesn't mesh well with Node's architecture.)

我个人已经从@liammclennan 反过来做了这件事。有些人认为通过 Apache 代理会破坏 Node 的一些性能和可扩展性优势(我自己没有经验,因为我的服务器没有获得那么多流量,但来自 @liammclennan 的链接:“通过 Apache传入的每个请求都会导致一个 Apache 线程等待/阻塞,直到从您的 Node.js 进程返回响应。”,这显然与 Node 的架构不匹配。)

I used node-http-proxyto set up a Node proxy server roughly as described in the first link (my Node proxy runs on port 80; Apache and my other Node services don't). Seems to be working well so far, though I have had occasional stability problems that I've 'solved' through checking the proxy's still running with a cron job (edit:it seems a lot more stable these days). The proxy's pretty lightweight, taking up about 30MB memory.

我使用node-http-proxy大致按照第一个链接中的描述设置了一个 Node 代理服务器(我的 Node 代理在端口 80 上运行;Apache 和我的其他 Node 服务没有)。到目前为止似乎运行良好,尽管我偶尔会遇到稳定性问题,我通过检查代理是否仍在使用 cron 作业运行来“解决”(编辑:这些天似乎更稳定了)。代理非常轻巧,占用大约 30MB 内存。

回答by Matej

I do this via node.js proxy..

我通过 node.js 代理执行此操作..

Install http-proxywith npmor official page

安装http-proxy使用npm官方网页

Example:

例子:

var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
    hostnameOnly: true,
    router: {
        'domain.com':       '127.0.0.1:81',
        'domain.co.uk':     '127.0.0.1:82',
        '127.0.0.1':        '127.0.0.1:83'
    }
});

proxyServer.listen(80);

This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with foreverand adding an entry to init.dso your proxy is up in case system shuts down.

这将创建一个节点进程侦听端口 80,并转发对 :81,82,83 等域的请求。我建议运行它forever并添加一个条目,init.d以便您的代理在系统关闭时启动。

回答by Synexis

You can also use Apache 2's mod_proxy and mod_proxy_http, which might be more reliable or perform better depending on your system.

您还可以使用 Apache 2 的 mod_proxy 和 mod_proxy_http,它们可能更可靠或性能更好,具体取决于您的系统。

Here's an example:

下面是一个例子:

# Use Apache for requests to http://example.com/
# but use Node.js for requests to http://example.com/node/
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example/
    <Location /node>
        ProxyPass http://127.0.0.1:8124/
        ProxyPassReverse http://127.0.0.1:8124/
    </Location>
</VirtualHost>

And of course you can modify the directives to your needs, such as using a different port for your virtual host (e.g., 443), different port for Node.js, or set up the proxy under a different block, such as for a subdomain (e.g., node.example.com).

当然,您可以根据需要修改指令,例如为虚拟主机使用不同的端口(例如 443),为 Node.js 使用不同的端口,或在不同的块下设置代理,例如为子域(例如,node.example.com)。

回答by liammclennan

You can't. You have to run node.js on another port and then proxy requests through apache. You can do this using mod_proxy

你不能。您必须在另一个端口上运行 node.js,然后通过 apache 代理请求。您可以使用 mod_proxy 执行此操作

http://davybrion.com/blog/2012/01/hosting-a-node-js-site-through-apache/

http://davybrion.com/blog/2012/01/hosting-a-node-js-site-through-apache/

回答by Ask Bj?rn Hansen

I usually use haproxyas the front-end in situations like that and have that proxy to the appropriate backend server. (Though making your node.js process a proxy server is a valid approach too depending on your needs).

在这种情况下,我通常使用haproxy作为前端,并将该代理用于适当的后端服务器。(尽管根据您的需要,让您的 node.js 处理代理服务器也是一种有效的方法)。

回答by Nur Rony

I found a cool gist Run apache and nodejs on port 80. did not try it yet but will do of course

我发现了一个很酷的要点Run apache and nodejs on port 80。还没有尝试,但当然会做

Step 1

Get a VPS that offers 2or more IP addresses.

Step 2

From the WHM cPanel, find the menu item Service Configuration, select Apache Configurationand then click on Reserved IPs Editor.

Step 3

Tick the IP address you DON'T WANTApache to listen to, and write it down so you can use it in the next step. Click Save.

Step 4

Install Node.js, and create a server like this:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello, world!');
});

server.listen(80, '111.111.111.111');

Replacing 111.111.111.111with the IP address you previously reserved from the WHM cPanel.

Step 5

Stop wasting your time and never listen to those telling you to use mod_rewriteto proxy Node.js again.

第1步

获取提供2 个或更多 IP 地址的 VPS 。

第2步

从 WHM cPanel 中,找到菜单项Service Configuration,选择Apache Configuration然后单击Reserved IPs Editor

第 3 步

勾选您的IP地址不想阿帕奇听,并把它写下来,这样你可以在下一步中使用它。单击Save

第四步

安装 Node.js,然后像这样创建一个服务器:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello, world!');
});

server.listen(80, '111.111.111.111');

替换111.111.111.111为您之前从 WHM cPanel 保留的 IP 地址。

第 5 步

停止浪费你的时间,永远不要听那些告诉你mod_rewrite再次使用 代理 Node.js 的人。

Update:

更新:

We can solve a problem in many different ways and IMHO, we should at least know each possible way . We can do it without buying a new IP of course putting a proxy in front of both Apache and NodeJS server each running other ports except 80.

我们可以用许多不同的方式解决问题,恕我直言,我们至少应该知道每一种可能的方式。我们可以在不购买新 IP 的情况下做到这一点,当然,在 Apache 和 NodeJS 服务器前面放置一个代理,每个服务器都运行除 80 之外的其他端口。