Configure Nginx for Multiple Domains
-
Configuring Nginx to host multiple domains on a single server is done using Server Blocks (similar to "Virtual Hosts" in Apache), which define separate configurations for each domain. Nginx looks at the requested domain name (the
Hostheader) and routes the traffic to the corresponding directory.Here is a step-by-step guide to setting up two domains, which we will call domain1.com and domain2.com
Step 1 : Create Document Root Directories
First, you need to create a document root (the directory where the website files live) for each domain. It is standard practice to put these in the
/var/www/directory.Now, ceate 2 separate directories to store the website files for each domain:
sudo mkdir -p /var/www/domain1.com sudo mkdir -p /var/www/domain2.comNext, assign ownership of the directories to your regular user account (so you can edit files without needing
sudoevery time):sudo chown -R $USER:$USER /var/www/domain1.com sudo chown -R $USER:$USER /var/www/domain2.comEnsure the permissions are correct so the web server can read the files:
sudo chmod -R 755 /var/wwwStep 2 : Create Sample Pages (Optional, for Testing)
To test that everything is working later, create a simple
index.htmlfile in each directory.For domain1.com :
nano /var/www/domain1.com/index.htmlAdd this HTML:
<html> <head><title>Welcome to Domain 1!</title></head> <body><h1>Success! The domain1.com server block is working!</h1></body> </html>For domain2.com :
nano /var/www/domain2.com/index.htmlAdd this HTML:
<html> <head> <title>Welcome to Domain 2!</title> </head> <body> <h1>Success! The domain2.com server block is working!</h1> </body> </html>Step 3: Create the Server Block Configuration Files
Nginx keeps configuration files for individual sites in
/etc/nginx/sites-available/.Create the first configuration file for domain1.com :
sudo nano /etc/nginx/sites-available/domain1.comPaste the following configuration block into the file. Be sure to replace domain1.com with your actual domain name:
server { listen 80; listen [::]:80; # The directory where your website files are located root /var/www/domain1.com; # The default files to serve index index.html index.htm; # The domain names this server block will respond to server_name domain1.com www.domain1.com; location / { try_files $uri $uri/ =404; } }Save and close the file.
Now, create the second configuration file for domain2.com :
sudo nano /etc/nginx/sites-available/domain2.comAdd a similar configuration, making sure to update the
rootpath and theserver_namedirectives to match the second domain :server { listen 80; listen [::]:80; # The directory where your website files are located root /var/www/domain2.com; # The default files to serve index index.html index.htm; # The domain names this server block will respond to server_name domain2.com www.domain2.com; location / { try_files $uri $uri/ =404; } }Save and close the file.
Step 4: Enable the Server Blocks
To enable these configurations, you need to create a symbolic link (symlink) from the files in
sites-availableto thesites-enableddirectory. Nginx reads thesites-enableddirectory during startup.sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/Note: To avoid memory bucket problems that can arise from adding multiple server names, open the main Nginx configuration file:
sudo nano /etc/nginx/nginx.confFind the
server_names_hash_bucket_sizedirective and uncomment it (remove the#symbol):server_names_hash_bucket_size 64;Save and close the file.
Step 5: Test and Restart Nginx
Before restarting the web server, it is highly recommended to test your configuration files to make sure there are no syntax errors.
sudo nginx -tIf the test is successful, you will see output confirming that
syntax is okandtest is successful. If you see errors, double-check your configuration files for missing semicolons or typos.Finally, restart Nginx to apply the changes:
sudo systemctl restart nginx6. Configure DNS Records
Before you can see respective sample pages for domain1.com and domain2.com, ensure your DNS A-records for both domains are pointed to your server's public IP address.
You should now be able to visit http://domain1.com and http://domain2.com in your browser and see your respective sample pages.
Would you like me to walk you through how to secure these new domains with free SSL/HTTPS certificates using Let's Encrypt (Certbot)?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login