How to serve two node.js apps on one domain? Site #2 accessible through localhost but not through url. (Nginx)

October 16, 2018 1.4k views
Nginx Node.js Ubuntu 18.04

I am attempting to run two node.js applications from the same droplet, off the same url. I am serving the first app from http://finleywill.com, and would like to serve the second app from http://finleywill.com/todo.

I am running nginx on the server.

The first app, my portfolio page, listens to port 5000.

The second app, a to-do list, listens to port 8888.

  • When I go to 67.205.148.223:8888, the app loads.
  • However, trying to connect via http://finleywill.com/todo produces a “504 Gateway Time-out”

/etc/nginx/sites-available/finleywill.com is as following:

server {
listen 80;
listen [::]:80;

    root /var/www/finleywill.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name finleywill.com www.finleywill.com;

    access_log /root/source/access.log;
    error_log /root/source/error.log;

    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;
    }

    location /todo {
            proxy_pass http://localhost:8888;
            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;
    }

}

I use PM2 to run the apps as system processes

App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │


app │ 0 │ 1.0.0 │ fork │ 27064 │ online │ 0 │ 72m │ 0% │ 55.5 MB │ root │ disabled │
app-todo │ 1 │ 1.0.0 │ fork │ 27156 │ online │ 0 │ 40m │ 0% │ 37.0 MB │ root │ disabled │

2 comments
  • Your questions seems to have been answered here.

  • Thank you for your help! I had already found an answer by the time I saw this comment, but will keep it in mind as reference. This seems to be a different solve but will no doubt help my understanding in the future.

1 Answer

I figured it out with help from a friend!

…as per https://serverfault.com/questions/650117/serving-multiple-proxy-endpoints-under-location-in-nginx

I changed /etc/nginx/sites-available/finleywill.com to the following:

server {
listen 80;
listen [::]:80;

    root /var/www/finleywill.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name finleywill.com www.finleywill.com;

    access_log /root/source/access.log;
    error_log /root/source/error.log;

    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;
    }

   (LINE BELOW HAS CHANGED)
   location ^~ /todo/ {
            proxy_pass http://localhost:8888/;
    }

}

Have another answer? Share your knowledge.