0
0
NginxDebug / FixBeginner · 4 min read

How to Fix 403 Forbidden Error in Nginx Quickly

A 403 Forbidden error in nginx usually means the server lacks permission to access the requested files. Fix it by ensuring the nginx user has proper read permissions on your web files and that your nginx.conf or site config points to the correct directory with correct root or alias settings.
🔍

Why This Happens

A 403 Forbidden error occurs when nginx tries to serve a file but does not have permission to read it or the directory is misconfigured. This can happen if the file permissions are too restrictive or if the nginx configuration points to a wrong or inaccessible folder.

nginx
server {
    listen 80;
    server_name example.com;

    root /var/www/html/private;

    location / {
        try_files $uri $uri/ =404;
    }
}
Output
403 Forbidden nginx/1.24.0
🔧

The Fix

To fix the 403 error, make sure the nginx user (usually www-data or nginx) has read and execute permissions on the web root and files. Also, verify the root path is correct and accessible. Adjust permissions and ownership as needed.

bash/nginx
sudo chown -R www-data:www-data /var/www/html/private
sudo find /var/www/html/private -type d -exec chmod 755 {} \;
sudo find /var/www/html/private -type f -exec chmod 644 {} \;

# Corrected nginx config
server {
    listen 80;
    server_name example.com;

    root /var/www/html/private;

    location / {
        try_files $uri $uri/ =404;
    }
}
Output
Access granted, website loads without 403 error
🛡️

Prevention

Always set proper ownership and permissions on your web files before deploying. Use chmod 755 for directories and chmod 644 for files as a safe default. Test your nginx config with nginx -t before restarting. Avoid pointing root or alias to directories outside allowed paths.

⚠️

Related Errors

Other common errors include:

  • 404 Not Found: The file or page does not exist at the specified location.
  • 500 Internal Server Error: Server misconfiguration or script errors.
  • 502 Bad Gateway: Backend server is down or unreachable.

Each requires different fixes but checking permissions and config is a good first step.

Key Takeaways

403 Forbidden means nginx lacks permission to access files or directories.
Ensure nginx user owns and can read the web root and files with correct permissions.
Verify nginx config root or alias points to the right folder.
Use nginx -t to test config before restarting the server.
Set directories to 755 and files to 644 permissions as a safe default.