Mastering NGINX: Effortlessly Hosting Multiple Domains on a Single Server

Chiemeka Ifemegbulem
2 min readDec 26, 2023

--

Source: Google keyword search images

Configuring NGINX to serve multiple domains on a single server involves creating separate server blocks (also known as virtual hosts) for each domain. Here’s a general guide on how you can set this up:

Step 1: Domain Configuration

  1. Server Blocks: Create individual server blocks for each domain you want to host. These blocks typically reside in the /etc/nginx/sites-available/ directory.
sudo nano /etc/nginx/sites-available/domain1.com
sudo nano /etc/nginx/sites-available/domain2.com

2. Server Block Configuration: Inside each file, set up the server block configuration for each domain. An example for domain1.com:

server {
listen 80;
listen [::]:80;

server_name domain1.com www.domain1.com;

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

location / {
# Configuration for handling requests
}
}

Modify the server_name, root, and other directives as needed for each domain.

Step 2: Enable Server Blocks

  1. Symbolic Link: Create symbolic links from the sites-available directory to the sites-enabled directory.
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/

2. Configuration Test: Ensure the NGINX configuration is error-free.

sudo nginx -t

Step 3: Restart NGINX

After setting up the server blocks, restart NGINX to apply the changes.

sudo systemctl restart nginx

Step 4: DNS Configuration

Make sure the DNS records for each domain point to the server’s IP address. This can be done through your domain registrar or DNS provider.

Step 5: Files and Permissions

Ensure that the directories specified in your server blocks (/var/www/domain1.com/html) exist and have appropriate permissions to be served by NGINX.

Additional Notes:

  • SSL Configuration: If you want to use SSL/TLS for your domains, you’ll need to obtain SSL certificates (e.g., Let’s Encrypt) and configure SSL within each server block.
  • Default Server Block: You might want to set up a default server block that will handle requests for unrecognized domains or IPs.

Remember, this is a basic guide. Always back up your NGINX configuration files before making changes, and adjust configurations to fit your specific requirements.

Would you like more details on any specific part of this process?

--

--

Chiemeka Ifemegbulem

Good technical writing is the art of making complex information understandable and accessible to those who need it.