How To Configure Nginx Server Block For Multiple Domains Hosting

Technology Reads Mar 14, 2020

Excerpt

If you are setting up multiple domains within a single Linux server in the cloud; then, it's easy to do with the Nginx Server Block directive. Here is how.

Existing Set up Required

  1. Virtual server with any one of your favorite Linux distribution installed.
  2. Make sure initial server setups completed — from enabling the UFW firewall and the creation of regular users, etc.
  3. Nginx installed and necessary configuration completed — including profile registration with the UFW firewall for HTTP and SSL access.

Introduction

Nginx is a highly customizable and robust web server, which we can use to host multiple domains using Server Block.

Server Block directive is the way inside Nginx to separate multiple domain-specific configurations. With that, each file marked for a specific domain and the incoming traffic processed accordingly via port 80, or 443 depends on the settings.

Default configuration

With the initial installation, Nginx has the default Server Block configured to process all HTTP request coming into the server. And this request is handled with the response using an index.html file located inside the root directory.

You can find both the configuration file in this respective directory path.

How To Configure Nginx Server Block For Multiple Domains Hosting - renttheant.com

Nginx  default Server Block

/etc/nginx/sites-available/default

Index.html

/var/www/html/index.html

Steps to configure multiple domains

So, from looking at the default configuration. We have to make the some configuration adjustment at two-level —

First, with differentiated configuration to handle the inbound request through multiple Server Block directives.

Second, to make sure that Nginx is going to process the response from the designated web application directory per each domain.

Understanding Nginx file directory

First, understand the Nginx file directory structure and the relevant folders within /etc/nginx/. It has two folders available as sites-available and sites-enabled.

What are the function of these two folders?

As the name of the folders suggests, you can configure and make it available as many files as you want in the site-available directory. But you can choose to enable only the latest version or domains required from site-enabled.

To do that, we have to establish a file specific symbolic link using the following command.

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

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

With this set up in place, we have configured the server to process the incoming request based on three different sets off Server Block configuration.

../site-enabled/myfirstdomain.com 

Will process the request coming from myfirstdomain.com

../site-enabled/myseconddomain.com

Will process the request coming from myseconddomain.com

../site-enabled/default

Will process any request not matching the Server Block configuration from those above two.

Server Block is a kind of sequence processing request from specific to the general funnel of routing.

First part

Server Block update

When connected with the remote server via SFTP Client and able to navigate inside the server file directory, then proceed with download this file into local machine.

Then, simply copy that file to duplicate for each domain. And rename the file with the domain name like below.

/etc/nginx/sites-available/myfirstdomain.com
/etc/nginx/sites-available/myseconddomain.com

Now, we have the default configuration copied into domain-specific files. That needs an update to instruct Nginx to process the request differently.

To do that, we have to make the number of changes inside those file content.

Without the comments and other instructions, the default file looks like this.

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name _;

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

This file has one Server Block - server { ... }. Within that, four different Nginx directive is configured, which is to listen, root, server_name, and location.

This configuration is instructing the Nginx to process the incoming request from Port - 80, Process response from /var/www/, process all domain request.

Now, we have to modify the file.

  1. Remove status as "default_server" in the domain files.
  2. Process response from different root directory
root /var/www/myfirstdomain.com/html

root /var/www/myseconddomain.com/html

Third, server_name to reflect the individual file specific domain name, like below.

server_name  myfirstdomain.com  www.myfirstdomain.com
server_name  myseconddomain.com  www.myseconddomain.com

Note

In the next step of this configuration, we will cover how to set up a different root directory to process different responses.

In the end, update configuration files will look like this.

Path - /etc/nginx/sites-available/myfirstdomain.com
server { listen 80; listen [::]:80; root /var/www/myfirstdomain/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }

Path - /etc/nginx/sites-available/myseconddomain.com
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/myseconddomain/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }

Then, you need to update the location directive inside the server block files. And indicate where to look for data to use and serve up the response for the incoming domain's request.

Using CLI to update this

In case if you want to execute these activities through a command-line interface. Then using these following commands, you can perform both copies and modify files.

$ sudo cp /etc/nginx/site-available/default     /etc/nginx/myfirstdomain.com
$ sudo cp /etc/nginx/default      /etc/nginx/myseconddomain.com

Now, if you look at the folder directory now - /etc/nginx/sites-available, you must see three different files.

Next, you can address individual file modification separately using the following command.

$ sudo nano /etc/nginx/sitesavailable/myfirstdomain.com

Now, you will see this file information printed in the terminal UI

Perform the steps described above in each file.

Second Part

Set up Multiple Web application directory and Index HTML file to serve.

With the above Nginx configuration, we are ready to process the incoming request through a different route. Now, we need to have two separate pages mentioned in the configuration files.

Create them manually

When connected with the remote server via SFTP client. Then navigate to /var/html/www. And modify this directory to have a separate folder under /var/www/ for each domain.

Add the directory like this below.

/var/www/myfirstdomain/html
/var/www/myfirstdomain/html

With this directory set up is completed. Now you can add two new index.html pages inside the respective domain directory.

To differentiate the Nginx processing, Make sure to have different HTML content inside them.

Path - /var/www/myfirstdomain/html/index.html

<html>
    <head>
        <title>Hello World! - MyFirstDomain.com</title>
    </head>
    <body>
        <h1>I am processed through MyFirstDomain Server Block! Eureaka!!!</h1>
    </body>
</html>
Path - /var/www/myseconddomain/html/index.html

<html>
    <head>
        <title>Hello World! - MySecondDomain.com</title>
    </head>
    <body>
        <h1>I am processed through MySecondDomain Server Block! Eureaka!!!</h1>
    </body>
</html>

Using CLI to update this

First, create a directory.

Using the following command, you can create two new directories under /var/www.

$ sudo mkdir -p /var/www/myfirstdomain/html$ sudo mkdir -p /var/www/myseconddomain/html

Now, tackle the directory access.

Assuming that you are accessing the server via a regular user account, then this directory permission needs to be updated for the current user. This permission will allow us to modify the content without the sudo command.

To do that, use the $USER variable.

$ sudo chown -R $USER:$USER /var/www/myfirstdomain/html$ sudo chown -R $USER:$USER /var/www/myseconddomain/html
$ sudo chmod -R 755 /var/www

With that, the root directory configured with the necessary permission to proceed with index files creation.

Create the index.html for the first domain

$  nano /var/www/example.com/html/index.html

Now, open the file in the editor mode of cli. Use this following command.

$ nano /var/www/test.com/html/index.html

Make the necessary update to this file content to reflect some domain-specific information. Then, save the file.

Repeat these steps for the second domain as well. With that, we are end of the configuration setup and ready to test and validate.

Nginx Config file update

Since we are configuring multiple server names, the system may encounter hash memory problem, to avoid this update this single line of code in the Nginx config file.

Using administrative privilege, open this file in the editor.

$ sudo nano /etc/nginx/nginx.conf 

Navigate to the line configured with directive "server_names_hash_bucket_size." And update that default value with 64.

HTTP {
   .
   .
  
   server_names_hash_bucket_size 64;
   .
   .
 }

Now, you can test and restart the Nginx to validate multiple domain access.

$ sudo Nginx -t
$ sudo systemctl restart nginx

Each domain access must now serve the respective HTML pages from the root directory.

Inner Voice

I am "Rent The Ant" Inner Voice. The collective voice of our team both in-house and partners.