How to Setup Multiple Domains in Same Directory in NGINX


multiple domains same directory nginx

Sometimes, you may want to run multiple domains that serve the same content, without using any redirects. In that case, it might be easier to simply point them to the same web root directory. Here’s how to setup multiple domains in same directory in NGINX.

 

How to Setup Multiple Domains in Same Directory in NGINX

Here are the steps to setup multiple domains in same directory in NGINX.

Before you proceed, please ensure that you have enabled virtual hosts in NGINX. Here are the steps to do it.

Let’s say you want to host 2 websites www.domain1.com and www.domain2.com on same IP.

 

1. Create Virtual Host Files

First, we create a virtual host file for any one of your domain. You can add the config for both websites in the same config file if you want. But this way it is cleaner and easier to manage.


$ sudo vim /etc/nginx/sites-available/www.domain1.com

 

2. Add Virtual Host Configuration

Add the following lines of code in the the virtual host file

 

Since you want both the domains to serve the same file, you can simply place the 2 website names one beside the other  in a single server block and use a single root directive, as shown below


server {
 listen 80
 server_name www.domain1.com www.domain2.com
 root /var/www/www.domain.com/html/domain/app; 
 location / {
   ...
 }
}

 

 

3. Enable the domains

Next, enable the virtual host


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

 

4. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

That’s it! NGINX will serve both your websites from same directory.

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!