node.js - 使用 PM2 将 Node 作为服务运行 - 连接被拒绝

标签 node.js ubuntu nginx digital-ocean pm2

我关注 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/

    相关文章:

    node.js - TypeScript 2.0 从排除的文件中抛出错误?

    ruby-on-rails - 由于 bundle 程序错误 : "An error occurred while installing json...",,Capistrano 无法部署,但我可以手动安装

    ubuntu - 适用于 Ubuntu 的最佳 XQuery IDE

    linux - 无法打开 X11 显示 - 如何在 WSL 上执行 SFML 输出 - 加载 WSL GUI 应用程序

    docker - 在Nginx SNI docker swarm中重新解析后端

    node.js - 将应用程序部署到 Heroku - 我遇到内部服务器错误

    node.js - Sequelize : how to count objects and update the rank?

    javascript - 如何在 twilio api 中使用分页?

    ubuntu - Gitlab综合外部网址不起作用

    node.js - Web 服务器和 Node.js?