Question

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

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 │

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

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

}

I have configured as same as above, but the page loading

server {
    listen 443 ssl http2;
    listen [::]:443 ssl;
    include snippets/ssl-cert.conf;
    include snippets/ssl-params.conf;

    server_name dev.pm-lucida.com www.dev.pm-lucida.com;
	
    error_log   /var/log/nginx/complus.error.log;
    access_log  /var/log/nginx/complus.access.log;
 #   client_max_body_size 4M;


  location / {
    rewrite ^/(.*) /$1 break;
    proxy_ignore_client_abort on;
    proxy_pass http://13.234.214.144:3000;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header  Upgrade $http_upgrade;
    proxy_set_header  Connection "upgrade";
    client_max_body_size 4M;
  }
   location ^~/Charac/ {

        proxy_pass http://13.234.214.144:3001/;
}


}

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

    server_name dev.pm-lucida.com;

    return 302 https://$server_name$request_uri;
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel