Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Node as a service with PM2 - connection refused

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.

like image 420
rurp Avatar asked Jul 09 '16 02:07

rurp


1 Answers

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

like image 54
LordParsley Avatar answered Sep 30 '22 21:09

LordParsley