Question

Two droplets one domain (no subdomain!)

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?


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.

Bobby Iliev
Site Moderator
Site Moderator badge
June 6, 2024
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:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22-04

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 droplet

And 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

KFSys
Site Moderator
Site Moderator badge
June 7, 2024

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.

Step 1: Set Up Nginx on a Separate Server

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.

Step 2: Configure Nginx

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.

  1. 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.

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

Step 3: Ensure Proper Nginx Syntax and Reload

  • After editing the configuration, check for syntax errors:
sudo nginx -t
  • If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx

Step 4: Update DNS Records

  • Ensure that the DNS records for www.domain.com point to the IP address of the Nginx server if you are setting up a new server for Nginx.

Try DigitalOcean for free

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

Sign up

Featured on Community

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