Running Node as a service with PM2 - connection refused
Asked Answered
N

1

7

I am following this tutorial for setting up a node project on a Digital Ocean Ubuntu distribution. systemctl status pm2 shows the service is online:

App name │ id │ mode │ pid   │ status  │ restart │ uptime │ memory      │ watching
server   │ 1  │ fork │ 19999 │ online  │ 0       │ 0s     │ 21.219 MB   │ disabled │

However I get a connection refused error when I attempt to navigate to the domain. The application starts fine on port 5000 if I run it with npm start. I have Nginx installed and configured as follows:

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 version is v6.3.0 and pm2 version is 1.1.3.

Nerval answered 9/7, 2016 at 2:2 Comment(5)
have you tried running it with just node and not with pm2Leprose
Do you mean with npm start? If so, yes it works that way.Nerval
have you tried just with the domain name or have you tried the ip address of the server. Yoh could have your domain incorrectly configured.Leprose
I get a 502 Bad Gateway response.Nerval
Do you have a firewall on that ip? I would also reccomend you check your proxy server settings.Leprose
C
4

I experienced something similar:


  1. Running with node using node dist/app.js, the TCP server was accessible as expected:
$ nc -vz 1.1.1.1 5000
> Connection to 1.1.1.1 port 5000 [tcp/commplex-main] succeeded!

(Note: not the real IP address ^^^)

  1. Running with pm2 using pm2 restart dist/app.js, the TCP server refused connections:
$ nc -vz 1.1.1.1 5000
> nc: connectx to 1.1.1.1 port 5000 (tcp) failed: Connection refused

After checking my .env, I realised I had accidentally left my server host IP on the development localhost 127.0.0.1 instead of changing it to the public interface 0.0.0.0.

After switching to the public interface 0.0.0.0, they behaved the same way & connections were successful.

Perhaps instead of taking that route, it is best to configure Nginx as a reverse proxy server for TCP with stream using this different Digital Ocean tutorial: 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

Cara answered 6/8, 2020 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.