Sub topic 15
Reading Logs and Fixing 502, 403, and 404 Errors
Assume you know basic Linux commands, file permissions, HTTP status codes, and the Nginx-to-application layouts from earlier chapters. This chapter uses a Debian or Ubuntu-style server with Nginx. The log locations may differ on another distribution.
Follow the request path
A request can fail at several points:
A 404 Not Found means the server could not find a resource for the requested URL. A 403 Forbidden means the server found something but refused access. A 502 Bad Gateway means Nginx could not obtain a valid response from the upstream application server.
Start with the exact URL and request method. Then check whether the failure comes from Nginx, the file system, or the application.
Read the relevant logs
Nginx usually writes an access log for every request and an error log for processing failures. PHP-FPM and systemd services have separate logs.
sudo tail -n 50 /var/log/nginx/access.log
sudo tail -n 50 /var/log/nginx/error.log
sudo journalctl -u php8.3-fpm --since "15 minutes ago"
sudo journalctl -u myapp --since "15 minutes ago"Use the timestamp, request path, and status code to match one client request across these logs. For a Python service named myapp, journalctl shows Gunicorn output captured by systemd.
Fix 502 Bad Gateway
For PHP, check that PHP-FPM is running and that Nginx uses the same socket path:
sudo systemctl status php8.3-fpm
sudo ss -lx | grep php
sudo nginx -tFor Gunicorn, check the service and socket:
sudo systemctl status myapp
sudo journalctl -u myapp -n 50 --no-pager
sudo ls -l /run/gunicorn/app.sockCommon causes include a stopped process, a wrong socket or port, a crashed application, or permission denied when Nginx opens a Unix socket. The Nginx error log usually identifies the cause with messages such as “connect() failed” or “upstream prematurely closed connection.”
Fix 403 Forbidden
Check the configured root or alias, then inspect every parent directory:
sudo namei -l /var/www/example/public/index.html
sudo ls -ld /var/www/example/publicThe Nginx worker user needs execute permission on directories and read permission on files. A location that contains deny all; can also intentionally produce 403 responses. Check whether a hidden-file rule, missing index file, or directory listing restriction matches the request.
Fix 404 Not Found
Confirm that the URL maps to the intended root and location. For static content, verify the file exists at the path Nginx constructs. For application routes, ensure try_files or proxy_pass sends the request to the application instead of looking for a physical file.
curl -i http://127.0.0.1/unknown-path
sudo nginx -T | lessnginx -T prints the complete active configuration, including included files. This often reveals that you edited a disabled server block or that another server block handles the hostname.
Exercise
Nginx returns 502, and the error log says it cannot connect to a Unix socket. What should you check first?
Show answer
Check that the application service is running, that the configured socket path exists, and that the Nginx worker user has permission to access the socket.