Question

Setting up a minimal Nodejs app + Postgres in a Droplet

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?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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.

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.