By Dienstbereit
I have two droplets with different Node JS applications that should be accessible under one domain (no subdomain):
1. Droplet IP 123.123.123.123:
www.domain.com/ -> redirect -> www.domain.com/app/
www.domain.com/app/...
2. Droplet IP 321.321.321.321:
www.domain.com/api/
www.domain.com/api/...
Droplet/s: Ubuntu 20.04, NGINX 1.17.10
Would this be possible in principle and if so, what would I have to configure where?
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!
Accepted Answer
Hey!
Great question! Yes, it’s totally possible to have two different Node.js applications on separate Droplets under one domain without using subdomains. You can achieve this using Nginx as a reverse proxy.
You can take a look at this article here for more information:
First, let’s assume you’re using the Droplet with IP 123.123.123.123
as your main Droplet. This droplet will handle all incoming requests and forward them to the appropriate Dackend droplet based on the URL path.
Make sure you have Nginx installed on your main droplet. You can install it using:
sudo apt update
sudo apt install nginx
Next, we’ll configure Nginx. Open the Nginx configuration file. You can edit the default config or create a new one:
sudo nano /etc/nginx/sites-available/default
Add the following configuration:
server {
listen 80;
server_name www.domain.com;
location / {
return 301 /app/;
}
location /app/ {
proxy_pass http://123.123.123.123:YOUR_APP_PORT/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/ {
proxy_pass http://321.321.321.321:YOUR_API_PORT/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Replace YOUR_APP_PORT
and YOUR_API_PORT
with the actual port numbers your Node.js applications are running on.
To apply the changes, restart Nginx:
sudo systemctl restart nginx
Make sure your firewall allows traffic on the necessary ports. If you’re using ufw
, you can allow Nginx Full:
sudo ufw allow 'Nginx Full'
Then finally, make sure your domain (www.domain.com
) points to the IP address 123.123.123.123
of your main droplet.
Now, you can test your setup by navigating to:
http://www.domain.com/
should redirect to http://www.domain.com/app/
http://www.domain.com/api/
should serve content from the second dropletAnd that’s it! Your two Node.js applications should now be accessible under one domain. If you have any further questions, feel free to ask!
Best,
- Bobby
Heya,
Yes, it is entirely possible to configure a single domain to route requests to different backends (in your case, two separate droplets running different Node.js applications) based on the URL path. This is commonly done using a reverse proxy. Nginx is a popular choice for this purpose because of its efficiency and flexibility in handling such configurations.
Ideally, the Nginx server that acts as a reverse proxy should not run on the same servers as your applications. This separates concerns, allowing each server to be optimized and secured according to its role. You may choose to set up Nginx on one of the existing droplets or on a new one.
You’ll configure Nginx to proxy requests to the appropriate droplet based on the incoming URL path. Below is a sample Nginx configuration that demonstrates how to do this. This configuration assumes you have a third droplet or a separate server where Nginx is installed and configured to handle proxy tasks.
Edit the Nginx configuration file. This file is typically located at /etc/nginx/nginx.conf
or /etc/nginx/sites-available/default
. If you are using a custom setup, the location might differ.
Insert the following server block into the configuration:
server {
listen 80;
server_name www.domain.com;
location /app/ {
proxy_pass http://123.123.123.123/app/; # Ensure this ends with a slash
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/ {
proxy_pass http://321.321.321.321/api/; # Ensure this ends with a slash
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Redirect from root to /app/
location = / {
return 301 $scheme://$host/app/;
}
}
sudo nginx -t
sudo systemctl reload nginx
www.domain.com
point to the IP address of the Nginx server if you are setting up a new server for Nginx.Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.