通过端口80上的PM2运行节点应用程序

Ale*_*ach 8 port web-services amazon-ec2 node.js pm2

我有一个快递,我想在端口80上运行. - > app.listen(80);

我正在PM2用来管理应用程序(重启,停止,监控等).我有一个部署shell脚本,其最后一个命令是PM2 restart index.从控制台输出中,我看到没有错误并PM2报告它已成功完成命令.然而,当我到达my.ec2.ip.address:80该网站不起来.此外,如果我node index.js在我的服务器项目目录中运行,我会得到一个Error: listen EACCES 0.0.0.0:80.这对我来说有点意义,因为端口80低于1024,因此是特权端口. sudo node index.js将允许发射工作.

我是unix,服务器,权限和部署的新手,所以除了解决方案之外,我们将非常感谢对我的问题做出贡献的基本概念的解释.例如..简单地以超级用户身份运行我的节点应用程序是不是很糟糕?运行PM2(因此可能以...运行节点)root /超级用户是一种好习惯吗?命令sudo PM2 restart index导致sudo: pm2: command not found.PM2跑步时为什么没找到sudo PM2..如果PM2在我的路上?

但最终,在使用时PM2如何确保我的服务器在端口80上运行?未找到.

Ash*_*Jha 7

不要使用端口80,在其他端口(例如8080)上运行,并使用此命令将80重定向到该端口

  sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Run Code Online (Sandbox Code Playgroud)


Frx*_*rem 6

It's good to run as little as possible as a priviliged user, as you want to restrict the potential damage in case someone exploits your program. You don't want to run your Node code as root unless you absolutely have to.

Therefore, it's better to run your Node program on an unprivileged port (say, port 8000), and instead have a lightweight web server such as Nginx listen on port 80 and simply forward traffic to your Node program.

If you want to go with Nginx, you can use this configuration to do exactly what I described above, and then just listen with your Node program on port 3000:

server {
  listen 80 default;
  listen [::]:80 default;

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
  }
}
Run Code Online (Sandbox Code Playgroud)