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.
我的目标是运行Node。js在端口80上。这是因为我找到了node。js正被某些不允许从任何其他端口传输的网络阻塞。
It appears that the best way to do this is by proxying Apache through Node.js. I have tried using node-http-proxy to 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?
如何通过节点代理Apache。js使用node-http-proxy ?这会让我运行node吗?js在端口80上吗?node-http-proxy是实现这一目标的最佳方式吗?
Thank you.
谢谢你!
7 个解决方案
#1
29
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.
您需要做的是为正在运行的服务器设置两个ip。Apache有1个ip绑定到80端口,然后是节点。js将另一个ip绑定到端口80。
Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or DNS NAME);
使用node及其listen指令有2个值,如.listen(80, NODEJS_IP或DNS名称);
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.
我不会使用带有nodejs的apache,因为它没有被调用。这是不推荐的。我会考虑使用NGINX作为与Node更好的配对。
#2
97
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
#3
26
The simplest solution: safely configure your node app to run on port 80.
最简单的解决方案:安全配置节点应用程序,使其在端口80上运行。
sudo apt-get install libcap2-bin
- sudo apt-get安装libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/node
- sudo setcap cap_net_bind_service = + ep /道路/ /节点
- Ta da! You're done.
- 助教哒!你就完成了。
Why do I like it?
我为什么喜欢它?
- You don't have to use apache or nginx
- 您不必使用apache或nginx
- You don't have to run your application as root
- 您不必以root用户身份运行应用程序
- You won't have to forward ports (and handle that each time your machine boots)
- 您不需要转发端口(并且每次都要处理您的机器引导的端口)
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- ubuntu-vps(一篇关于如何在云托管上设置节点应用程序的优秀文章)。
#4
18
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
http://wiki.nginx.org/HttpUpstreamModule
http://wiki.nginx.org/HttpUpstreamModule
#5
8
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服务器。(即节点:80 - >节点: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
)
当您启动节点时,您将获得“使用中的地址”,因为Apache已经在使用端口80。如果希望使用Apache进行代理,则必须在另一个端口(比如9000)上使用Node,并让Apache监听端口80,并将请求转发到端口9000上的Node。(例如Apache:80 - >节点: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
).
看起来您正在使用的库的用途恰恰相反:使用节点作为代理并将请求转发给Apache。在这种情况下,您必须配置Apache在另一个端口上运行,而不是端口80。(即节点:80 - > Apache:9000)。
Are you wanting to do Node:80 -> Apache:9000
or Apache:9000 -> Node:80
, in the end?
您是想做节点:80 -> Apache:9000还是Apache:9000 ->节点:80,最后呢?
EDIT after comments: If you want to do Apache:80 -> Node:9000
, you can use mod_proxy
on Apache and use the ProxyPass
/ProxyPassReverse
directives, something like
编辑注释后:如果您想做Apache:80 ->节点:9000,您可以在Apache上使用mod_proxy,并使用ProxyPass/ProxyPassReverse指令,类似这样的操作
ProxyPass /nodeurls/ http://localhost:9000/
ProxyPassReverse /nodeurls/ http://localhost:9000/
where nodeurls
is the family of URLs you wish for Apache to forward to Node.
nodeurl是您希望Apache转发到Node的url的家族。
#6
3
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。
https://*.com/a/9645091/500270
https://*.com/a/9645091/500270
#7
-2
if you just in develop environment mode
如果你只是在开发环境模式
you can su root
, then
那么你可以根了
node index.js
or ./node_modules/coffee-script/bin/coffee index.coffee
索引节点。js或。/ node_modules coffee-script index.coffee / bin /咖啡
#1
29
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.
您需要做的是为正在运行的服务器设置两个ip。Apache有1个ip绑定到80端口,然后是节点。js将另一个ip绑定到端口80。
Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or DNS NAME);
使用node及其listen指令有2个值,如.listen(80, NODEJS_IP或DNS名称);
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.
我不会使用带有nodejs的apache,因为它没有被调用。这是不推荐的。我会考虑使用NGINX作为与Node更好的配对。
#2
97
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
#3
26
The simplest solution: safely configure your node app to run on port 80.
最简单的解决方案:安全配置节点应用程序,使其在端口80上运行。
sudo apt-get install libcap2-bin
- sudo apt-get安装libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/node
- sudo setcap cap_net_bind_service = + ep /道路/ /节点
- Ta da! You're done.
- 助教哒!你就完成了。
Why do I like it?
我为什么喜欢它?
- You don't have to use apache or nginx
- 您不必使用apache或nginx
- You don't have to run your application as root
- 您不必以root用户身份运行应用程序
- You won't have to forward ports (and handle that each time your machine boots)
- 您不需要转发端口(并且每次都要处理您的机器引导的端口)
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- ubuntu-vps(一篇关于如何在云托管上设置节点应用程序的优秀文章)。
#4
18
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
http://wiki.nginx.org/HttpUpstreamModule
http://wiki.nginx.org/HttpUpstreamModule
#5
8
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服务器。(即节点:80 - >节点: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
)
当您启动节点时,您将获得“使用中的地址”,因为Apache已经在使用端口80。如果希望使用Apache进行代理,则必须在另一个端口(比如9000)上使用Node,并让Apache监听端口80,并将请求转发到端口9000上的Node。(例如Apache:80 - >节点: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
).
看起来您正在使用的库的用途恰恰相反:使用节点作为代理并将请求转发给Apache。在这种情况下,您必须配置Apache在另一个端口上运行,而不是端口80。(即节点:80 - > Apache:9000)。
Are you wanting to do Node:80 -> Apache:9000
or Apache:9000 -> Node:80
, in the end?
您是想做节点:80 -> Apache:9000还是Apache:9000 ->节点:80,最后呢?
EDIT after comments: If you want to do Apache:80 -> Node:9000
, you can use mod_proxy
on Apache and use the ProxyPass
/ProxyPassReverse
directives, something like
编辑注释后:如果您想做Apache:80 ->节点:9000,您可以在Apache上使用mod_proxy,并使用ProxyPass/ProxyPassReverse指令,类似这样的操作
ProxyPass /nodeurls/ http://localhost:9000/
ProxyPassReverse /nodeurls/ http://localhost:9000/
where nodeurls
is the family of URLs you wish for Apache to forward to Node.
nodeurl是您希望Apache转发到Node的url的家族。
#6
3
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。
https://*.com/a/9645091/500270
https://*.com/a/9645091/500270
#7
-2
if you just in develop environment mode
如果你只是在开发环境模式
you can su root
, then
那么你可以根了
node index.js
or ./node_modules/coffee-script/bin/coffee index.coffee
索引节点。js或。/ node_modules coffee-script index.coffee / bin /咖啡