Nginx Server Blocks on Debian: Setup Guide
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.comand edit your local hosts file. - Basic familiarity with the command line and file editing (nano or vim).
Step-by-Step
- 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/htmlsudo mkdir -p /var/www/example.com/logs
Repeat for any other domains you plan to host. - Set ownership and permissions. Assign the directory to your user (or to
www-dataif 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 - Create a test page. Add an
index.htmlfile so you can verify the server block works later:echo 'Hello from example.com!' | sudo tee /var/www/example.com/html/index.html - 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 - 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.
- Enable the server block. Create a symlink from
sites-availabletosites-enabled:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ - Test the configuration. Run:
sudo nginx -t
If the output showssyntax is okandtest is successful, proceed. If there are errors, fix them before continuing. - Reload Nginx. Apply the changes without dropping connections:
sudo systemctl reload nginx - Verify the site. In a browser, visit
http://example.com(or usecurl http://localhostwith the Host header if you haven’t set DNS). You should see your test page. - 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-availablebut not symlinking it tosites-enabledmeans 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 missingwww.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 -tfirst. - Using the default site: The default server block at
/etc/nginx/sites-enabled/defaultwill catch requests that don’t match any otherserver_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_nameset to a test subdomain and use theHostheader 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
includefor 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
rootpath is correct and that the file exists. Also ensure theindexdirective 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/htmland 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_namein each config and ensure the default site is not intercepting. Usecurl -H "Host: example.com" http://your_server_ipto see which site responds. - Nginx fails to reload: Run
sudo nginx -tto see the error. Common issues are missing semicolons, unclosed braces, or duplicatelistendirectives. - 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/hostson 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.