How to configure Multiple Domains with Nginx on Ubuntu

Configuring multiple domains in Nginx on Ubuntu

Learn how to install Nginx and configure it for multiple domains on Ubuntu.

Installing Nginx

  1. Update Apt repository cache
    sudo apt update
  2. Install Nginx
    sudo apt install -y nginx
  3. After the installation, you can check which version has been installed.
    nginx -v

    For Ubuntu 16.04 Xenial Xerus, you will see the following:

    nginx version: nginx/1.10.3 (Ubuntu)

Configure First Domain

The default configuration file for Nginx is /etc/nginx/nginx.conf, and we’re free to add our domains to this configuration. However, it is strongly recommended not do. The single, biggest reason not to combine all domains in one configuration is that it will become very unwieldy, and cumbersome to maintain. Rather, it is advised to create individual configuration files for each domain, placing them in the /etc/nginx/sites-available directory.

Our first domain will be named domain-one.com. So let’s create its configuration file.

Create the web root and configuration file

  1. Create the root directory to host our website’s files.
    sudo mkdir -p /var/www/domain-one.com/public_html
  2. Create the Nginx configuration file under /etc/nginx/sites-available. For easy reference, name the configuration file after the domain name.
    sudo touch /etc/nginx/sites-available/domain-one.com.conf
  3. Open the configuration file in a text editor. During this tutorial we will be using VI, however, Nano is a good alternative.
    sudo vi /etc/nginx/sites-available/domain-one.com.conf

Configure the first domain

Our configuration file is created. It’s time to define our domain and its individual settings.

  1. Open the configuration file in a text editor. For our example, we use VI; however, feel free to use Nano as an alternative.
    sudo vi /etc/nginx/sites-available/domain-one.com.conf
  2. We set the server directive and tell Nginx that this configuration maps to all domain requests that match domain-one.com over port 80.
    server {
         listen 80
         listen [::]:80;
         server_name domain-one.com www.domain-one.com;
    }
  3. Nginx needs to know which directory to serve content from. We’ll also want to let Nginx know which type of files to serve by default, when no file is specified. So, let’s define those settings by adding the following lines to our configuration. Place them in between the server curl braces, underneath server_name
    root /var/www/domain-one.com/public_html;
    
    index index.html index.htm;
    
  4. Finally, let’s tell Nginx to attempt to serve the request as a file. If a file doesn’t exist, attempt a directory; otherwise, show a 404 Page not found error.
    location / {
         try_files $uri $uri/ =404;
    }

    This location directive will map all requests to anything access the root of our domain. Since there are no other location directives yet, all requests will map to it.

  5. The configuration file should now look similar to the following example.
    server {
         listen 80;
         listen [::]:80;
         server_name domain-one.com www.domain-one.com;
    
         root /var/www/domain-one.com/public_html;
    
         index index.html index.htm;
    
         location / {
              try_files $uri $uri/ =404;
         }
    }
  6. Save your changes and exit the text editor.

Validate our Configuration and Start Nginx

  1. Validate the configuration file has no syntax errors. This is good practice for doing any work on a production server, as a simple syntax error will prevent the Nginx service from starting, preventing visitors from accessing your site.
    sudo nginx -t -c /etc/nginx/sites-available/domain-one.com.conf
  2. Provided no errors were found, enable the site. To do so we’ll need to create a symbolic link of the site configuration file in the sites-enabled directory.
    sudo ln -s /etc/nginx/sites-available/domain-two.com.conf /etc/nginx/sites-enabled/domain-two.com.conf
  3. Start or restart the Nginx service.
    sudo systemctl start nginx

    If Nginx is already running, reload all configuration files without stopping the service.

    sudo systemctl reload nginx
  4. Verify that Nginx is running.
    sudo systemctl status nginx

Configure Additional Domains

Our additional domains is nearly identical to adding our first one. The only difference is we’ll need to change the server’s hostname. All domains can be served from the same port, which uses 80 as a standard default.

Our second domain will be, creatively, named domain-two.com.  Let’s begin setting it up.

Create the Web Root and Configuration File

    • Create the directory to be used as our web root.
      sudo mkdir -p /var/www/domain-two.com/public_html

 

  • Create the configuration file. As with our first domain, we’ll name it after the domain name.
    sudo touch /etc/nginx/sites-available/domain-two.com.conf
  • Configure the settings exactly the same as the first domain, except change the domain name and root directory.
    server {
         listen 80;
         listen [::]:80;
         servername domain-two.com www.domain-two.com;
    
         root /var/www/domain-two.com/public_html;
    
         index index.html index.htm;
    
         location / {
              try_files $uri $uri/ =404;
         }
    }
  • Save your changes and exit the text editor.

Validate the Configuration and Start Nginx

  • Validate the configuration file, checking it for syntax errors.
    sudo nginx -t -c /etc/nginx/sites-available/domain-two.com
  • Provided no errors were found, enable the site. To do so we’ll need to create a symbolic link of the site configuration file in the sites-enabled directory.
    sudo ln -s /etc/nginx/sites-available/domain-two.com.conf /etc/nginx/sites-enabled/domain-two.com.conf
  • Start or restart Nginx.
    sudo systemctl start nginx

    If Nginx is already running, reload the configuration files.

    sudo systemctl reload nginx
  • Verify that Nginx is running.
    sudo systemctl status nginx

Test Your Domains

It isn’t enough to see the service is running to verify that everything is working as expected. As a good practice, visit your sites to ensure both are loading fine.