Nginx 403 forbidden for all files

Nginx 403 forbidden for all files

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Nginx 403 Forbidden for All Files

When Nginx returns a 403 Forbidden error immediately upon requesting any file, even static assets like HTML or CSS, the root cause is almost never an application logic issue. It is strictly a filesystem access control problem. The most common misconception in these scenarios is assuming that setting permissions on individual files within a directory is sufficient if the parent directory itself remains inaccessible to the web server process.

In the specific case of Nginx running under the www-data user, the failure occurs because the web server cannot traverse the path from root to the application directory. If the parent directory lacks execute (x) permissions for the www-data user, Nginx is effectively locked out before it even checks the file permissions. This results in the error open() "..." failed (13: Permission denied), regardless of whether the target file has 777 permissions.

The Importance of Directory Traversal Permissions

The core principle here is that read (r) and write (w) permissions on a directory do not grant access to its contents without execute (x) permission on every parent directory leading up to them. Execute permission on a directory allows the process to enter it and list its contents or traverse through it.

If your application resides at /home/demo/sites/example.com/public_html, Nginx must have execute permission on:

  • /
  • /home
  • /home/demo
  • /home/demo/sites
  • /home/demo/sites/example.com
  • /home/demo/sites/example.com/public_html

Even if the final file, index.html, is owned by www-data:www-data with mode 777, Nginx will fail to open it if any directory in that chain denies execution. The standard fix involves ensuring the group ownership matches or setting the world-executable bit on all parent directories involved in the path traversal.

You can verify this quickly using ls -ld on the parent directories. Look specifically for the permission string. If you see a dash - instead of an x in the third column (the execute bit) for any directory owned by a group that www-data is not part of, or if the permissions are set to 750 or 755 but the user lacks access, you will encounter this specific error.

Configuring Correct Permissions

To resolve this, you typically need to adjust the ownership or permissions of the web root and its parents so that www-data can traverse them. A robust approach involves ensuring the application directory is owned by the web server user, while parent directories in a shared home environment (like /home/demo) are often configured with setgid and specific group permissions.

If you are on a system where /home is restricted to root or specific users, you cannot simply change permissions to 777 globally without creating security risks. Instead, the directory structure should be planned such that:

  1. The web server user (www-data) owns the application files.
  2. Parent directories allow execute access for the group or world.

For example, if /home/demo is owned by demo:demo with permissions 755, and www-data is not a member of the demo group, www-data cannot enter /home. In this scenario, you would need to either add www-data to the demo group and set the setgid bit (2775) on the directories within /home/demo, or ensure that all parent directories explicitly allow world execution.

Verifying FastCGI Configuration

While filesystem permissions are the primary suspect for a blanket 403 error, it is worth noting that misconfigured fastcgi_param values can also lead to confusion. In your configuration snippet, you have set:

root /home/demo/sites/example.com/public_html;

This tells Nginx where static files live. However, if the PHP handler fails to locate the script due to SCRIPT_FILENAME miscalculations, you might see 500 errors rather than 403s. A 403 on all requests, including non-PHP files, isolates the issue strictly to the Nginx daemon's ability to read the filesystem.

Ensure that your root directive points to a directory where www-data has execute permission. If you are using a chroot environment or SELinux is enabled (common on CentOS), additional context switching labels must match. On CentOS specifically, if SELINUX=enforcing, the default policies might block www-data from accessing /home/demo regardless of Unix permissions. You would need to add a type transition rule in the SELinux policy for your custom application path.

Summary of Resolution Steps

To fix the issue:

  1. Check ls -ld on every directory in the path from root to public_html.
  2. Ensure every directory has at least rwx for the user or group that owns it, and rx for any other users if they need to traverse.
  3. If using a shared home directory like /home/demo, add www-data to the owning group and set the setgid bit on directories (chmod g+s /home/demo).
  4. Restart Nginx after making changes: service nginx restart.

Once the execute bits are correctly applied to the parent directories, Nginx will be able to traverse the path, read the static files, and serve them successfully, resolving the permission denied errors for both PHP and non-PHP content.

Our network