如何在端口 80 上运行 Node.js?
我的目标是在端口 80 上运行 Node.js。这是因为我发现某些网络阻止了 Node.js,这些网络不允许来自任何其他端口的流量。
看来,最好的方法是通过 Node.js 代理 Apache。我尝试使用 node-http-proxy 来执行此操作,但我没有任何运气。
我正在使用的代码在这里:
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);
但是我不断收到端口 80 的错误“地址正在使用”。我一定做错了什么。
如何使用 node-http-proxy 通过 node.js 代理 Apache?这能让我在端口 80 上运行 node.js 吗? node-http-proxy 是实现这一目标的最佳方法吗?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
则在高端口 8080 或其他端口上运行您的应用程序
如果您不使用 ngnix 或 apache,
run your app on a high port 8080 or whatev then
If you are not using ngnix or apache
最简单的解决方案:安全地配置您的节点应用程序以在端口 80 上运行。
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/node
为什么我喜欢它?
参考链接: https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps< /a> (一篇关于如何在云托管上设置节点应用程序的精彩文章)。
The simplest solution: safely configure your node app to run on port 80.
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/node
Why do I like it?
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).
您需要做的是为您正在运行的服务器拥有 2 个 ip。 Apache 有 1 个 IP 绑定到端口 80,然后 Node.js 将另一个 IP 绑定到端口 80。
使用 Node 及其监听指令有 2 个值,例如: .listen(80, NODEJS_IP 或 DNS 名称);
其他一些建议。
我不会将 apache 与 nodejs 一起使用,因为它没有事件。所以这确实不推荐。我实际上会考虑使用 NGINX,因为它与 Node.js 的更好搭配。
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.
Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or 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.
目前不建议在端口 80 上运行节点,因为这需要以 root 身份运行节点。
您对 apache 的喜爱程度如何?通过 nginx 代理节点是一个经过验证的真实解决方案,其 nginx 配置如下:
Nginx 文档:
http:// wiki.nginx.org/HttpProxyModule
http://wiki.nginx.org/HttpUpstreamModule
It is currently not recommended to run node on port 80, as that requires running node as root.
How attached are you to apache? Proxying node through nginx is a tried and true solution, with an nginx-config such as this:
Nginx documentation:
http://wiki.nginx.org/HttpProxyModule
http://wiki.nginx.org/HttpUpstreamModule
您的代码看起来像示例代码,其中您创建从端口 80 到端口 9000 的基于节点的代理,然后在端口 9000 上创建基于节点的 HTTP 服务器。(即
Node:80 -> Node: 9000
)当你启动 Node 时,你会得到“地址正在使用”,因为 Apache 已经在使用端口 80。如果你想使用 Apache 进行代理,则必须在不同的端口上使用 Node(例如 9000)并让 Apache 侦听端口 80 并将请求转发到端口 9000 上的 Node。(即
Apache:80 -> Node:9000
)看起来您正在使用的库正在执行相反的操作:使用Node作为代理,将请求转发给Apache。在这种情况下,您必须将 Apache 配置为在端口 80 之外的其他端口上运行。(即
Node:80 -> Apache:9000
)。您想要执行
Node:80 -> Apache:9000
或Apache:9000 -> Node:80
,到底呢?评论后编辑:
如果你想做 Apache:80 -> Node:9000,您可以在 Apache 上使用
mod_proxy
并使用ProxyPass
/ProxyPassReverse
指令,类似于where
nodeurls
是您希望 Apache 转发到 Node 的 URL 系列。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
)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
)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
).Are you wanting to do
Node:80 -> Apache:9000
orApache:9000 -> Node:80
, in the end?EDIT after comments:
If you want to do
Apache:80 -> Node:9000
, you can usemod_proxy
on Apache and use theProxyPass
/ProxyPassReverse
directives, something likewhere
nodeurls
is the family of URLs you wish for Apache to forward to Node.如果您是非root用户,则无法使用低于1024的端口(在Unix系统中)运行或绑定。要允许非 root 用户可以在低于 1024 的端口上运行节点,请使用此命令。
If you are a non-root user, you cannot run or bind with ports lower than 1024 (in Unix system). To allow non-root user can run node on port lower than 1024 use this command.
我遇到了同样的问题,以下是我如何使用 node-http-proxy 侦听端口 80,然后转发到 Express 或 apache 来解决该问题。
https://stackoverflow.com/a/9645091/500270
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.
https://stackoverflow.com/a/9645091/500270
我也遇到了同样的问题,我只是将端口更改为 8080 就可以了。
I had the same issue, I just changed my port to 8080 and it worked.
如果您只是在开发环境模式下
,您可以
su root
,然后node index.js
或./node_modules/coffee-script/bin/coffee index.coffee< /代码>
if you just in develop environment mode
you can
su root
, thennode index.js
or./node_modules/coffee-script/bin/coffee index.coffee