Question

Using Nginx to host multiple Node.js applications

server {
    listen 80;

    server_name greatwallprojects.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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 /pingtest/  {
            proxy_pass http://127.0.0.1:8081;
            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;
        }
    }

The second application pingtest is not displayed. My application folders lie in home/user/

You can check the problem by visiting http://greatwallprojects.com/pingtest/ a blank page is displayed.

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.

KFSys
Site Moderator
Site Moderator badge
July 18, 2023

Heya,

First, it’s important to ensure that both your Node.js applications are running correctly on their respective ports (8080 for the main application, and 8081 for the /pingtest/ application).

Once you have confirmed that, let’s check your NGINX configuration. From what you’ve shared, it seems like the configuration is generally correct. However, when using the location directive to serve a Node.js app from a subdirectory (in your case /pingtest/), there can be issues related to the way paths are handled.

By default, when you use proxy_pass with the URL path specified (http://127.0.0.1:8081), Nginx will pass the exact same request URI to the upstream server. So, when you’re visiting http://greatwallprojects.com/pingtest/, Nginx is trying to serve http://127.0.0.1:8081/pingtest/, which might not be correctly handled by your Node.js app.

To solve this, you can use a URI in the proxy_pass directive. This will cause Nginx to replace the part of the request URI that matches the location path with the path specified in the proxy_pass directive.

Here’s how you can modify your configuration:

location /pingtest/  {
    rewrite ^/pingtest/(.*) /$1 break;
    proxy_pass http://127.0.0.1:8081;
    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;
}

This configuration uses the rewrite directive to rewrite the request URI, stripping out the /pingtest/ part, before passing the request to your Node.js application.

After making these changes, don’t forget to test your configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Please note that your Node.js application should be set up to correctly handle requests based on this configuration. If the application is expecting requests to be at /pingtest/, you will need to adjust its routing accordingly.

This comment has been deleted

    This comment has been deleted

      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