This guide walks you through setting up Nginx server blocks (the equivalent of Apache’s virtual hosts) so you can host multiple websites on a single server. You’ll learn how to create the directory structure, configure the server block files, enable them, and test your setup. We’ll also cover the most common errors you’ll encounter and how to fix them.
What You’ll Need
- A server running Linux (Ubuntu/Debian commands shown; adjust for your distribution).
- Nginx installed (
sudo apt install nginxon Debian/Ubuntu). - Root or sudo access.
- Domain names (or subdomains) pointing to your server’s IP address.
- Basic familiarity with the command line.
Step-by-Step
- Create the directory structure. For each site, create a directory under
/var/www/. For example, forexample.com:sudo mkdir -p /var/www/example.com/html
Repeat for each domain you plan to host.
- Set ownership and permissions. Assign the directory to your user (replace
$USERwith your username) and set appropriate permissions:sudo chown -R $USER:$USER /var/www/example.com/html sudo chmod -R 755 /var/www/example.com
- Create a sample index page. Add a simple HTML file to test later:
echo '<h1>Hello from example.com</h1>' | sudo tee /var/www/example.com/html/index.html
- Create a server block file. In Nginx, server blocks are stored in
/etc/nginx/sites-available/. Create a file for your site:sudo nano /etc/nginx/sites-available/example.com
- Write the server block configuration. A minimal configuration looks like this:
server { listen 80; listen [::]:80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm; }Save and exit. The
server_namemust match your domain exactly. - Enable the server block. Create a symbolic link in
sites-enabled:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Test the configuration. Run this command to check for syntax errors:
sudo nginx -t
You should see
syntax is okandtest is successful. - Reload Nginx. Apply the changes without dropping connections:
sudo systemctl reload nginx
- Verify. Visit
http://example.comin a browser. You should see your test page. Repeat steps 1-8 for each additional site.
Common Mistakes
- Forgetting the trailing slash in the
rootdirective. Use/var/www/example.com/html/(trailing slash is optional but recommended). - Wrong file permissions causing 403 Forbidden errors. Ensure the directory is readable (755) and files are 644.
- Not setting
server_namecorrectly, leading to the default server block being used. - Syntax errors like missing semicolons or braces. Always run
nginx -tafter editing. - Using the same port for multiple server blocks without proper
server_namedifferentiation.
Tips
- Use descriptive file names for server blocks (e.g.,
example.com, notsite1). - Keep the default server block (
default) as a catch-all for unmatched requests. - Test changes with
curl -H "Host: example.com" http://localhostif DNS isn’t set up yet. - Use
sudo systemctl status nginxto check if Nginx is running. - Consider using a configuration management tool (like Ansible) for multiple sites.
Troubleshooting
If you get a 403 Forbidden: Check that the root directory exists and has correct permissions. Also ensure the index file is present.
If you get a 404 Not Found: Verify the root path is correct and the file exists. Check for typos in the server_name or root directives.
If the wrong site loads: Ensure each server block has a unique server_name. If multiple blocks match, Nginx uses the first one in alphabetical order. Use default_server to specify a fallback.
If Nginx fails to start: Run sudo nginx -t to see the error. Common issues are missing semicolons, unmatched braces, or invalid paths. Check the error log at /var/log/nginx/error.log.
If changes don’t take effect: Remember to reload Nginx after editing: sudo systemctl reload nginx. Also clear your browser cache.
If you’re using SSL (HTTPS): Ensure you have a valid certificate and the listen 443 ssl directive is set. Use Certbot for free certificates.
Frequently Asked Questions
What is the difference between sites-available and sites-enabled?
sites-available holds all your server block configurations, while sites-enabled contains symbolic links to the ones you want active. This allows you to easily enable/disable sites without deleting files.
Can I use IP addresses instead of domain names?
Yes, you can use an IP address in server_name, but it’s not recommended for production. Nginx will treat the IP as a literal string; you’d need to use default_server to catch all requests for that IP.
How do I redirect HTTP to HTTPS?
Add a server block that listens on port 80 and returns a 301 redirect to the HTTPS version:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Why does my site show the default Nginx page?
This usually means your server block isn’t being used. Check that the server_name matches your domain exactly, and that the symbolic link exists in sites-enabled. Also ensure the default server block isn’t catching your request.
Can I host multiple sites on the same IP?
Yes, that’s the whole point of server blocks. As long as each site has a unique server_name, Nginx will route requests correctly based on the Host header.
How do I set up a subdomain like blog.example.com?
Create a new server block with server_name blog.example.com; and point its root to a different directory. Make sure the DNS record for the subdomain points to your server.
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
- Mac Start Up Automatically After Power Failure?
- Expose Self-Hosted Service Securely Without Cloudflare Tunnel
- Nano Delivery for Functional Foods: What It Can and Can’t Do4.00
- Disable Apple Watch Turn-by-Turn Vibration While Driving
- Assassin’s Creed Mirage: How Long to Beat
- Phishing Detector 96.70% Accuracy: What It Means