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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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:
Next, we’ll configure Nginx. Open the Nginx configuration file. You can edit the default config or create a new one:
Add the following configuration:
Replace
YOUR_APP_PORT
andYOUR_API_PORT
with the actual port numbers your Node.js applications are running on.To apply the changes, restart Nginx:
Make sure your firewall allows traffic on the necessary ports. If you’re using
ufw
, you can allow Nginx Full:Then finally, make sure your domain (
www.domain.com
) points to the IP address123.123.123.123
of your main droplet.Now, you can test your setup by navigating to:
http://www.domain.com/
should redirect tohttp://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.
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.
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:
Step 3: Ensure Proper Nginx Syntax and Reload
Step 4: Update DNS Records
www.domain.com
point to the IP address of the Nginx server if you are setting up a new server for Nginx.