How to Add a Second Domain to Your VPS with Nginx Server Blocks
Most VPS owners start with one site, then sooner or later a second project needs a home. The good news is that one VPS can serve many sites, each with its own domain, its own folder, and its own certificate. This guide shows you how to add a second domain to your VPS with Nginx server blocks, from DNS to HTTPS, without touching the first site.
A server block is the Nginx name for a virtual host: a config file that tells Nginx which folder to serve when a request arrives for a given domain name. You already have one for your first site. You will add a second one, point the new domain at your server IP, and let Certbot handle the certificate. The whole job takes about fifteen minutes once DNS has spread.
What You Need Before You Start
You need a VPS with Nginx already installed and serving at least one site, sudo access, and a registered second domain with access to its DNS settings at your registrar. You also need to know where your first site lives: its config file in /etc/nginx/sites-available/ and its web root, usually under /var/www/. If you have never set up a block from scratch, read our walkthrough on how to set up Nginx server blocks on Ubuntu first, since this guide builds on the same layout.
Pick a folder name that matches the domain, such as /var/www/seconddomain.com/html. Keep each site in its own folder so a mistake in one site can never leak files from another. If your first site runs PHP through PHP-FPM, the second site will need its own location ~ .php$ block too; the static example below is the starting point, and you can copy the PHP block from your first site config once the static version works.
Add a Second Domain to Nginx: Point DNS First
Before Nginx can serve the new domain, the domain has to resolve to your VPS. Log in to your registrar or DNS provider and add two records. The first is an A record for the root name (@) pointing to your VPS public IP address. The second is an A record for www pointing to the same IP. Add a second domain DNS A record for both names in one go, so you do not have to come back later when the www version fails.
Set the TTL low while you work, such as 300 seconds, so a wrong record can be fixed fast. A high TTL such as 86400 means a typo can take a full day to clear from caches. Check the records from your own machine with this command, replacing the domain with yours:
dig +short seconddomain.com
dig +short www.seconddomain.com
Both commands should print your VPS IP address. If they print nothing or an old IP, DNS has not spread yet. Wait a few minutes and check again. Do not move on to Certbot until this step returns the right IP, because the certificate check will fail while DNS still points elsewhere.
Create a Folder and a Test Page
Make a folder for the new site and put a small test page in it, so you can tell at a glance whether Nginx is serving the right folder. Run these commands, replacing seconddomain.com with your real domain:
sudo mkdir -p /var/www/seconddomain.com/html
sudo chown -R www-data:www-data /var/www/seconddomain.com/html
sudo chmod -R 755 /var/www/seconddomain.com
Then create a test page with a line of text that names the domain, so there is no doubt which site you are looking at:
echo "<h1>Second domain works</h1>" | sudo tee /var/www/seconddomain.com/html/index.html
The chown step matters. If the folder belongs to root with tight permissions, Nginx can read the files but PHP-FPM or your deploy user may not be able to write to them later. The www-data owner with 755 permissions is the standard setup on Ubuntu, and it matches how most guides on how to host a static website with Nginx on Ubuntu lay out their folders.
Write the Nginx Server Block for the Second Domain
Create a new file in /etc/nginx/sites-available/ named after the domain, for example /etc/nginx/sites-available/seconddomain.com, with this content:
server {
listen 80;
listen [::]:80;
server_name seconddomain.com www.seconddomain.com;
root /var/www/seconddomain.com/html;
index index.html index.htm;
access_log /var/log/nginx/seconddomain.com.access.log;
error_log /var/log/nginx/seconddomain.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
Three lines do the heavy lifting. server_name tells Nginx this block answers for the new domain and its www version. root points at the folder you just created. try_files serves the requested file, falls back to a directory index, and returns a 404 when neither exists. The separate access and error logs are worth adding now: when something breaks later, you can read this site’s log without wading through the first site’s traffic.
Enable the site by linking it into sites-enabled, then test the config and reload Nginx:
sudo ln -s /etc/nginx/sites-available/seconddomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Visit http://seconddomain.com in your browser. You should see the test page. If you see your first site instead, Nginx matched the wrong block, which usually means a typo in server_name or a missing symlink. The troubleshooting section below covers that case.
Add HTTPS to the Second Domain with Certbot
With the site answering over HTTP, add a free certificate from Let’s Encrypt. If Certbot is not installed yet, install it with sudo apt install certbot python3-certbot-nginx. Then run Certbot against both names of the new domain:
sudo certbot --nginx -d seconddomain.com -d www.seconddomain.com
Certbot checks that the domain points at your server, issues the certificate, and rewrites your server block to listen on port 443 with the certificate paths filled in. When it asks whether to redirect HTTP to HTTPS, choose the redirect option so every visitor lands on the secure version. If you want to understand the redirect it adds, see our guide on how to redirect HTTP to HTTPS in Nginx.
The Certbot second domain certificate is separate from your first site’s certificate, so renewing one never affects the other. Test the automatic renewal with sudo certbot renew --dry-run. If the dry run passes, the systemd timer that Certbot installs will keep the certificate fresh without further work from you. For the full install walkthrough, see our guide to install a free Let’s Encrypt certificate with Certbot.
Fix Common Problems When Adding a Second Domain
Most failures after adding a second domain come from a short list of causes. Work through them in order:
- The browser shows the first site, or the Nginx welcome page. Nginx picks the block whose
server_namematches the request. A typo inserver_name, a missing symlink insites-enabled, or adefault_serverflag on the wrong block all cause this. Runsudo nginx -Tand check which block Nginx actually loaded for the domain. - 404 Not Found on every page. The
rootpath in the block does not match the folder where the files live. Compare therootline with the real path and check for a missing/htmlat the end. - 403 Forbidden. The folder permissions are too tight, or a parent folder blocks the
www-datauser. The 755 permissions from the setup step above fix most cases. Check the site’s error log at/var/log/nginx/seconddomain.com.error.logfor the exact path that was denied. - DNS still shows the old IP. Your local resolver may be caching the old record. Check with
dig @8.8.8.8 seconddomain.comto ask Google’s resolver directly. If that shows the new IP, the record is fine and only your local cache needs time. - Certbot fails the challenge. Certbot reaches your server over port 80 to prove you control the domain. If DNS has not spread yet, or a firewall blocks port 80, the check fails. Fix DNS or open port 80 first, then run Certbot again.
nginx -treports an error. Read the line number in the error message. The usual cause is a missing semicolon or a missing closing brace in the block you just wrote. Fix it, test again, and only reload when the test says the config is OK.
Once the second site loads over HTTPS, the setup is done. Nginx multiple domains work the same way: adding a third domain repeats the same five steps: DNS record, folder, server block, symlink and reload, then Certbot. Keep each site in its own folder with its own logs, and one site’s problem will stay that site’s problem.