How to Configure Multiple Domains in NGINX


configure multiple domains in nginx

If you are running multiple websites, it might be a good idea to run them on same server. Here’s how to configure multiple domains in NGINX.

 

How to Configure Multiple Domains in NGINX

Here are the steps to configure multiple domains in NGINX.

 

1. Open NGINX config file

Open NGINX config file in a text editor. You will typically find it at /etc/nginx/nginx.conf


$ sudo vim /etc/nginx/nginx.conf

 

2. Allow multiple domains in NGINX

Let’s say you want to configure multiple domains (domain1.com and domain2.com), then simply add the following code in your config file.


server {
 listen 80;
 server_name www.domain1.com;
 root /var/www/domain1;
}

server {
 listen 80;
 server_name www.domain2.com;
 root /var/www/domain2;
}

Basically, you need to create a separate server block for each domain, and distinguish each block using the server_name directive. Each server block’s root directive must point to its appropriate root directory.

 

Within each server block, you can add multiple location blocks, depending on each website.

 

Similarly, if you want to setup multiple subdomains in the same NGINX server, you can change the server names as shown below


server {
 listen 80;
 server_name blog.domain1.com;
 root /var/www/domain1;
}

server {
 listen 80;
 server_name app.domain2.com;
 root /var/www/domain2;
}

 

 

3. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

 

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!