ThisCalculator

Everyday

Nginx Server Blocks on Debian: Setup Guide

Updated · Standard formula, shown below

This guide walks you through setting up Nginx server blocks (virtual hosts) on Debian. Server blocks let you host multiple websites on a single server, each with its own domain name, document root, and configuration. By the end, you’ll have a working multi-site Nginx setup with proper permissions and basic security.

What You’ll Need

  • A Debian server (10, 11, or 12) with root or sudo access.
  • Nginx installed (sudo apt install nginx).
  • Domain names (or subdomains) pointing to your server’s public IP. For testing, you can use example.com and edit your local hosts file.
  • Basic familiarity with the command line and file editing (nano or vim).

Step-by-Step

  1. Create the directory structure. For each site, create a directory for the web files and a separate directory for logs. For example, for example.com:
    sudo mkdir -p /var/www/example.com/html
    sudo mkdir -p /var/www/example.com/logs
    Repeat for any other domains you plan to host.
  2. Set ownership and permissions. Assign the directory to your user (or to www-data if you want Nginx to write to it):
    sudo chown -R $USER:$USER /var/www/example.com/html
    Then ensure the web root is readable by Nginx:
    sudo chmod -R 755 /var/www/example.com
  3. Create a test page. Add an index.html file so you can verify the server block works later:
    echo 'Hello from example.com!' | sudo tee /var/www/example.com/html/index.html
  4. Create a server block configuration file. Nginx on Debian loads server blocks from /etc/nginx/sites-available/ and enables them via symlinks in /etc/nginx/sites-enabled/. Create a new file:
    sudo nano /etc/nginx/sites-available/example.com
  5. Write the server block configuration. Paste the following, adjusting the domain and paths as needed:
    server {
        listen 80;
        listen [::]:80;
    
        server_name example.com www.example.com;
    
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
    
        access_log /var/www/example.com/logs/access.log;
        error_log /var/www/example.com/logs/error.log;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }

    Save and exit.

  6. Enable the server block. Create a symlink from sites-available to sites-enabled:
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  7. Test the configuration. Run:
    sudo nginx -t
    If the output shows syntax is ok and test is successful, proceed. If there are errors, fix them before continuing.
  8. Reload Nginx. Apply the changes without dropping connections:
    sudo systemctl reload nginx
  9. Verify the site. In a browser, visit http://example.com (or use curl http://localhost with the Host header if you haven’t set DNS). You should see your test page.
  10. Repeat for additional sites. For each new domain, repeat steps 1-9 with a new directory and a new configuration file. Remember to create a separate symlink and test again.

Common Mistakes

  • Forgetting to enable the site: Creating the file in sites-available but not symlinking it to sites-enabled means Nginx won’t see it.
  • Wrong permissions: If the web root is owned by root and not readable by others, Nginx (running as www-data) may return 403 Forbidden.
  • Typos in server_name: The domain must match exactly, including subdomains. A missing www. can cause the wrong site to load.
  • Not testing before reload: A syntax error in a config file can bring down all sites. Always run nginx -t first.
  • Using the default site: The default server block at /etc/nginx/sites-enabled/default will catch requests that don’t match any other server_name. If you want your site to be the default, edit that file or remove it.

Tips

  • Use a separate log per site: This makes debugging easier. The configuration above already does this.
  • Set up SSL early: Once the site works over HTTP, use Let’s Encrypt (via certbot) to add HTTPS. It’s easier to do before you have complex redirects.
  • Use a staging server block: For testing, create a server block with server_name set to a test subdomain and use the Host header in curl to simulate requests.
  • Keep the configuration organized: Name files after the domain (e.g., example.com) and include comments. This helps when you have many sites.
  • Use include for common settings: If you have many sites, put shared settings (like gzip or security headers) in a separate file and include it in each server block.

Troubleshooting

  • Site returns 404: Check that the root path is correct and that the file exists. Also ensure the index directive lists the file you have (e.g., index.html).
  • Site returns 403 Forbidden: This usually means the web root or index file is not readable by Nginx. Check permissions with ls -l /var/www/example.com/html and ensure the path is traversable (execute permission on directories).
  • Wrong site loads: This often happens when multiple server blocks match the request. Check the server_name in each config and ensure the default site is not intercepting. Use curl -H "Host: example.com" http://your_server_ip to see which site responds.
  • Nginx fails to reload: Run sudo nginx -t to see the error. Common issues are missing semicolons, unclosed braces, or duplicate listen directives.
  • Changes not taking effect: After editing a config file, you must reload Nginx: sudo systemctl reload nginx. Also, check that your symlink points to the correct file.
  • DNS not resolving: If you’re using real domains, verify that the A/AAAA records point to your server’s IP. For local testing, edit /etc/hosts on your client machine to map the domain to the server IP.

Frequently Asked Questions

What is the difference between sites-available and sites-enabled?

sites-available holds all your configuration files, while sites-enabled contains symlinks to the ones you want active. This lets you disable a site by removing the symlink without deleting the config.

Can I use IP addresses instead of domain names?

Yes, you can set server_name to an IP address. However, this is less flexible if the IP changes, and you can only have one site per IP unless you use different ports or SNI with SSL.

How do I serve PHP files with server blocks?

You need to install PHP-FPM and add a location ~ \.php$ block that passes requests to the FPM socket. The exact configuration depends on your PHP version, but a typical setup uses fastcgi_pass unix:/run/php/php8.2-fpm.sock;.

What is the default server block?

The default server block (usually in /etc/nginx/sites-available/default) handles requests that don’t match any other server_name. You can modify it or remove it to control what happens for unknown domains.

Why is my site not showing up in the browser?

Check that DNS is correct, the server is reachable, Nginx is running, and the server block is enabled. Also check your firewall (if any) to ensure port 80 is open.

Can I have multiple server blocks on the same port?

Yes, Nginx uses the server_name directive to distinguish between them. As long as the domain names are different, multiple blocks can listen on port 80.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.