我关注 this在 Digital Ocean Ubuntu 发行版上设置 Node 项目的教程。 systemctl status pm2
显示服务在线:
App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching
server │ 1 │ fork │ 19999 │ online │ 0 │ 0s │ 21.219 MB │ disabled │
但是,当我尝试导航到域时出现连接被拒绝错误。如果我使用 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;
}
}
Node版本是v6.3.0,pm2版本是1.1.3。
最佳答案
我经历过类似的事情:
node
一起运行使用 node dist/app.js
,可以按预期访问 TCP 服务器:$ nc -vz 1.1.1.1 5000
> Connection to 1.1.1.1 port 5000 [tcp/commplex-main] succeeded!
(注:不是真实IP地址^^^)pm2
一起运行使用 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
在检查了我的
.env
,我意识到我不小心将我的服务器主机 IP 留在了开发 localhost 127.0.0.1
上而不是将其更改为公共(public)接口(interface)0.0.0.0
.切换到公共(public)界面后
0.0.0.0
,他们的行为方式相同并且连接成功。也许最好不要采用该路由,而是将 Nginx 配置为具有
stream
的 TCP 反向代理服务器。使用这个不同的 digital ocean 教程: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-server
关于node.js - 使用 PM2 将 Node 作为服务运行 - 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38277557/