How to configure Multiple Domains with Nginx on Ubuntu
Posted in how to on April 23, 2020 ‐ 4 min read
Learn how to install Nginx and configure it for multiple domains on Ubuntu.
This article assumes Nginx is already installed.
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
Create the root directory to host our website’s files.
sudo mkdir -p /var/www/domain-one.com/public_html
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
Open the configuration file in a text editor. During this tutorial we will be using nano, however, VI is a good alternative.
sudo vi /etc/nginx/sites-available/domain-one.com.conf
Config File for First Domain
Our configuration file is created. It’s time to define our domain and its individual settings.
Open the configuration file in a text editor. For our example, we use nano; however, feel free to use VI as an alternative.
sudo vi /etc/nginx/sites-available/domain-one.com.conf
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; }
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;
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.
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; } }
Save your changes and exit the text editor.
Validate our Configuration and Start Nginx
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
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 the Nginx service.
sudo systemctl restart nginx
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; server_name 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
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.