Setting up an Nginx Reverse Proxy for Serving Multiple Node.js Apps from Subfolders
If you’re running multiple Node.js scripts under the same domain on a VPS like DigitalOcean, you’ll need to use a reverse proxy since two Node.js apps can’t listen on the same port. Nginx is a popular choice for setting up a reverse proxy.
To begin, you’ll need to edit the Nginx configuration file:
1 | sudo nano /etc/nginx/sites-available/default |
The default configuration file looks like this:
1 | server { |
This configuration sets up a reverse proxy for a single Node.js app running on port 3000 and serving as the main app on the root path (“/“).
If you want to run another app under the subfolder “/myservice”, you’ll need to create another Node.js app that listens on a different port (e.g., 3001) and add the following configuration:
1 | location /myservice { |
After making these changes, it’s always a good idea to check the Nginx configuration for correctness:
1 | sudo nginx -t |
If everything looks fine, you can restart Nginx for the changes to take effect:
1 | sudo systemctl restart nginx |
Using Nginx as a reverse proxy allows you to serve multiple Node.js apps from subfolders under the same domain. Keep in mind that this setup assumes you have the necessary Node.js apps running on the specified ports (e.g., 3000 and 3001).
tags: [“nginx”, “reverse proxy”, “Node.js”, “subfolder”, “VPS”, “DigitalOcean”]