Question

Set up nginx with multiple domains and node process

As I was following this tutorial: https://www.digitalocean.com/community/questions/setting-up-multiple-nodejs-applications-using-nginx-vitual-hosts

It doesn’t seem to work, as when i completed all steps and went to the sub domain it says this site cannot be reached, but if i just go to the ip it just shows this: https://imgur.com/a/CeY6iiu


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
March 29, 2024

To set up Nginx as a reverse proxy for multiple Node.js applications, each serving a different domain, you need to configure Nginx to listen on different server blocks for each domain and then proxy the requests to the respective Node.js applications running on different ports.

Here’s a basic guide on how to achieve this:

Step 1: Start Node.js Applications on Different Ports

Let’s assume you have two Node.js applications you want to serve on domain1.com and domain2.com. Start each Node.js application on a different port. For example:

  • Node App 1 (for domain1.com) on port 3001
  • Node App 2 (for domain2.com) on port 3002

Step 2: Install Nginx

If Nginx is not already installed on your server, you can install it using your package manager:

For Ubuntu/Debian:

sudo apt update
sudo apt install nginx

For CentOS/RHEL:

sudo yum install nginx

Step 3: Configure Nginx

Create separate Nginx server blocks for each domain. This involves editing or creating files in /etc/nginx/sites-available/ and then creating a symbolic link to them in /etc/nginx/sites-enabled/.

  1. Create Nginx Server Block for Domain 1

    Create a new configuration file:

sudo nano /etc/nginx/sites-available/domain1.com

Add the following configuration:

server {
    listen 80;
    server_name domain1.com www.domain1.com;

    location / {
        proxy_pass http://localhost:3001;
        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;
    }
}
  1. Create Nginx Server Block for Domain 2

Create another configuration file:

sudo nano /etc/nginx/sites-available/domain2.com

Add a similar configuration, but change the server_name and proxy_pass:

server {
    listen 80;
    server_name domain2.com www.domain2.com;

    location / {
        proxy_pass http://localhost:3002;
        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;
    }
}

Enable the Server Blocks

Create symbolic links to enable these configurations:

sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/

Step 4: Test and Restart Nginx

  1. Test the Nginx configuration for syntax errors:
sudo nginx -t
sudo systemctl restart nginx

Step 5: DNS Configuration

Make sure the DNS records for domain1.com and domain2.com are correctly pointing to the IP address of your server where Nginx is running.

Step 6: Firewall Configuration

If you have a firewall enabled, ensure that it allows HTTP (and HTTPS, if you’re using SSL) traffic:

For Ubuntu/Debian with UFW:

sudo ufw allow 'Nginx Full'

For CentOS/RHEL:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Step 7: Optional - Set up SSL (HTTPS)

Consider securing your applications with SSL certificates. You can use Let’s Encrypt to obtain free certificates. Certbot is a tool that can automate this process for you.

Bobby Iliev
Site Moderator
Site Moderator badge
August 22, 2020

Hi there @selll,

I would recommend following the steps from this tutorial here instead:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04#set-up-nginx-as-a-reverse-proxy-server

Then once you want to start a second Node.js application keep the following things in mind:

  • Make sure that you use a different port for each application
  • Make a copy of the /etc/nginx/sites-available/default to /etc/nginx/sites-available/example2.conf and adjust the values there to match your second application
  • Then enable the second site:
  1. ln -s /etc/nginx/sites-available/example2.conf /etc/nginx/sites-example/example2.conf

Finally, restart Nginx as explained in the tutorial.

Let me know how it goes. Regards, Bobby

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