keepcalculator

06 Numbers and Powers

Nginx Server Blocks: Setup and Error Fixes

Card No. 06·6193 · Updated · Standard formula, shown below

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 nginx on 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

  1. Create the directory structure. For each site, create a directory under /var/www/. For example, for example.com:
    sudo mkdir -p /var/www/example.com/html

    Repeat for each domain you plan to host.

  2. Set ownership and permissions. Assign the directory to your user (replace $USER with your username) and set appropriate permissions:
    sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chmod -R 755 /var/www/example.com
  3. 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
  4. 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
  5. 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_name must match your domain exactly.

  6. Enable the server block. Create a symbolic link in sites-enabled:
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  7. Test the configuration. Run this command to check for syntax errors:
    sudo nginx -t

    You should see syntax is ok and test is successful.

  8. Reload Nginx. Apply the changes without dropping connections:
    sudo systemctl reload nginx
  9. Verify. Visit http://example.com in a browser. You should see your test page. Repeat steps 1-8 for each additional site.

Common Mistakes

  • Forgetting the trailing slash in the root directive. 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_name correctly, leading to the default server block being used.
  • Syntax errors like missing semicolons or braces. Always run nginx -t after editing.
  • Using the same port for multiple server blocks without proper server_name differentiation.

Tips

  • Use descriptive file names for server blocks (e.g., example.com, not site1).
  • Keep the default server block (default) as a catch-all for unmatched requests.
  • Test changes with curl -H "Host: example.com" http://localhost if DNS isn’t set up yet.
  • Use sudo systemctl status nginx to 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

The whole Numbers and Powers drawer →