How to Configure Multiple Host Names in NGINX

NGINX is a popular web server used by millions of high traffic websites as well as blogs. It is very versatile since it can be used as a reverse proxy or a load balancer. Often system administrators need to run multiple websites on NGINX server. For this run each website on a different physical/virtual server, which can be expensive and difficult to manage. Did you know that you can run multiple websites on a single NGINX server instance? In this article, we will learn how to configure multiple host names in NGINX.

Why Configure Multiple Host Names in a Server

There are significant benefits of setting up multiple hosts in NGINX.

  1. Cost Effective – NGINX allows you to serve multiple domains, subdomains and websites from a single domain, instead of using separate servers for each of them. This can save a lot of server costs
  2. Easy to Manage – It is easier to manage multiple domains and subdomains from a single server instead of using separate virtual servers or hosting accounts.
  3. Resource Sharing – If you run several low-medium traffic sites and blogs, then it makes sense to run them all on a single server instance, where they can share the memory, CPU and disk space more efficiently. NGINX will make sure that they do not interfere with each other’s resources.
  4. Simpler Routing – Based on the value of requested request header, NGINX will appropriately route the request to proper domain or subdomain.
  5. Scalable – As you need to host new domains or subdomains, you can easily add them to your existing server by simply modifying NGINX configuration. No need to buy more infrastructure. It is a centralized yet scalable set up.
  6. Better Organized – Even if all your web content is located on a single server, you can configure different host names (e.g. blog, API, app, etc.) to serve content from each of these locations. This keeps your work neatly organized.

How to Configure Multiple Host Names in NGINX

There are a couple of different ways to set up multiple host names in NGINX. In the first approach, we will learn how to host completely different websites located on same physical infrastructure, using a single server. In this case, each host serves different requests and content.

In the second case, we simply configure multiple domains and subdomains to point to the same website folder. In this case, all these sites serve same request paths and content.

1. Configure Multiple Websites in NGINX

In this examples, we we will setup NGINX to serve to completely different websites site1.com and site2.com.

Create Document Root Directories

First, we will create separate document root directories for each website. Make sure your NGINX user has permission to access the contents of these folders.

sudo mkdir -p /var/www/site1.com/html
sudo mkdir -p /var/www/site2.com/html

Create Test Pages

We will create a couple of simple test pages that we will request while testing these sites. For this, we will create index.html page in each document root, along with some simple text to be displayed on page load.

echo 'hello site1' | sudo tee /var/www/site1.com/index.html
echo 'hello site2' | sudo tee /var/www/site2.com/index.html

Create Virtual Host Files

Next, we will create separate virtual host configuration files for each website. Here’s an example to create virtual host file for site1.com.

sudo vi /etc/nginx/sites-available/site1.com.conf

Add the following server block in this virtual host file. Customize it as per your requirement.

server {
listen 80;
server_name site1.com;
root /var/www/site1.com/html;
index index.html;

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

The above code tells NGINX that this virtual host file listens to port 80 and serves requests sent to site1.com domain. Its document root is also specified.

Save and close the file. Repeat this step for site2.com. Create virtual host config file for site2.com.

sudo vi /etc/nginx/sites-available/site1.com.conf

Add the following code to this file.

server {
listen 80;
server_name site2.com;
root /var/www/site2.com/html;
index index.html;

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

The above code tells NGINX that this virtual host file listens to port 80 and serves requests sent to site2.com domain. Save and close this file too.

Enable Virtual Hosts

Enable the above 2 virtual hosts by creating symbolic links to them in /etc/nginx/sites-enabled folder.

sudo ln -s /etc/nginx/sites-available/site1.com.conf /etc/nginx/sites-enabled/site1.com.conf

sudo ln -s /etc/nginx/sites-available/site2.com.conf /etc/nginx/sites-enabled/site2.com.conf

Now the actual files reside in /etc/nginx/sites-available and symbolic links exist in /etc/nginx/sites-enabled directory. You may need to remove default_server file from /etc/nginx/sites-enabled directory, to avoid any routing conflicts.

Reload NGINX

Reload NGINX to apply changes.

sudo systemctl reload nginx

Test Websites

Now you have two virtual hosts listening to port 80. When you request a page from site1.com, NGINX will route the request to virtual host with site1.com in its server_name directive. That is site1.com.conf. This virtual host will process the request and send back the response. Similar routing happens in case of request to site2.com.

Open web browser and go to http://site1.com/index.html and http://site2.com/index.html. You should be able to see the contents of their respective index.html pages.

2. Configure Multiple Domains to Same Website

Sometimes, you may want to point one or more domains/sub domains to a single website. In other words, you may want to serve the same content for example1.com/product as well as example2.com/product. In such cases, you can directly use server_name directive to point different domains to same website folder. You do not need to set up separate virtual hosts for this purpose.

Open NGINX configuration file and add the following line to the server blocks listening to ports 80 and 443.

server_name example1.com example2.com;

Here is an example.

server {
   listen 80;
   ...
   server_name example1.com example2.com;
   ...
}

Now, whether you request example1.com or example2.com, you will see the same content on your web browser. You can also use this method to point different sub domains to same website.

server {
listen 80;
...
server_name blog.example.com articles.example.com;
...
}

Save and close the file. Restart NGINX to apply changes.

Conclusion

In this article, we have learnt how to configure multiple domains on a single NGINX server. We have learnt about the benefits of doing so. We have also learnt how to set up multiple host names on NGINX server, in case they serve different content, that is, each domain points to a different website altogether. We have also learnt how to configure NGINX to serve same content for different domain names. You can use the above steps as per your requirement.

Also read:
How to Increase File Upload Size in NGINX
How to Block Image Hotlinking But Allow Google
How to Remove Trailing Slash in NGINX

Leave a Reply

Your email address will not be published. Required fields are marked *