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

By RunRacks Team · Published

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 root folder.

Read the error log

Terminal
sudo tail -n 20 /var/log/nginx/error.log

Request the page again and match the new line to a cause:

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:

Terminal
sudo nginx -T | grep -E "^\s*(root|index)"

And what is in the folder:

Terminal
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 change root.
  • The site is PHP but index only lists index.html. Use index index.php index.html;.
  • For apps such as WordPress or Laravel, try_files should 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:

Terminal
namei -l /var/www/example.com/public/index.html

Every 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:

Terminal
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:

Terminal
chmod 711 /home/deploy

Deny rules

access forbidden by rule means an allow/deny directive matched. Find them:

Terminal
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, allow rules 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:

Terminal
ls -Z /var/www/example.com/public/index.html

If it shows user_home_t or default_t, restore the right labels:

Terminal
sudo restorecon -Rv /var/www/example.com

For 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:

NGINX
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:

Terminal
sudo nginx -t && sudo systemctl reload nginx

Check it works

Terminal
curl -I https://example.com/

You want 200. Test as the Nginx user to confirm it can read a file:

Terminal
sudo -u www-data cat /var/www/example.com/public/index.html > /dev/null && echo readable

Use -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 index directive. 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-known from it.
  • 403 from Cloudflare, not Nginx: the page is branded by Cloudflare. Check your Cloudflare firewall rules instead.

Next steps

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.

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