configure multiple host names in nginx

How To Configure Multiple Host Names in NGINX

Last updated on September 21st, 2021 at 10:46 am

Did you know you can configure multiple domains in a single NGINX instance? For that, we have to configure multiple virtual hosts in NGINX. So let’s see how to configure multiple host names in NGINX. If you host multiple websites in NGINX this way, you can save up on server costs.

 

How To Configure Multiple Host Names in NGINX

Here are the steps to configure multiple host names in NGINX. We will create 2 NGINX configuration files, one for each domain.

 

Create Web Root Directory for First Domain

Create a directory for your domain no.1, where you can host its files

sudo mkdir -p /var/www/domain-one.com/html

Create configuration file for our website #1 at /etc/nginx/sites-available

sudo vi /etc/nginx/sites-available/domain-one.conf

 

Add Server Configuration for First Domain

Add the following server block to your configuration file

server {
     listen 80;
     listen [::]:80;
     server_name domain-one.com www.domain-one.com;

     root /var/www/domain-one.com/html;

     index index.html index.htm;

     location / {
          try_files $uri $uri/ =404;
     }
}

 

Let’s look at the above configuration. The first 3 lines tell NGINX to listen to all requests that match domain-one.com over port 80.

The root directive specifies the root location of your website. Then we specify which html file to be loaded for your website’s home page (index page) using index directive.

We finally tell NGINX to handle all requests as a file; as a directory if that’s not possible, and if neither of them is possible, then throw 404 error.

 

Test NGINX configuration and restart NGINX server

Run the following command to test NGINX configuration for errors.

sudo nginx -t -c /etc/nginx/sites-available/domain-one.conf

 

Next, we create a symbolic link of the configuration file

sudo ln -s /etc/nginx/sites-available/domain-one.conf /etc/nginx/sites-enabled/domain-one.conf

 

Finally, we restart NGINX server

sudo systemctl reload nginx

 

Bonus Read: How to Redirect URL in NGINX

 

Next, we repeat all the above steps for second domain to configure multiple host names in NGINX.

Create Web Root Directory for Second Domain

Create a directory for your domain no.2, where you can host its files

sudo mkdir -p /var/www/domain-two.com/html

Create configuration file for our website #2 at /etc/nginx/sites-available

sudo vi /etc/nginx/sites-available/domain-two.conf

 

Add Server Configuration for Second Domain

Add the following server block to your configuration file

server {
     listen 80;
     listen [::]:80;
     server_name domain-two.com www.domain-two.com;

     root /var/www/domain-two.com/html;

     index index.html index.htm;

     location / {
          try_files $uri $uri/ =404;
     }
}

 

Bonus Read: How to Implement NGINX Reverse Proxy

 

Test NGINX configuration and restart NGINX server

Run the following command to test NGINX configuration for errors.

sudo nginx -t -c /etc/nginx/sites-available/domain-two.conf

 

Next, we create a symbolic link of the configuration file

sudo ln -s /etc/nginx/sites-available/domain-two.conf /etc/nginx/sites-enabled/domain-two.conf

 

Finally, we restart NGINX server to configure multiple host names in NGINX.

sudo systemctl reload nginx

 

Alternatively, you can also add the 2 server blocks to NGINX’s default configuration file at /etc/nginx/nginx.conf if you want to configure multiple host names in NGINX. However, it is advisable to create separate copies for better security & management, if you want to host multiple websites on NGINX.

Now NGINX will listen to requests directed to both host names (domain-one.com and domain.two.com). Open a web browser and go to your domains to ensure that they are loading properly. That’s it! You can use the same steps to configure multiple subdomains on a single NGINX server.

By the way, if you want to create charts & dashboards to monitor your website or business, you can try Ubiq. We offer a 14-day free trial.