如何在端口 80 上运行 Node.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6109089/
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
How do I run Node.js on port 80?
提问by Kit
My aim is to run Node.js on port 80. This is because I am finding node.js is being blocked from certain networks which do not allow traffic from any other port.
我的目标是在端口 80 上运行 Node.js。这是因为我发现 node.js 被某些网络阻止,这些网络不允许来自任何其他端口的流量。
It appears that the best way to do this is by proxying Apache through Node.js. I have tried using node-http-proxyto do this but I have not had any luck.
似乎最好的方法是通过 Node.js 代理 Apache。我曾尝试使用node-http-proxy来做到这一点,但我没有任何运气。
The code I am using is here:
我正在使用的代码在这里:
var util = require('util'),
http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createServer(9000, 'localhost').listen(80);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
But I keep getting the error "Address in use" for port 80. I must be doing something wrong.
但是我一直收到端口 80 的错误“地址正在使用”。我一定是做错了什么。
How do I proxy Apache through node.js using node-http-proxy? Will this enable me to run node.js on port 80? And is node-http-proxy the best way to achieve this?
如何使用 node-http-proxy 通过 node.js 代理 Apache?这能让我在端口 80 上运行 node.js 吗?node-http-proxy 是实现这一目标的最佳方式吗?
Thank you.
谢谢你。
采纳答案by PaulM
What you need to do is have 2 ip's for the server you are running. Apache has 1 ip bound to port 80 and then node.js has the other ip bound to port 80.
您需要做的是为您正在运行的服务器提供 2 个 ip。Apache 有 1 个 ip 绑定到端口 80,然后 node.js 有另一个 ip 绑定到端口 80。
Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or DNS NAME);
使用节点及其监听指令有 2 个值,例如。.listen(80, NODEJS_IP 或 DNS NAME);
Some other advice.
其他一些建议。
I would not use apache with nodejs as it's not evented. So this really isn't recommended. I would actually look into using NGINX as its a much better pairing with Node.
我不会将 apache 与 nodejs 一起使用,因为它没有发生。所以这个真的不推荐。我实际上会考虑使用 NGINX 作为它与 Node.js 更好的配对。
回答by fullstacklife
run your app on a high port 8080 or whatev then
然后在高端口 8080 上运行您的应用程序
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
If you are not using ngnix or apache
如果您没有使用 ngnix 或 apache
回答by Kyle Chadha
The simplest solution: safely configure your node app to run on port 80.
最简单的解决方案:安全地配置您的节点应用程序以在端口 80 上运行。
sudo apt-get install libcap2-binsudo setcap cap_net_bind_service=+ep /path/to/node- Ta da! You're done.
sudo apt-get install libcap2-binsudo setcap cap_net_bind_service=+ep /path/to/node- 哒哒!你完成了。
Why do I like it?
为什么我喜欢它?
- You don't have to use apache or nginx
- You don't have to run your application as root
- You won't have to forward ports (and handle that each time your machine boots)
- 你不必使用 apache 或 nginx
- 您不必以 root 身份运行您的应用程序
- 您不必转发端口(并在每次机器启动时处理)
Reference Link: https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps(A great article on how to set up your node app on cloud hosting).
参考链接:https: //www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps(一篇很棒的文章关于如何在云托管上设置您的节点应用程序)。
回答by Waquo
It is currently not recommended to run node on port 80, as that requires running node as root.
目前不建议在端口 80 上运行 node,因为这需要以 root 身份运行 node。
How attached are you to apache? Proxying node through nginx is a tried and true solution, with an nginx-config such as this:
你对 apache 的依恋程度如何?通过 nginx 代理节点是一个久经考验的真正解决方案,使用 nginx-config 如下:
upstream node_cluster {
ip_hash;
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 0.0.0.0:80;
server_name foo;
access_log /var/log/nginx/foo.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://node_cluster/;
proxy_redirect off;
}
}
Nginx documentation:
Nginx 文档:
http://wiki.nginx.org/HttpProxyModule
http://wiki.nginx.org/HttpProxyModule
回答by nicolaskruchten
Your code looks like example code in which you're creating a Node-based proxy from port 80 to port 9000, and then creating a Node-based HTTP server on port 9000. (i.e. Node:80 -> Node:9000)
您的代码看起来像示例代码,其中您正在从端口 80 到端口 9000 创建基于节点的代理,然后在端口 9000 上创建基于节点的 HTTP 服务器。(即Node:80 -> Node:9000)
You are getting "address in use" when you launch Node because Apache is already using port 80. If you want to use Apache to proxy, you must use Node on a different port (say 9000) and have Apache listening on port 80 and forwarding the requests to Node on port 9000. (i.e. Apache:80 -> Node:9000)
当您启动 Node 时,您会收到“正在使用的地址”,因为 Apache 已经在使用端口 80。如果您想使用 Apache 进行代理,您必须在不同的端口(例如 9000)上使用 Node,并让 Apache 监听端口 80 并转发在端口 9000 上对节点的请求。(即Apache:80 -> Node:9000)
It looks like the library you're using is for doing the opposite: using Node as the proxy and forwarding requests to Apache. In this case you must configure Apache to run on another port than port 80. (i.e. Node:80 -> Apache:9000).
看起来您正在使用的库用于做相反的事情:使用 Node 作为代理并将请求转发到 Apache。在这种情况下,您必须将 Apache 配置为在端口 80 之外的另一个端口上运行。(即Node:80 -> Apache:9000)。
Are you wanting to do Node:80 -> Apache:9000or Apache:9000 -> Node:80, in the end?
你想要做Node:80 -> Apache:9000或Apache:9000 -> Node:80,到底是谁?
EDIT after comments:
If you want to do Apache:80 -> Node:9000, you can use mod_proxyon Apache and use the ProxyPass/ProxyPassReversedirectives, something like
评论后编辑:如果你想做Apache:80 -> Node:9000,你可以mod_proxy在 Apache 上使用并使用ProxyPass/ProxyPassReverse指令,比如
ProxyPass /nodeurls/ http://localhost:9000/
ProxyPassReverse /nodeurls/ http://localhost:9000/
where nodeurlsis the family of URLs you wish for Apache to forward to Node.
nodeurls您希望 Apache 转发到 Node.js 的 URL 系列在哪里?
回答by uglymunky
I was having the same issue, here is how I resolved it using node-http-proxy to listen on port 80, then forward to either express or apache.
我遇到了同样的问题,这是我如何使用 node-http-proxy 侦听端口 80,然后转发到 express 或 apache 来解决它的。
回答by jiahut
if you just in develop environment mode
如果你只是在开发环境模式下
you can su root, then
你可以su root,然后
node index.jsor ./node_modules/coffee-script/bin/coffee index.coffee
node index.js或者 ./node_modules/coffee-script/bin/coffee index.coffee

