Configure Nginx to host multiple websites.

Kuldip Mori
2 min readAug 18, 2023

When it comes to hosting multiple websites under a single domain with subdomains, this article give you perfect guide with Nginx web-server.

Step 1) Creating the Directory Structure
The site data is stored in the directory structure. The internet is then made available to it. The top-level directory is called the Document root. To provide material to the internet, Nginx or a web server searches the document root.

The document root is configured to be a specific directory inside /var/www. For any website you wish to host, you may build a directory. Make a public /html folder in each of these folders. For greater flexibility, the site files themselves will be stored.

Create the /var/www folder, which will serve as the document’s root location, in this stage. Domains like mainsite.com and subsite.com are examples of subdomains.

sudo mkdir /var/www/html/mainsite.com
sudo mkdir /var/www/html/mainsite.com/subsite.com

Step 2) Grant Permission
Setting universal read and execute permissions for web content in /var/www/html.

sudo chmod -R 755 /var/www/html

Step 3) Create a test index file for each domain
To view data, we need to create a test index.html file. Make an index.html file for each valid domain for the websites.

echo "i am from mainsite" > /var/www/html/mainsite.com/index.html
echo "i am from mainsite.com/subsite.com/index.html" > /var/www/html/mainsite.com/subsite.com/index.html

Step 4) Nginx Configuration file
Two server blocks must be created for each domain ( main site & subsite ) and must update the server name and root values ( index.html path ). You can add certain domain names. Include each domain’s document root locations as well. Install nginx: click me

Create a website configuration file for the mainsite: mainsite.167.99.233.55.nip.io

server {
listen 80;

server_name mainsite.167.99.233.55.nip.io; #Need to replace With Your server IP

root /var/www/html/mainsite.com/;
index index.html;

location /subsite {
alias /var/www/html/mainsite.com/subsite.com;
index index.html;

}
}

Create a website configuration file for a subsite: subsite.167.99.233.55.nip.io

server {
listen 80;

server_name subsite.167.99.233.55.nip.io; #Need to replace With Your server IP

root /var/www/html/mainsite.com/subsite.com/;
index index.html;
}

Step 5) Create a symbolic link for the nginx conf file
We create symbolic links for the NGINX configuration files from the sites-available directory to the sites-enabled directory. This is a crucial step in Nginx’s configuration process because it allows you to enable or disable specific server blocks (virtual hosts) easily without modifying the original configuration files directly.

#Need to Replace mainsite.167.99.233.55.nip.io Nginx file to your file name
sudo ln -s /etc/nginx/sites-available/mainsite.167.99.233.55.nip.io /etc/nginx/sites-enabled/

#Need to Replace subsite.167.99.233.55.nip.io Nginx file to your file name
sudo ln -s /etc/nginx/sites-available/subsite.167.99.233.55.nip.io /etc/nginx/sites-enabled/

Step 6) Need to test & restart the Nginx
for test :

sudo nginx -t

for reload service :

sudo nginx -s reload

Step 7) Hit the URL
In mycase, (Example URL is not working while you click it’s just for Testing)

For the Main Site URL: http://mainsite.167.99.233.55.nip.io
For the Sub Site URL: http://mainsite.167.99.233.55.nip.io/subsite

--

--