This guide shows you how to configure a single Nginx server to host multiple websites, some using plain HTTP and others using HTTPS, all on the same IP address. You’ll use Server Name Indication (SNI) so Nginx can tell which site a request is for and serve the correct certificate and content. You’ll need root or sudo access to your server, a domain name pointing to your server’s IP, and SSL certificates for the HTTPS sites (Let’s Encrypt is a common free option).
What You’ll Need
- A server running Linux (Ubuntu, Debian, CentOS, etc.) with Nginx installed.
- Root or sudo access to edit Nginx configuration files.
- At least one domain name (e.g., example.com) with DNS A records pointing to your server’s public IP.
- SSL/TLS certificates for each HTTPS site. You can get free ones from Let’s Encrypt using certbot, or use your own.
- Basic familiarity with the command line and text editors (nano or vim).
Step-by-Step
- Check Nginx is installed and running. Run
nginx -vto see the version. If not installed, use your package manager (e.g.,sudo apt install nginxon Ubuntu). Ensure it’s running withsudo systemctl status nginx. - Create directory structure for your sites. For each site, create a folder for its files and logs. For example, for
example.comandapi.example.com:sudo mkdir -p /var/www/example.com/html sudo mkdir -p /var/www/api.example.com/html sudo mkdir -p /var/log/nginx/example.com sudo mkdir -p /var/log/nginx/api.example.com
- Add a test index file for each site. Create a simple
index.htmlin each site’s html folder so you can verify it works later. For example:echo "<h1>Example Site</h1>" | sudo tee /var/www/example.com/html/index.html echo "<h1>API Site</h1>" | sudo tee /var/www/api.example.com/html/index.html
- Obtain SSL certificates. If you don’t have certificates, use certbot. Install it (e.g.,
sudo apt install certbot python3-certbot-nginx), then run for each HTTPS domain:sudo certbot certonly --nginx -d example.com -d www.example.com
This will place certificates in
/etc/letsencrypt/live/example.com/. Repeat for other HTTPS sites. - Create Nginx server blocks (virtual hosts). Nginx uses server blocks in
/etc/nginx/sites-available/and symlinks in/etc/nginx/sites-enabled/. Create a file for each site. For an HTTP-only site (e.g.,example.com), create/etc/nginx/sites-available/example.comwith:server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html; access_log /var/log/nginx/example.com/access.log; error_log /var/log/nginx/example.com/error.log; } - Create a server block for an HTTPS site. For
api.example.com, create/etc/nginx/sites-available/api.example.comwith:server { listen 443 ssl; server_name api.example.com; root /var/www/api.example.com/html; index index.html; ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; access_log /var/log/nginx/api.example.com/access.log; error_log /var/log/nginx/api.example.com/error.log; } - Enable the site configurations. Create symlinks from
sites-availabletosites-enabled:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/
- Test the Nginx configuration. Run
sudo nginx -tto check for syntax errors. If it says “test is successful”, proceed. If not, fix the errors as indicated. - Reload Nginx. Apply the changes with
sudo systemctl reload nginx(orsudo service nginx reload). - Test your sites. Open a browser and visit
http://example.comandhttps://api.example.com. You should see the respective test pages. If you get a certificate warning, check your certificate paths and domain names.
Common Mistakes
- Forgetting to enable the site. A symlink in
sites-enabledis required. Without it, Nginx won’t load the server block. - Wrong certificate paths. Double-check the
ssl_certificateandssl_certificate_keypaths. They must point to the actual files, not just the directory. - Mixing up server_name. Ensure
server_namematches the domain exactly, including subdomains. Wildcards like*.example.comwork but only for one level. - Listening on the same port twice. You can’t have two server blocks both listening on 80 with the same server_name. For HTTPS, you can have multiple blocks on 443, but they must have different server_name values.
- Not reloading after changes. A reload is needed for changes to take effect. A restart also works but is more disruptive.
Tips
- Use HTTP/2. Add
listen 443 ssl http2;to your HTTPS server blocks to improve performance. - Redirect HTTP to HTTPS. If a site should be HTTPS-only, add a server block that listens on 80 and returns a 301 redirect to the HTTPS version. For example:
server { listen 80; server_name example.com www.example.com; return 301 https://example.com$request_uri; } - Set up automatic certificate renewal. Certbot can renew automatically. Test with
sudo certbot renew --dry-runand set up a cron job if needed. - Keep your Nginx version updated. SNI works in Nginx 0.5.32 and later, but modern versions have better TLS defaults.
Troubleshooting
- Site not loading at all. Check Nginx is running:
sudo systemctl status nginx. Check the error log at/var/log/nginx/error.log. - Certificate warning in browser. This usually means the certificate is for a different domain, is expired, or the path is wrong. Run
sudo nginx -tand check the certificate withopenssl x509 -in /path/to/cert.pem -text -noout. - Wrong site served. If you visit
http://example.comand get the API site, check that the server_name in your HTTP block matches exactly. Also check that you haven’t defined a default server that catches all requests. - HTTPS works but HTTP doesn’t. Ensure you have a server block listening on 80 for that domain. If you only have an HTTPS block, HTTP requests will fail.
- Port 443 is already in use. Check with
sudo netstat -tulpn | grep 443. Another service might be using it. If so, stop it or change your Nginx config.
Frequently Asked Questions
Can I host both HTTP and HTTPS sites on the same IP with SNI?
Yes, that’s exactly what this guide does. SNI lets Nginx look at the domain name in the TLS handshake and route to the correct server block, even if they share the same IP.
Do I need a separate IP for each site?
No. With SNI, you can host many sites on one IP, as long as each has a unique server_name and, for HTTPS, a valid certificate for that name.
What if I want to force all HTTP traffic to HTTPS?
Add a server block that listens on 80 and returns a 301 redirect to the HTTPS URL as shown in the Tips section. This is common for security.
How do I get a free SSL certificate?
Let’s Encrypt provides free certificates. Use certbot with the certonly command to get one for your domain. It’s automated and renews automatically.
Can I use wildcard certificates?
Yes, but they cost more or require DNS-01 challenges with Let’s Encrypt. For a few subdomains, individual certificates are simpler.
What is SNI exactly?
Server Name Indication is an extension to TLS that lets the client specify the hostname it’s connecting to during the handshake. This allows the server to present the correct certificate and configuration for that hostname.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Samsung Unpacked August 5: 5 Devices Teased?
- Google Workspace Accounts Created Bypassing?
- Recover Deleted Screenshots on Mac: Full Guide
- Go vs Node.js: Cloudflare API Request Data
- ROG Phone 3 Certified Specs Leak: What’s Confirmed
- Clear Cache on a Philips Smart TV: Right Menu