Do you have a server with a single public IP address, but need to host multiple domains or subdomains? What would you do? This scenario can be confusing if you’re a beginner. But don’t worry, this tutorial has you covered, so one NGINX subdomain and multiple domains can serve.
In this tutorial, you will learn how to effectively serve NGINX subdomains or multiple domains on a server with a single IP address.
If you’re ready, it’s time to get down to business!
tutorial comes with hands-on demos, but it doesn’t require many prerequisites and will assume you have an Ubuntu Server LTS with SSH enabled and NGINX installed. The demos in this tutorial are on Ubuntu Server LTS 20.04.1.
Configuring DNS records
Before you can serve NGINX subdomains or multiple domains, you must add an A record in a DNS control panel. The A record links and points all domains and subdomains to a single IP address to allow web browsers to find your website.
Related:How to use PowerShell for DNS 1 records
. Launch your favorite web browser and log in to your DNS control panel.
2. Click Add Record to start adding A records. Your browser redirects to a page where you will configure DNS settings for your domain and subdomains.
Now configure the DNS settings with the following:
in the Type field.
Enter @ in the Host field and the server IP in the Value field
Select the desired value in the TTL field, then click the Confirm button to save the
Repeat the same process for other subdomains.
The final DNS configuration looks like the following.
Your DNS records control panel may vary in functionality and design, but the same principles will apply to everyone
. Configuring web directories for
NGINX domain and subdomain
Now that you’ve added A records for your domain and subdomains, it’s time to set up your web directories. NGINX comes with a default virtual host file and is configured to serve a web directory located at /usr/share/nginx/html.
You will create a separate web directory for each domain within the default NGINX document root (/var/www/html).
1. First, open your terminal and run the following commands to create web directories for all domains and subdomains. In doing so, the files on each website are separated, organized, and isolated.
Arabic numeral. Then, run the chown command to recursively change (-R) the ownership of each directory that you created in step one to www-data user and group. You are changing the ownership of each directory to www-data user and group since NGINX runs as a www-data user.
3. Create a file named index.html in your preferred code editor, and then copy and paste the following code into the file .html index. Save the file to the home web directory of your domains (/var/www/html/awstutorial.net).
The following HTML displays a message that says “Congratulations! The website is awstutorial.net working!” when the file .html index is opened in a web browser.
4. Finally, create the same index.html in the /var/www/html/web1.awstutorial.net and /var/www/html/web2.awstutorial.net directories. But replace awstutorial.net with web1.awstutorial.net and web2.awstutorial.net in the code of each index.html accordingly.
Virtual host configuration
for NGINX domains and subdomains
You already have a page .html index for your domain and subdomains to serve through an NGINX web server. The next step is to create an NGINX virtual host configuration file for each domain to serve the HTML pages.
Related: How to Test Your NGINX Setup Before You Rumble It 1
. Create an NGINX virtual host configuration file named awstutorial.net in your preferred code editor, and then copy/paste the following code into that file. Save the file in the /etc/nginx/sites-available/ directory.
The following code controls server behavior, such as the server name and index (home) page when a user tries to access your domain
2. Then run the following nginx command to check (-t) the NGINX configuration file for any syntax errors.
If no syntax error is found in the NGINX configuration file, you will get the following output
3. Then run the following command to create a symbolic link (ln -s) from the /etc/nginx/sites-available directory to the /etc/nginx/sites-enabled/ directory. This command enables the virtual host configuration file awstutorial.net.
The format of available and site-enabled sites is standard within an Ubuntu NGINX installation, but other distributions may use a different standard
4. Repeat steps one through three to create NGINX virtual host configuration files named web1.awstutorial.net and web2.awstutorial.net.
Change the following lines in each NGINX virtual host configuration file:
Replace the line root /var/www/html/awstutorial.net with the webroot directory of each subdomain (root /var/www/html/web1.awstutorial.net and
- root /var/www/html/web2.awstutorial.net). Replace the line server_name awstutorial.net with the name of each subdomain (
- server_name web1.awstutorial.net and server_name web2.awstutorial.net).
5. Now run the sudo ln commands below as you did in step three to enable the virtual host configuration files.
6. Run the following systemctl command to restart nginx to apply all configuration changes.
7. Finally, navigate to the domain URLs and subdomains in your browser to test whether the websites are working well.
Related:How to Use Python to Load Test Websites
If the domain and subdomains
load, you’ll see a message like the following
. Configuring HTTPS on the NGINX domain and subdomain You have successfully set up and tested an
NGINX domain and
subdomains on the HTTP protocol, which is great. But you need to secure your domain and subdomains’ connection by enabling HTTPS. How? With a certificate you’ll get from a certificate authority like Let’s Encrypt SSL.
1. First, run the following command to install the Certbot software package (apt-get install certbot). Certbot allows you to download an SSL certificate for your domain and subdomains.
Arabic numeral. Then run the certbot command below to download an SSL certificate (certonly) for your domain (-d awstutorial.net). Please note that you accept the term of service (—accepts) with your email address (—email).
Replace the email with yours
3. Enter ‘1’ to select the NGINX Web Server (nginx) plug-in to authenticate with the ACME CA, as shown below.
After selecting the NGINX Web Server plug-in, you will see the SSL certificate download progress for your domain (awstutorial.net).
By default, Let’s Encrypt SSL certificates are stored in the /etc/letsencrypt/live/ directory.
4. Now run each command below as you did in step two to download SSL certificates for the remaining subdomains (web1.awstutorial.net and web2.awstutorial.net).
5. Finally, run the ls command below to list all SSL certificates in the /etc/letsencrypt/live/ directory that contain (*) the awstutorial.net in their names. If you do, you can verify that SSL certificates exist.
Next, you’ll see the SSL certificates for your
domain and subdomains
Configuring the NGINX Virtual Host to Use
At this point, you already have SSL certificates at your disposal. But how do you use them to protect your domain and subdomains? You will define the path of the certificates in the server block of each NGINX virtual host configuration file.
1. Open the NGINX virtual host configuration file awstutorial.net located in the /etc/nginx/sites-available/ directory in your preferred code editor.
2. Replace the contents of the file with the following code, where you define the path of your domain’s SSL certificate and the SSL protocol (under SSL certificate path).
3. Repeat the same process (steps one by two) for the remaining virtual host configuration files (web1.awstutorial.net and web2.awstutorial.net). But be sure to replace the SSL certificate path with the certificate path of your subdomain (under SSL certificate path).
4. Now run the systemctl command again below to restart the NGINX service to apply the configuration changes.
5. Finally, navigate to your domain and subdomain URLs in your web browser. But this time, instead of the HTTP protocol, use HTTPS to see if they work.
You can then see a lock icon in the address bar, indicating that the website is secure with its SSL certificate
In this tutorial, you learned how to serve an NGINX subdomain or multiple domains by configuring a virtual host configuration file. You have also touched to protect your domains with an SSL certificate that you also define in the configuration file of the virtual host.
Now, why not use this setup in a production environment, such as hosting multiple applications on a single server and providing affordable web hosting?