Sub topic 15
Troubleshooting: wrong site, 403, 404 and failed reloads
This chapter assumes you can read an Nginx server block, run nginx -t, and use curl with a Host header. You should also know how a request is matched to a server block by listen and server_name, and how file ownership and permissions work on Linux. The goal is a repeatable way to find out why a multi-site setup misbehaves, starting from the symptom.
A method that works for every symptom
Most multi-site problems fall into four groups: the configuration does not load, the wrong server block answers, the right block cannot read the files, or the right block looks in the wrong place. Work through them in this order:
- Confirm the configuration is valid and the running Nginx has loaded it.
- Confirm which server block answers the request.
- Read the error log of that block.
- Check the file path and permissions the log names.
Failed reloads
systemctl reload nginx can report success or failure, but the reliable check is nginx -t. It parses the whole configuration, including every included file, and prints the file and line of the first problem.
sudo nginx -tTypical results and their meaning:
| Message | Cause |
|---|---|
| `unexpected "}"` or `directive is not terminated by ";"` | A missing semicolon or unbalanced brace in a site file |
| `unknown directive "servername"` | A typo in a directive name |
| `duplicate listen options for 0.0.0.0:80` | Two blocks declare `default_server` on the same address and port |
| `conflicting server name "example.com" on 0.0.0.0:80, ignored` | Two blocks claim the same name. This is a warning, and the first block wins |
| `bind() to 0.0.0.0:80 failed (98: Address already in use)` | Another process, such as Apache, holds the port |
| `cannot load certificate "...": No such file` | A wrong certificate path, or the certificate has not been issued yet |
| `open() "/etc/nginx/sites-enabled/x" failed (2: No such file or directory)` | A dangling symlink in `sites-enabled` |
Two points catch people out. First, a reload with a broken configuration does not take the site down: Nginx keeps running the old configuration and logs the error. A site you "just changed" may therefore still behave as before. Second, nginx -t checks syntax and file existence, not whether your logic is right. A valid file can still serve the wrong site.
If a reload fails, read the service log:
sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -n 30 --no-pagerTo see the configuration Nginx actually loaded, with every include expanded, use:
sudo nginx -T | lessThis is the fastest way to find a stale file in sites-enabled or an include you forgot about.
The wrong site answers
When example.org shows the content of example.com, Nginx has chosen a different block than you expected. Nginx chooses by address and port first, then by the Host header. If no server_name matches, it uses the default_server for that address and port. If none is marked, it uses the first block defined for it.
Test without involving DNS:
curl -i -H 'Host: example.org' http://127.0.0.1/
curl -i --resolve example.org:80:127.0.0.1 http://example.org/Then check these causes:
- Misspelled
server_name.server_name exmaple.org;never matches, so the request falls to the default block. Compare the name in the file with the name in the request. - The site is not enabled. The file exists in
sites-availablebut there is no symlink insites-enabled. Runls -l /etc/nginx/sites-enabled/. - A broken symlink.
ls -lshows a red or missing target. Recreate it with an absolute path. - The reload did not happen. Run
sudo nginx -t && sudo systemctl reload nginxand watch for errors. - The
listenaddresses differ. A block withlisten 192.0.2.10:80;and one withlisten 80;are not competing for the same socket. Nginx first picks the most specific matching address, and only then compares names within that address. - A leftover default site. The packaged
defaultfile often declaresdefault_server. If it is still enabled, unknown names land there and show the welcome page. - Port mismatch in
Host. A client that sendsHost: example.com:8080still matchesserver_name example.com, because Nginx ignores the port when it compares names.
A wildcard or regular expression name can also steal requests. The order of preference is an exact name, then the longest wildcard starting with *, then the longest wildcard ending with *, then the first matching regular expression. Run sudo nginx -T | grep -n server_name to list every name in one view.
For HTTPS the same rules apply, with one extra step. The server first selects a certificate using SNI, the name the client sends during the TLS handshake. Then it chooses the block using the Host header. A test with curl -H 'Host: ...' https://127.0.0.1/ sends no SNI name for the TLS handshake, so you see the default certificate. Use --resolve instead, which sets both:
curl -vI --resolve example.com:443:127.0.0.1 https://example.com/403 Forbidden
A 403 means Nginx found the site and refused to serve the path. The error log gives the exact reason:
sudo tail -n 20 /var/log/nginx/error.logThe two common messages are:
directory index of "/var/www/example.com/html/" is forbiddenopen() "/var/www/example.com/html/index.html" failed (13: Permission denied)
The first message means the request ended at a directory, there was no index file in it, and autoindex is off. Fix it by adding the file, or by correcting index to name the file that exists. If the file is index.htm and the block says index index.html;, you get this error.
The second message is a permission problem. Nginx workers run as an unprivileged user, usually www-data on Debian and Ubuntu and nginx on RHEL-based systems. That user needs execute permission on every directory in the path and read permission on the file. Check each element with namei:
namei -l /var/www/example.com/html/index.html
ps -o user= -C nginx | sort -uLook for a directory in the chain, such as /home/alice, that lacks x for others. A typical fix is:
sudo chmod 755 /var/www/example.com /var/www/example.com/html
sudo chmod 644 /var/www/example.com/html/index.htmlOn systems with SELinux, such as RHEL, Rocky and Fedora, correct Unix permissions are not enough. Files under a non-standard path need the httpd_sys_content_t label. Check the denial and fix the label:
sudo ausearch -m avc -ts recent
sudo semanage fcontext -a -t httpd_sys_content_t '/srv/sites(/.*)?'
sudo restorecon -Rv /srv/sitesA deny rule in a location, or an allow/deny pair that excludes your address, also returns 403. In that case the error log says access forbidden by rule.
404 Not Found
A 404 means Nginx chose a block and looked for a file that does not exist. The error log shows the full path it tried:
open() "/var/www/example.com/html/about.html" failed (2: No such file or directory)Compare that path with the real file. These are the usual causes:
rootis wrong. Nginx appends the URI toroot. Withroot /var/www/example.com/html;, a request for/about.htmlreads/var/www/example.com/html/about.html. A typo or a missing/htmldirectory is enough to break every page.rootvsalias. Withroot, the full location path is appended. Inlocation /static/ { root /data; }, a request for/static/a.cssreads/data/static/a.css. Withalias /data/;it reads/data/a.css. Mixing the two up gives 404 for assets only.- Case sensitivity. Linux file names are case sensitive, so
Index.htmlis notindex.html. try_files ... =404. This is intended behaviour. If you expected a single-page application to handle unknown paths, end with/index.htmlinstead of=404.- Wrong block. If the 404 body is the default Nginx page or the content of another site, the request was matched to a different block. Go back to the previous section.
- Path in a different error log. If a block sets its own
error_log, entries are not in/var/log/nginx/error.log. Look in the file named inside the block.
For a proxied site, a 404 can come from the application and not from Nginx. Compare with a direct request:
curl -i http://127.0.0.1:3000/
curl -i -H 'Host: app.example.net' http://127.0.0.1/If the first succeeds and the second does not, check the proxy_pass URL. A proxy_pass with a URI part, such as http://127.0.0.1:3000/app/;, rewrites the path, while one without it passes the path through unchanged.
502 and 504 on proxied sites
A 502 Bad Gateway means Nginx could not get a valid answer from the application. A 504 Gateway Timeout means it waited too long. The error log shows connect() failed (111: Connection refused) when nothing listens on the port. Check the application:
sudo ss -ltnp | grep ':3000'If nothing is listening, start the application service. On SELinux systems, connect() ... (13: Permission denied) points to the httpd_can_network_connect boolean, which you enable with sudo setsebool -P httpd_can_network_connect 1.
A short checklist
sudo nginx -t
sudo nginx -T | grep -nE 'server_name|listen|root'
ls -l /etc/nginx/sites-enabled/
curl -i --resolve example.com:80:127.0.0.1 http://example.com/
sudo tail -n 20 /var/log/nginx/error.logRun these in order. The first shows whether the file is valid. The second shows what Nginx loaded. The third shows which sites are enabled. The fourth shows what the server answers for one name, and the fifth shows why it refused or failed.
Exercise
After you enable a new site example.org, a request with curl -H 'Host: example.org' http://127.0.0.1/ returns the page of example.com. nginx -t passes. List three things you would check, in order, and the command for each.
Show answer
1) Check that the site is loaded: sudo nginx -T | grep -n server_name should list example.org, and ls -l /etc/nginx/sites-enabled/ should show a working symlink. 2) Check the spelling of server_name in the new file and that it listens on the same address and port as the other block. 3) Confirm the reload actually ran: sudo nginx -t && sudo systemctl reload nginx, then repeat the curl. If the name is correct and the site is loaded, also look for another block that uses a wildcard or regular expression name that matches first.
Exercise
The error log shows open() "/var/www/example.com/html/index.html" failed (13: Permission denied). Which command reveals the directory that blocks access, and what must the worker user have on directories and files?
Show answer
namei -l /var/www/example.com/html/index.html lists the owner and mode of every path component. The worker user (for example www-data) needs execute (x) permission on every directory in the path and read (r) permission on the file. On SELinux systems the content must also carry the httpd_sys_content_t label.
Exercise
A request returns 404, and the error log says open() "/data/static/a.css" failed (2: No such file or directory). The file exists at /data/a.css, and the block contains location /static/ { root /data; }. What change fixes it, and why?
Show answer
Replace root /data; with alias /data/;. With root, Nginx appends the whole URI, so /static/a.css becomes /data/static/a.css. With alias, the matched /static/ prefix is replaced by /data/, giving /data/a.css.