Nginx 403 Forbidden — Tell Permission Errors from Index Errors
Two error.log messages explain a 403. Find the blocking directory with namei -l for Permission denied; add an index or autoindex when the index is forbidden.
The browser shows only 403 Forbidden, but error.log records one of two different causes. The lines below come from Nginx on high ports with permissions broken on purpose; the leading time and PID and the trailing client:, server: and host: fields are shortened to …. For the same kind of triage on 5xx errors, see the 502 guide.
[1] Search error.log around the time of the 403. Find its path with nginx -T.
# nginx -T 2>/dev/null | grep error_log# grep -E 'forbidden|Permission denied' /var/log/nginx/error.log | tailYou get one of these:
… [error] … open() "/srv/site/index.html" failed (13: Permission denied), … request: "GET /index.html HTTP/1.1", …… [error] … directory index of "/srv/files/" is forbidden, … request: "GET / HTTP/1.1", …If the line ends in (13: Permission denied), the operating system refused the Nginx worker: go to steps 2–4. If it says directory index of … is forbidden, permissions are not the problem: go to step 5.
[2] Check which user the worker runs as. Permissions are checked against the worker user, not the root master process.
# ps -o user,pid,cmd -C nginxUSER PID CMDroot 113249 nginx: master process …www-data 113250 nginx: worker process[3] Print the permissions of every component on the way to the file with namei -l. A 644 file is still blocked if **one directory on the path lacks execute (x) for others**.
$ namei -l /srv/app/www/index.htmldrwxr-xr-x root root /drwxr-xr-x root root srvdrwx------ root root appdrwxr-xr-x root root www-rw-r--r-- root root index.htmlHere app is drwx------, so www-data cannot pass through it. Add execute for others; read permission on the directory is not needed to traverse it.
# chmod o+x /srv/appThe wording depends on the request, but both give 403. A direct request such as /index.html logs open() … failed; a request for / that fails while opening the index file logs this:
… [error] … "/srv/app/www/index.html" is forbidden (13: Permission denied), … request: "GET / HTTP/1.1", …[4] If the path is open but the file itself is unreadable, the log is the same open() … failed (13: Permission denied). Check the mode with ls -l.
# ls -l /srv/sitetotal 4-rw------- 1 root root 5 … index.html# chmod 644 /srv/site/index.html[5] directory index of "/srv/files/" is forbidden is not a permission problem. The requested directory has no file the index directive looks for (default index.html) and autoindex is off. Check the file names with ls, then add the file, match the index name, or, where a listing is acceptable, turn on autoindex.
server { listen 80; root /srv/files; autoindex on;}# nginx -t && systemctl reload nginxWith autoindex on, the same request returns 200 and an HTML file listing. The contents become public, so use it only where that is fine.
[6] Servers that use try_files look different. The same permission problem returns 404, and the log level is crit, not error.
# grep 'stat() ' /var/log/nginx/error.log | tail -n 3… [crit] … stat() "/srv/app/www/index.html" failed (13: Permission denied), … request: "GET /index.html HTTP/1.1", …That line came from a server with location / { try_files $uri $uri/ =404; }. When stat() fails, try_files treats the file as missing and moves on to the next candidate. If the last candidate is an internal redirect such as /index.html, the stat() lines repeat until rewrite or internal redirection cycle and the response becomes 500. A 404 with stat() … (13 in the log means steps 3–4.
[7] Request the page again. If you changed only permissions, no reload is needed.
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/200| error.log message | Meaning | Step |
|---|---|---|
open() … failed (13: Permission denied) | file or parent directory permissions | 3, 4 |
"…/index.html" is forbidden (13: Permission denied) | cannot open the index file | 3, 4 |
directory index of "…" is forbidden | no index file and autoindex off | 5 |
[crit] stat() … failed (13: Permission denied) | try_files cannot pass the path (response 404) | 6 |
On SELinux distributions, see the Rocky guide; for installation, see Install Nginx.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.
Check it with the chmod · umask calculator tool ↗ — calculated in your browser.