A Complete Server Block
server {
listen 80;
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;
location / {
try_files $uri $uri/ =404;
}
}
- listen: the port, for IPv4 and IPv6.
- server_name: the host names this block answers for.
- root: the folder the files are served from.
- index: the file served when the address ends in a folder.
- try_files: serve the file or folder if it exists, otherwise answer 404.
Step 1: Create the Web Root
sudo mkdir -p /var/www/example.com/html
sudo chown -R deploy:deploy /var/www/example.com
echo 'example.com works' | sudo -u deploy tee /var/www/example.com/html/index.html
Nginx only needs to read the files. Folders with mode 755 and files with 644, owned by the account that deploys the site, are enough. Never use 777.
Step 2: Save the Block on Debian and Ubuntu
sudo nano /etc/nginx/sites-available/example.com
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Nginx reads only sites-enabled. The link makes the site active; removing the link turns the site off and keeps the file. Links are explained in how to create a symlink in Linux.
Step 2 on AlmaLinux and Rocky Linux
sudo nano /etc/nginx/conf.d/example.com.conf
sudo restorecon -Rv /var/www/example.com
There are no sites folders: every file in conf.d that ends in .conf is loaded. The worker processes run as nginx instead of www-data. The restorecon line makes sure the folder has the SELinux label for web content. Files created there usually inherit it, but files moved in from a home folder keep their old label, and Nginx then answers 403 even with correct permissions.
Step 3: Test and Reload
sudo nginx -t
sudo systemctl reload nginx
The test should report "syntax is ok" and "test is successful". Reload rather than restart: if the test failed and you reload anyway, the running server keeps the old configuration, while a restart with a broken file takes every site down. Before DNS points to the server, test with the host name set by hand:
curl -H "Host: example.com" http://localhost/
How Nginx Picks a Server Block
- It keeps the blocks that listen on the address and port the request arrived on.
- It compares the Host header with each
server_name: an exact name first, then the longest wildcard that starts with*, then the longest wildcard that ends with*, then the first matching regular expression in the order they appear. - If nothing matches, the default server for that port answers: the block marked
default_server, or else the first block defined for that port.
Set a Default Server
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
This block catches requests for unknown host names and for the bare IP address. Code 444 is special to Nginx: it closes the connection without a response. Only one block per address and port may carry default_server. On Debian and Ubuntu the shipped default site already has it, so disable that site first; sudo rm /etc/nginx/sites-enabled/default removes only the link. On AlmaLinux and Rocky Linux the server block inside nginx.conf has no marker, and the conf.d files are included before it, so without a marker the first site in conf.d becomes the default for port 80.
Common Mistakes
- No link in sites-enabled: the file exists but Nginx never reads it.
- Wrong file name in conf.d: a file without the
.confending is ignored. - Duplicate default server:
nginx -tfails with "a duplicate default server" and names the file and line of the second one. - Conflicting server name: two blocks claim the same name on the same port; the test prints a warning that ends in "ignored", and only the first block answers for that name.
- Wrong root: pages answer 404; see 404 Not Found. Permission problems give a 403 Forbidden.
Once the site answers over HTTP, add a certificate with Certbot and then redirect HTTP to HTTPS.