Hi,

My task is to setup a Nodejs app with Express/knex and Postgres. Unfortunately the “App” way did not work for me due to some self signed SSL certificate issue and technical support after a very long ping-pong game suggested to try the Droplet with Managed Database way.

I followed instructions here:

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04

to install NginX, then also here

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

to install nodejs and run the app. Now I have a Droplet with nodejs and nginx and it is possible to connect to the managed db via psql tool if I have ssh'ed to Droplet.

Now what I see is that I have nginx running and serving /var/www/html default page it puts there, but I can’t make it serve the nodejs server running on port 3000.

I ended up with the following config:

nano /etc/nginx/conf.d/website.conf

server {
    listen 80;
    server_name api.thewebsite.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://<IP address of this droplet>:3000;
    }
}

Opening the IP in web browser shows nginx error code 502 Bad Gateway.

I am sure the nginx was restarted, it worked before the conf added and served the default html page. Also the nodejs server is running fine if I launch it manually.

What am I missing?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi,
First of all, check what address and port your app is listening on:

sudo netstat -tulpn

If your app is listening only on droplet’s public IP, ensure that you opened port TCP 3000 in a firewall.

If your app is listening on localhost (127.0.0.1) or on all droplet’s adresses (0.0.0.0/0), change your server block proxy pass directive:

proxy_pass http://localhost:3000;

In my opinion, the better approach is to have app listening on localhost rather than on public IP. In such case you do not have to open another port in firewall.

  • Hi, thank you for suggestion! I have changed the proxy_pass to localhost:3000 but that does not help. Both nginx and server were restarted with the new settings.

    I’ve noticed one thing. Subdomain name I use had DNS record of A-type to point to the IP address of the droplet. I can reach the server via IP address and it shows me the index file (standard by Nginx). If I open the mentioned subdomain, it shows 502 error Bad Gateway.

    • @pirator wrote
      I’ve noticed one thing. Subdomain name I use had DNS record of A-type to point to the IP address of the droplet. I can reach the server via IP address and it shows me the index file (standard by Nginx). If I open the mentioned subdomain, it shows 502 error Bad Gateway.

      It is probably 100% OK, unless you changed default nginx configuration. The server block you provided us serves your subdomain. If any http request, received by your nginx, does not match server name defined in server blocks, then it is served by default configuration, and it lands on default nginx welcome website, unless it was intentionally changed. You can find this configuration in /etc/nginx/nginx.conf.

      @pirator wrote
      I have changed the proxy_pass to localhost:3000 but that does not help. Both nginx and server were restarted with the new settings.

      1. Can you provide a result of

      curl -XGET http://localhost:3000
      

      2. Check nginx log for any clue

      sudo tail -100 /var/log/nginx/error.log
      
      • So the error.log contains records as:

        2020/10/21 14:48:26 [error] 2299#2299: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: api.website.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "api.website.com"
        

        and the curl -XGET http://localhost:3000 from within the server returns

        curl: (7) Failed to connect to localhost port 3000: Connection refused
        
        • What does netstat say ?

          sudo netstat -tulpn | grep -e 3000
          
          • netstat -plnt
            Active Internet connections (only servers)
            Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
            tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3316/nginx: master
            tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 571/systemd-resolve
            tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 654/sshd: /usr/sbin
            tcp6 0 0 :::80 :::* LISTEN 3316/nginx: master
            tcp6 0 0 :::22 :::* LISTEN 654/sshd: /usr/sbin
            

Hi,
We achieved maximum replies, so I had to start a new answer.

Apparently, your app does not listen on any address:port at the moment. Did you install and configure pm2 to demonize your app ? I think you need to follow the steps of this tutorial again.

by Lisa Tagliaferri
by Kathleen Juell
Node.js is an open-source JavaScript runtime environment for building server-side and networking applications. The platform runs on Linux, macOS, FreeBSD, and Windows. Though you can run Node.js applications at the command line, this tutorial will…
Submit an Answer