使用端口 80 运行 Node.js 的最佳实践(Ubuntu/Linode) [英] Best practices when running Node.js with port 80 (Ubuntu / Linode)

查看:23
本文介绍了使用端口 80 运行 Node.js 的最佳实践(Ubuntu/Linode)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在云Linux节点上设置我的第一个Node.js服务器,我对Linux admin的细节还很陌生.(顺便说一句,我不会同时尝试使用 Apache.)

I am setting up my first Node.js server on a cloud Linux node and I am fairly new to the details of Linux admin. (BTW I am not trying to use Apache at the same time.)

一切都安装正确,但我发现除非我使用 root login,否则我无法使用 node.js 在 port 80 上侦听.但是,出于安全原因,我宁愿不以 root 身份运行它.

Everything is installed correctly, but I found that unless I use the root login, I am not able to listen on port 80 with node. However I would rather not run it as root for security reason.

最佳做法是:

  1. 为节点设置良好的权限/用户以使其安全/沙盒化?
  2. 允许在这些限制范围内使用端口 80.
  3. 启动节点并自动运行.
  4. 处理发送到控制台的日志信息.
  5. 任何其他一般维护和安全问题.

我应该将端口 80 流量转发到不同的侦听端口吗?

Should I be forwarding port 80 traffic to a different listening port?

谢谢

推荐答案

Port 80

我在云实例上所做的是使用以下命令将端口 80 重定向到端口 3000:

Port 80

What I do on my cloud instances is I redirect port 80 to port 3000 with this command:

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

然后我在端口 3000 上启动我的 Node.js.对端口 80 的请求将被映射到端口 3000.

Then I launch my Node.js on port 3000. Requests to port 80 will get mapped to port 3000.

您还应该编辑您的 /etc/rc.local 文件并添加该行减去 sudo.这将在机器启动时添加重定向.您不需要 /etc/rc.local 中的 sudo 因为那里的命令在系统启动时以 root 运行.

You should also edit your /etc/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. You don't need sudo in /etc/rc.local because the commands there are run as root when the system boots.

使用 forever 模块来启动您的 Node.js.它将确保它在崩溃时重新启动,并将控制台日志重定向到一个文件.

Use the forever module to launch your Node.js with. It will make sure that it restarts if it ever crashes and it will redirect console logs to a file.

将您的 Node.js 启动脚本添加到您为端口重定向编辑的文件中,/etc/rc.local.这将在系统启动时运行您的 Node.js 启动脚本.

Add your Node.js start script to the file you edited for port redirection, /etc/rc.local. That will run your Node.js launch script when the system starts.

这不仅适用于 Linode,也适用于 Digital Ocean、AWS EC2 和其他 VPS 提供商.但是,在基于 RedHat 的系统上,/etc/rc.local/ect/rc.d/local.

This not only applies to Linode, but Digital Ocean, AWS EC2 and other VPS providers as well. However, on RedHat based systems /etc/rc.local is /ect/rc.d/local.

这篇关于使用端口 80 运行 Node.js 的最佳实践(Ubuntu/Linode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆