使用 PM2 将节点作为服务运行 - 连接被拒绝

rur*_*urp 6 ubuntu nginx node.js digital-ocean pm2

我正在按照教程在 Digital Ocean Ubuntu 发行版上设置节点项目。systemctl status pm2显示服务在线:

App name ? id ? mode ? pid   ? status  ? restart ? uptime ? memory      ? watching
server   ? 1  ? fork ? 19999 ? online  ? 0       ? 0s     ? 21.219 MB   ? disabled ?
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试导航到域时,出现连接被拒绝的错误。如果我使用 npm start 运行该应用程序,则它可以在端口 5000 上正常启动。我已经安装并配置了 Nginx,如下所示:

server {
    listen 80;

    server_name <mysite.com>;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}
Run Code Online (Sandbox Code Playgroud)

Node 版本为 v6.3.0,pm2 版本为 1.1.3。

Lor*_*ley 4

我经历过类似的事情:


  1. 使用nodeusing运行node dist/app.js,可以按预期访问 TCP 服务器:
$ nc -vz 1.1.1.1 5000
> Connection to 1.1.1.1 port 5000 [tcp/commplex-main] succeeded!
Run Code Online (Sandbox Code Playgroud)

(注:不是真实的IP地址^^^)

  1. pm2使用 using运行pm2 restart dist/app.js,TCP 服务器拒绝连接:
$ nc -vz 1.1.1.1 5000
> nc: connectx to 1.1.1.1 port 5000 (tcp) failed: Connection refused
Run Code Online (Sandbox Code Playgroud)

检查后.env,我意识到我不小心将服务器主机 IP 留在了开发本地主机上127.0.0.1,而不是将其更改为公共接口0.0.0.0

切换到公共界面后0.0.0.0,他们的行为方式相同并且连接成功。

也许最好不要采用这种方法,而是使用stream以下不同的 Digital Ocean 教程将 Nginx 配置为 TCP 的反向代理服务器: https: //www.digitalocean.com/community/tutorials/how-to-develop-a- node-js-tcp-server-application-using-pm2-and-nginx-on-ubuntu-16-04#step-4-%E2%80%94-set-up-nginx-as-a-reverse-proxy -服务器