Fix 403 Forbidden in Nginx
Nginx returns 403 Forbidden when it will not serve a file or folder. Work through the usual causes, from a missing index file to permissions, deny rules and SELinux.
- Beginner
- Linux VPS
- 5 min read
403 Forbidden means Nginx found the request, decided it is not allowed to serve it, and said so. Unlike a 404, the path usually exists. The cause is nearly always one of a handful of things: no index file, file permissions, a deny rule, or SELinux. The error log tells you which. This guide goes through each in order.
Before you start
- A RunRacks Linux VPS running Nginx.
- SSH access with sudo.
- The URL that returns 403 and the site's
rootfolder.
Read the error log
sudo tail -n 20 /var/log/nginx/error.logRequest the page again and match the new line to a cause:
directory index of "/var/www/example.com/public/" is forbidden→ missing index fileopen() "/var/www/..." failed (13: Permission denied)→ permissions, or on AlmaLinux and Rocky, SELinuxaccess forbidden by rule→ deny rules
If the site has its own error log, check that instead. Find it with sudo nginx -T | grep error_log.
Missing index file
When a visitor asks for a folder such as /, Nginx looks for a file listed in index. If none exists and directory listing is off, the result is 403.
Check what the site expects:
sudo nginx -T | grep -E "^\s*(root|index)"And what is in the folder:
ls -la /var/www/example.com/public/Common causes:
- The files were uploaded one folder too deep, for example into
public/site/. Move them up or changeroot. - The site is PHP but
indexonly listsindex.html. Useindex index.php index.html;. - For apps such as WordPress or Laravel,
try_filesshould end in/index.php?$args, not$uri/alone. See Fix Nginx 404 errors with try_files.
Owner and permissions
The Nginx worker runs as www-data on Ubuntu and Debian, and as nginx on AlmaLinux and Rocky. It needs:
- read permission on every file it serves;
- execute (search) permission on every folder in the path, all the way from
/.
The second point is the one people miss. namei shows the whole chain at once:
namei -l /var/www/example.com/public/index.htmlEvery folder line should show x for others or for the Nginx group, for example drwxr-xr-x. A home folder such as /home/deploy is often drwx------ or drwxr-x---, which blocks Nginx even if the files inside are readable.
Safe defaults for a web root:
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;If your files must live under a home folder, give it search permission without listing:
chmod 711 /home/deployDo not fix 403s with chmod -R 777. It lets any process on the server change your site's files and makes a compromise much worse.
Deny rules
access forbidden by rule means an allow/deny directive matched. Find them:
sudo nginx -T | grep -nE "^\s*(allow|deny)"Typical surprises:
- An admin location limited to an IP that has since changed.
- A dotfile rule like
location ~ /\.that also blocks/.well-known/. - Behind Cloudflare,
allowrules compare against Cloudflare's IPs unless the real IP module is set up.
See Allow or block IP addresses in Nginx.
SELinux contexts
On AlmaLinux and Rocky Linux, permissions can look perfect and still give "Permission denied". SELinux only lets Nginx read files labelled httpd_sys_content_t. Files moved (rather than copied) from a home folder keep their old label.
Check the label:
ls -Z /var/www/example.com/public/index.htmlIf it shows user_home_t or default_t, restore the right labels:
sudo restorecon -Rv /var/www/example.comFor a web root outside /var/www, add a rule first, then restore. The full process is in Fix Nginx permission denied caused by SELinux.
Directory listing (autoindex)
If you want visitors to browse a folder of downloads, turn on listings for that location only:
location /downloads/ {
autoindex on;
}Leave it off everywhere else. A listing exposes every file name in the folder.
Apply and retest
Permission and SELinux fixes take effect immediately. Config changes need a reload:
sudo nginx -t && sudo systemctl reload nginxCheck it works
curl -I https://example.com/You want 200. Test as the Nginx user to confirm it can read a file:
sudo -u www-data cat /var/www/example.com/public/index.html > /dev/null && echo readableUse -u nginx on AlmaLinux and Rocky. If this prints readable but Nginx still returns 403, the cause is a rule or SELinux, not file permissions.
Troubleshooting
- 403 on the home page only, other pages fine: a missing index file or
indexdirective. Check the root folder contents. - 403 on uploaded images only: the upload folder or files have restrictive permissions set by the app. Fix them and the app's upload umask.
- 403 after moving files on AlmaLinux or Rocky: SELinux labels. Run
restorecon. - 403 on `.well-known` during certificate renewal: a dotfile rule. Exclude
.well-knownfrom it. - 403 from Cloudflare, not Nginx: the page is branded by Cloudflare. Check your Cloudflare firewall rules instead.
Next steps
- Permissions for WordPress in particular: WordPress file permissions.
- Pages missing instead of forbidden: Fix Nginx 404 errors with try_files.
- Host your sites on a Linux VPS.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Troubleshooting
Fix Nginx 413 Request Entity Too Large
Nginx returns 413 Request Entity Too Large when an upload is bigger than client_max_body_size. Raise the limit for one site or the whole server, match PHP's limits, and test.
- Beginner
- Linux VPS
- 5 min read
Troubleshooting
Is your ISP or router blocking port 12230? How to tell
Work out whether your internet provider, home router, parental controls or a proxy is blocking RDP port 12230, and collect the details support needs to help.
- Intermediate
- Windows VPS · Admin RDP
- 5 min read
Troubleshooting
Fix "Your credentials did not work" in Remote Desktop
Fix rejected RDP logins: username format, the right password, stale saved credentials, account lockout and Remote Desktop permissions.
- Beginner
- Windows VPS · Admin RDP
- 3 min read