nginx
Ah, now you want Nginx to serve multiple websites (virtual hosts) from the same server — each with its own domain name and directory. This is called name-based virtual hosting. Here’s how to do it step by step.
1️⃣ Directory structure
Section titled “1️⃣ Directory structure”Suppose you have 2 sites:
| Domain | Root folder |
|---|---|
site1.example.com | /var/www/site1 |
site2.example.com | /var/www/site2 |
Create the directories:
sudo mkdir -p /var/www/site1sudo mkdir -p /var/www/site2Add a test file in each:
echo "Hello from Site 1" | sudo tee /var/www/site1/index.htmlecho "Hello from Site 2" | sudo tee /var/www/site2/index.htmlSet ownership and permissions:
sudo chown -R www-data:www-data /var/www/site1 /var/www/site2sudo chmod -R 755 /var/www/site1 /var/www/site22️⃣ Create server blocks for each site
Section titled “2️⃣ Create server blocks for each site”Site 1
Section titled “Site 1”sudo nano /etc/nginx/sites-available/site1.confserver { listen 80; listen [::]:80; server_name site1.example.com;
root /var/www/site1; index index.html index.htm;
location / { try_files $uri $uri/ =404; }}Site 2
Section titled “Site 2”sudo nano /etc/nginx/sites-available/site2.confserver { listen 80; listen [::]:80; server_name site2.example.com;
root /var/www/site2; index index.html index.htm;
location / { try_files $uri $uri/ =404; }}3️⃣ Enable the sites
Section titled “3️⃣ Enable the sites”sudo ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/sudo ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/Test Nginx config:
sudo nginx -tsudo systemctl reload nginx4️⃣ Update DNS
Section titled “4️⃣ Update DNS”Both domains must point to your server’s IP:
site1.example.com → <server-IP>site2.example.com → <server-IP>
5️⃣ Optional: HTTPS with Let’s Encrypt
Section titled “5️⃣ Optional: HTTPS with Let’s Encrypt”sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d site1.example.com -d site2.example.comCertbot can automatically configure HTTPS for multiple sites.
6️⃣ Optional: Serve multiple directories on different ports
Section titled “6️⃣ Optional: Serve multiple directories on different ports”You can also assign different ports:
server { listen 82; server_name site1.example.com; root /var/www/site1; index index.html;}
server { listen 83; server_name site2.example.com; root /var/www/site2; index index.html;}But in practice, name-based hosting on port 80/443 is preferred, because clients don’t need to type the port.
✅ Notes
Section titled “✅ Notes”- Each
server_namegets its own root folder. - You can combine with reverse proxy to point some domains to backend apps.
- Nginx automatically picks the server block based on the
Hostheader.
If you want, I can create a ready-to-copy Nginx configuration for 3+ domains with both static files and reverse proxy backends, all HTTPS-enabled, so you don’t have to modify anything manually.
Do you want me to do that?
Perfect! Let’s create a ready-to-use Nginx configuration that handles multiple domains, each with its own directory or backend, and HTTPS enabled.
I’ll assume:
-
3 domains for example:
site1.evolo.fr→ static files in/var/www/site1site2.evolo.fr→ static files in/var/www/site2site3.evolo.fr→ backend app (Node.js/Python) on127.0.0.1:3000
-
You want automatic HTTPS via Let’s Encrypt
1️⃣ Directory setup
Section titled “1️⃣ Directory setup”sudo mkdir -p /var/www/site1 /var/www/site2sudo chown -R www-data:www-data /var/www/site1 /var/www/site2sudo chmod -R 755 /var/www/site1 /var/www/site2
# Add test pagesecho "Hello from Site 1" | sudo tee /var/www/site1/index.htmlecho "Hello from Site 2" | sudo tee /var/www/site2/index.html2️⃣ Nginx config
Section titled “2️⃣ Nginx config”Create a single config file for all 3 sites:
sudo nano /etc/nginx/sites-available/multi-sites.confPaste:
# ------------------------# Site 1: Static# ------------------------server { listen 80; listen [::]:80; server_name site1.evolo.fr;
root /var/www/site1; index index.html index.htm;
location / { try_files $uri $uri/ =404; }}
# ------------------------# Site 2: Static# ------------------------server { listen 80; listen [::]:80; server_name site2.evolo.fr;
root /var/www/site2; index index.html index.htm;
location / { try_files $uri $uri/ =404; }}
# ------------------------# Site 3: Backend app# ------------------------server { listen 80; listen [::]:80; server_name site3.evolo.fr;
location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; }}3️⃣ Enable the config
Section titled “3️⃣ Enable the config”sudo ln -s /etc/nginx/sites-available/multi-sites.conf /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl reload nginx4️⃣ Obtain HTTPS via Let’s Encrypt
Section titled “4️⃣ Obtain HTTPS via Let’s Encrypt”sudo apt install certbot python3-certbot-nginx
# For all 3 domains at oncesudo certbot --nginx -d site1.evolo.fr -d site2.evolo.fr -d site3.evolo.frCertbot will:
- Create HTTPS server blocks
- Redirect HTTP → HTTPS automatically
- Configure SSL certificates
5️⃣ Optional: Force HTTPS manually
Section titled “5️⃣ Optional: Force HTTPS manually”If needed, add this server block before the others:
server { listen 80; listen [::]:80; server_name site1.evolo.fr site2.evolo.fr site3.evolo.fr; return 301 https://$host$request_uri;}✅ Advantages of this setup
Section titled “✅ Advantages of this setup”- Each domain has its own folder or backend
- HTTPS enabled automatically
- Works for static sites or reverse-proxied apps
- Easy to add more sites: just create a new
server {}block
I can also make a fully ready template for 5+ domains with both static and dynamic backends, including directory listing disabled, caching, and compression, so it’s production-ready.
Do you want me to do that?