Sub topic 15
Troubleshooting: Wrong Site, 403, 404, and Failed Reloads
Assume Nginx is installed and running, and that you understand basic DNS, HTTP requests, server blocks, and Linux permissions. Troubleshoot one layer at a time: request matching, file access, content lookup, and configuration loading.
A useful troubleshooting model
Nginx first chooses a listening address and port from listen. It then compares the request's Host header with server_name. After choosing a server block, Nginx applies its location rules. A static request is mapped through root and try_files; a proxied request is sent to the upstream application.
The wrong site is served
A wrong site usually means that the request did not match the block you expected. Common causes include a missing or misspelled server_name, a different listen address, an enabled default site, or a request using a different host name than you tested.
Inspect the complete active configuration, then test locally with an explicit host name:
sudo nginx -T
curl -i -H 'Host: example.org' http://127.0.0.1/
curl -i --resolve example.com:80:127.0.0.1 http://example.com/The first command displays included files and enabled blocks. The second changes only the HTTP Host header. The third makes curl resolve example.com to the local address for that request. Both tests avoid changing DNS.
If no server_name matches, Nginx uses the default server for that address and port. Check whether an unwanted distribution-provided default site is enabled. Remove its symlink only after confirming that no required site uses it, then validate and reload.
A 403 Forbidden response
A 403 means Nginx found a request but refused access. For static files, check the complete path, not only the file:
namei -l /var/www/example.com/html/index.html
sudo -u www-data test -r /var/www/example.com/html/index.html && echo readable
sudo tail -n 50 /var/log/nginx/example.com.error.logThe Nginx worker user needs execute permission on every parent directory and read permission on the file. A directory without an index file may also produce 403 when directory listing is disabled. Prefer placing an index.html file or configure the intended application behavior. Use the worker account from your distribution if it is not www-data. Do not make the whole document root world-writable to solve a permission problem.
A 404 Not Found response
A 404 commonly comes from a wrong root, a missing file, or a try_files rule that deliberately returns 404. Confirm the URI-to-file mapping:
sudo nginx -T | grep -A 12 -B 3 'server_name example.com'
ls -l /var/www/example.com/html/
curl -i -H 'Host: example.com' http://127.0.0.1/missing.htmlFor a reverse proxy, the 404 may come from the application rather than Nginx. Check the per-site access and error logs and the application's logs. Also inspect proxy_pass carefully because a trailing slash can change the path sent upstream.
A failed configuration test or reload
Never reload after an unverified edit. Run:
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -n 50 --no-pagernginx -t identifies syntax errors, duplicate listen options, missing certificate files, and invalid include paths. Fix the reported file and line first. A reload keeps existing workers serving requests while new workers use the valid configuration. If the test fails, the old configuration remains active, but the new change is not applied.
Exercise
A request to example.org returns the example.com page. What should you check first?
Show answer
Inspect the active configuration with nginx -T, then test with curl -H 'Host: example.org' http://127.0.0.1/. Check the exact server_name, listen directive, and enabled default site.