Nginx 403 Forbidden Error: Causes and How to Fix It

nginx 403 forbidden - custom-ngx403-featured.png

A 403 Forbidden from Nginx means the server understood the request but refused to serve it. Unlike a 502 Bad Gateway,
the backend is not the problem: Nginx itself decided not to return the file. The cause is almost always one of six things, and the error log tells you which.

Quick answer: run sudo tail -n 20 /var/log/nginx/error.log. Permission denied means file permissions or SELinux; directory index … is forbidden means a missing index file.

Read the Error Log First

Every 403 leaves a line in the error log that names the cause. Reproduce the error, then read the newest lines.

basherror log
sudo tail -n 30 /var/log/nginx/error.log
sudo tail -f /var/log/nginx/error.log      # then reload the page
textthe two most common messages
open() "/var/www/site/index.html" failed (13: Permission denied)
directory index of "/var/www/site/" is forbidden
Start with the error log
It names the cause

Fix File and Directory Permissions

The Nginx worker user (www-data on Debian/Ubuntu, nginx on RHEL/AlmaLinux) must be able to read files and execute (traverse) every directory in the path, all the way from /.

bashcheck the whole path
namei -l /var/www/site/index.html
ps -o user= -C nginx | sort -u
bashtypical fix
sudo chown -R www-data:www-data /var/www/site
sudo find /var/www/site -type d -exec chmod 755 {} +
sudo find /var/www/site -type f -exec chmod 644 {} +
WarningDo not fix a 403 with chmod 777. It hides the real problem and makes files writable by everyone.
Permissions
Read files, traverse dirs

Site Files in a Home Directory

Home directories are often 750 or 700, so the Nginx user cannot pass through them even if the site files themselves are readable. Move the site to /var/www or allow traversal.

bashallow traversal (or move the site)
ls -ld /home/anil
sudo chmod o+x /home/anil
Files under /home
Parent directory blocks access

Missing Index File or Autoindex Off

When a URL points to a directory, Nginx looks for the files in index. If none exists and autoindex is off, it returns 403 directory index is forbidden.

nginxserver block
root /var/www/site;
index index.php index.html index.htm;
# autoindex on;   # only if you really want a file listing
TipFor PHP apps, make sure index.php is in the list and the PHP location block exists.
Missing index
index and autoindex

Wrong root Path

A typo in root or a root placed inside a location block can point Nginx at a directory that exists but has no index, which also produces 403.

bashconfirm the effective config
sudo nginx -T | grep -nE 'server_name|root|index'
ls -la /var/www/site
root path
Points at the wrong folder

SELinux on RHEL, AlmaLinux and Rocky

If permissions look right but the log still says Permission denied on a RHEL-family server, SELinux is probably blocking files that lack the web content label.

bashcheck and fix SELinux labels
getenforce
ls -Z /var/www/site
sudo semanage fcontext -a -t httpd_sys_content_t '/var/www/site(/.*)?'
sudo restorecon -Rv /var/www/site
sudo ausearch -m avc -ts recent | tail
NoteBackground on enforcing mode is in the SELinux and AppArmor guide.
SELinux context
httpd_sys_content_t

deny Rules and IP Restrictions

A deny all, an allow list that does not include your IP, or a rule blocking dot-files will return 403 by design. Search the config for them.

bashfind access rules
sudo grep -rnE 'deny|allow|return 403' /etc/nginx/
nginxexample: admin area for one IP
location /admin/ {
    allow 203.0.113.10;
    deny all;
}
deny / allow rules
Intentional blocks

Test, Reload and Verify

After each change, test the configuration, reload Nginx and request the page again with curl so you see the status code directly.

bashverify
sudo nginx -t && sudo systemctl reload nginx
curl -I https://example.com/
TipAutomate the test-and-reload step with the Nginx config test script.
Test and reload
nginx -t, reload, curl

Quick Reference

What causes 403 Forbidden in Nginx?

Usually file or directory permissions, a missing index file with autoindex off, SELinux labels, a wrong root path, or a deliberate deny rule. The error log shows which.

How do I fix Nginx 403 permission denied?

Make sure the Nginx user can read the files and traverse every directory in the path (755 for directories, 644 for files), then check SELinux on RHEL-family systems.

What does ‘directory index is forbidden’ mean?

The URL points to a directory with no file listed in the index directive, and autoindex is off. Add an index file or fix the index line.

Should I use chmod 777 to fix a 403?

No. It weakens security and rarely fixes the real cause. Use correct ownership and 755/644 permissions.

Which user does Nginx run as?

www-data on Debian and Ubuntu, nginx on RHEL, AlmaLinux and Rocky. Check with ps -o user= -C nginx.

Cheat Sheet

  • tail /var/log/nginx/error.log, namei -l /path/file, nginx -T
  • chmod 755 dirs / 644 files, restorecon -Rv /var/www, nginx -t && systemctl reload nginx

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.