简体   繁体   English

无法在端口80或443上运行node.js Web服务

[英]cannot run node.js webservice on port 80 or 443

I have a VPS running CENTOS and I'm experimenting with Node.js. 我有一个运行CENTOS的VPS,并且正在尝试使用Node.js。 I ran an example node.js server running correctly on various ports, but whenever I tried to run node on port 80 (same error for 443) I get the following error: 我运行了一个示例Node.js服务器,它在各个端口上正确运行,但是每当我尝试在端口80上运行节点时(443的相同错误),我都会收到以下错误:

root@mic [~/Projects/NodeTutorial2]# node index.js
Server running on port 80.

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:883:11)
    at Server._listen2 (net.js:1021:14)
    at listen (net.js:1043:10)
    at Server.listen (net.js:1109:5)
    at Object.<anonymous> (/root/Projects/NodeTutorial2/index.js:8:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

I've tried to redirect port 80 to 3000 by doing: 我尝试通过执行以下操作将端口80重定向到3000:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

Apparently I executed this command twice because: when I did a: sudo iptables -t nat -L, it returned: 显然,我执行了此命令两次,因为:当我执行了:sudo iptables -t nat -L时,它返回了:

root@mic [~]# sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 3000
REDIRECT   tcp  --  anywhere             anywhere            tcp dpt:http redir ports 3000

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

but to no avail, I still get the same error. 但无济于事,我仍然遇到相同的错误。

I tried to see what is running on port 80 with the netstat -tulpn | 我试图用netstat -tulpn |查看端口80上正在运行什么。 grep:80 command and it returns: grep:80命令,它返回:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      477/httpd
tcp        0      0 :::80                       :::*                        LISTEN      477/httpd

So I had killed the process running on port 80 and I got kicked off my VPS immediately. 因此,我终止了在端口80上运行的进程,并立即启动了VPS。

What ports should I be running my node.js webservice? 我应该在哪个端口上运行node.js Web服务?

According to my putty settings the port I'm accessing is port 22, so I don't understand what is going on there. 根据我的腻子设置,我要访问的端口是端口22,所以我不知道那里发生了什么。 Additionally, I'm not planning to run my webservice as root. 此外,我不打算以root用户身份运行Web服务。 I've created another user with less permissions that will run the node.js webservice. 我创建了另一个具有较少权限的用户,该用户将运行node.js网络服务。

  1. Setup nodejs to use port 8000 设置nodejs以使用端口8000
  2. install nginx and set it up as a reverse proxy for your nodejs app. 安装nginx并将其设置为您的nodejs应用程序的反向代理。

Don't use Apache, it creates a thread/process for every request, while nginx doesn't. 不要使用Apache,它会为每个请求创建一个线程/进程,而nginx不会。 It works similar to nodejs event loop where you have a queue of request that need to be processed and nginx worker processes take each request from the queue and process it - in your case send a request to nodejs server and then wait for the response. 它的工作原理类似于nodejs事件循环,在这里,您有一个需要处理的请求队列,nginx工作进程从队列中获取每个请求并对其进行处理-在您的情况下,将请求发送到nodejs服务器,然后等待响应。

And never use nodejs on port 80, there are just too many use-cases to handle and there is no need to reinvent the wheel 而且永远不要在端口80上使用nodejs,因为有太多的用例需要处理,因此无需重新发明轮子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM