Sub topic 15
Reading logs and fixing 502, 403, and 404 errors
This chapter assumes you have a working Nginx setup in front of PHP-FPM or Gunicorn, as built in the earlier chapters. You should be comfortable with systemctl, file ownership, and HTTP status codes. The goal is a repeatable method for finding the cause of a failing request. Memorizing error messages is not the goal.
Where the evidence lives
A failing request leaves traces in several places. Check them in this order.
| Source | What it tells you | How to read it |
|---|---|---|
| Nginx access log | The status code Nginx sent, the URL, and timing | `/var/log/nginx/access.log` |
| Nginx error log | Why Nginx failed: missing files, denied access, upstream connection errors | `/var/log/nginx/error.log` |
| Application server log | Exceptions and worker crashes inside Gunicorn | `journalctl -u gunicorn` |
| PHP-FPM log | Pool errors and worker crashes | `journalctl -u php8.3-fpm` |
The unit names gunicorn and php8.3-fpm match the examples in this tutorial. Use the names on your own server. The access log tells you that something failed. The error log usually tells you why.
A default access log line looks like this:
203.0.113.7 - - [28/Nov/2026:04:10:02 +0000] "GET /report HTTP/1.1" 502 157 "-" "curl/8.5.0"The default format omits upstream details. You can add them with a custom format in the http block:
log_format upstream '$remote_addr "$request" $status $body_bytes_sent '
'rt=$request_time urt=$upstream_response_time ua=$upstream_addr';
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.access.log upstream;
error_log /var/log/nginx/example.error.log warn;
}$upstream_addr shows which socket or port Nginx tried. If it is empty, the request never reached an upstream, so Nginx answered from its own files or rules. This one field separates "Nginx problem" from "application problem" quickly.
A triage method
Start from the status code, then confirm it in the error log.
Always run sudo nginx -t after editing the configuration and before reloading. A syntax error stops the reload, so the old configuration keeps running and your fix appears to do nothing.
Fixing 502 Bad Gateway
A 502 means Nginx reached for the upstream and got no valid response. The upstream is PHP-FPM or Gunicorn. The error log names the exact failure:
connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream
connect() to unix:/run/gunicorn/app.sock failed (13: Permission denied) while connecting to upstream
connect() failed (111: Connection refused) while connecting to upstream
upstream prematurely closed connection while reading response header from upstreamEach message points to a different cause:
- No such file or directory (2). The socket does not exist. Either the service is stopped or the path in
fastcgi_passorproxy_passis wrong. A PHP upgrade often changes the socket name fromphp8.2-fpm.socktophp8.3-fpm.sock. - Permission denied (13). The socket exists but the Nginx worker user cannot open it. Check the socket's owner and mode, and the permissions on each parent directory.
- Connection refused (111). You are using TCP and nothing is listening on that port. The service is down or bound to a different address.
- Prematurely closed connection. The app accepted the connection and then died. Look in the application log for a crash, a killed worker, or an out-of-memory event.
Run these checks:
systemctl status gunicorn php8.3-fpm
sudo ss -xlp | grep -E 'gunicorn|php'
ls -l /run/gunicorn/app.sock /run/php/php8.3-fpm.sock
sudo journalctl -u gunicorn -n 50 --no-pagerss -xl lists listening Unix sockets. If your socket is missing from that list, Nginx cannot connect to it, whatever the configuration says. For PHP-FPM, the listen.owner, listen.group, and listen.mode settings in the pool file control who can open the socket. Nginx usually runs as www-data on Debian and Ubuntu, so the socket must allow that user.
A 504 Gateway Timeout is a close relative. The upstream accepted the request but did not answer within proxy_read_timeout or fastcgi_read_timeout. Look for slow queries before you raise the timeout. A higher timeout hides the slowness and ties up workers longer.
Fixing 403 Forbidden
A 403 means Nginx understood the request and refused it. Two causes cover most cases.
The first is a filesystem permission problem. The error log shows:
open() "/var/www/example/public/logo.png" failed (13: Permission denied)Nginx workers need execute permission on every directory in the path and read permission on the file. The namei command shows the permissions along the whole path:
namei -l /var/www/example/public/logo.png
sudo -u www-data test -r /var/www/example/public/logo.png && echo readableA directory such as /var/www/example with mode 700 owned by another user blocks Nginx even when the file itself is world-readable. Fix the narrowest thing: give the directory 755 or add www-data to the owning group. Avoid chmod -R 777, which removes the protection the permissions were providing.
The second cause is a directory request with no index file:
directory index of "/var/www/example/public/" is forbiddenNginx found a directory, found no file named in index, and has autoindex off. In the PHP configuration, the line index index.php; must name a file that exists in root. With try_files $uri $uri/ /index.php?$query_string;, a missing index.php produces this error.
Two other sources are worth checking. A deny all; rule can match more than you intended. The hidden-file rule location ~ /\.(?!well-known) { deny all; } blocks /.git/ and /.env, which is correct, but it also blocks any other path segment that starts with a dot. On servers with SELinux enabled, such as RHEL-family systems, a correct Unix permission can still be denied. Check sudo ausearch -m avc -ts recent in that case.
Fixing 404 Not Found
Nginx has two kinds of 404. Telling them apart saves time.
Nginx cannot find a file. The error log shows the full path Nginx tried:
open() "/var/www/example/public/css/site.css" failed (2: No such file or directory)Compare that path with where the file really is. Common causes are a wrong root, a typo in the deployment directory, and alias used with the wrong trailing slash. With alias, the matched part of the URL is replaced. In location /static/ { alias /srv/myapp/static/; }, both paths end in a slash. If one does, the other must too, or the paths join incorrectly.
The application returns the 404. The access log shows $upstream_addr filled in, and the error log has no open() failure. The framework's router did not recognize the URL. This is an application problem. Nginx forwarded the request correctly. Check the application's routes.
PHP-FPM has a third variant. The browser shows "File not found." and the Nginx error log shows:
FastCGI sent in stderr: "Primary script unknown"PHP-FPM received a SCRIPT_FILENAME that does not exist or that it cannot read. Verify that root is correct and that the PHP-FPM user can read the file.
A working debug loop
Change one thing, test, and read the log again.
sudo nginx -t && sudo systemctl reload nginx
curl -i http://example.com/some/path
sudo tail -n 20 /var/log/nginx/example.error.logYou can also bypass Nginx to find which side is at fault. Request Gunicorn directly with curl --unix-socket /run/gunicorn/app.sock http://localhost/. If that works and the public URL returns 502, the problem is in the Nginx-to-socket connection. If it fails too, the problem is in the application.
Exercise
Your site returns 502. The Nginx error log shows connect() to unix:/run/gunicorn/app.sock failed (13: Permission denied). systemctl status gunicorn reports the service as active. What is the likely cause, and which command do you run first?
Show answer
The socket exists and Gunicorn is running, so the problem is access. The Nginx worker user (usually www-data) cannot open the socket, or cannot traverse a parent directory. Run ls -l /run/gunicorn/app.sock and namei -l /run/gunicorn/app.sock to see the owner, mode, and directory permissions. Then fix the ownership or group, for example by running Gunicorn with a group that Nginx shares, or by setting the socket mode in the Gunicorn command.
Exercise
A request for /static/site.css returns 404, and the error log says open() "/srv/myapp/staticsite.css" failed (2: No such file or directory). The configuration has location /static/ { alias /srv/myapp/static; }. What is wrong, and what is the fix?
Show answer
The alias path lacks a trailing slash while the location path has one. Nginx replaces /static/ with /srv/myapp/static, which produces /srv/myapp/staticsite.css. Change the alias to /srv/myapp/static/, run sudo nginx -t, then reload Nginx.