Fix Nginx 403 Forbidden Error On Ubuntu

Fix Nginx 403 Forbidden Error On Ubuntu

Encountering a 403 Forbidden error means your Nginx server understands who's asking, but flat-out refuses to serve the requested content. When you're running Nginx on Ubuntu, this usually comes down to missing file permissions, a missing default index file, or misconfigured server block root directives. Let's fix it without guessing.

Check Your Document Root and Index Files

The most common culprit behind an unexpected 403 error is a missing index.html or index.php file inside your defined web root directory. If Nginx looks inside /var/www/html and finds no default file to serve, and autoindex is turned off, it drops a 403 error instead of listing the directory contents.

Run this command to check if your index file actually exists in the web directory:

ls -la /var/www/html/

If the directory is empty or lacks the file specified in your Nginx configuration, create a temporary test page: Additional analysis by Bloomberg explores similar views on this issue.

echo "Hello World" | sudo tee /var/www/html/index.html

Next, open your Nginx server block configuration file—usually located in /etc/nginx/sites-available/default—and verify that the index directive includes your file types:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html index.htm index.php;
}

Test your configuration for syntax errors and reload the service:

sudo nginx -t
sudo systemctl reload nginx

Correct File and Directory Permissions

Linux file permissions can be strict. If the user running the Nginx worker processes doesn't have read access to your web files, you'll see a 403 error instantly. By default on Ubuntu, Nginx runs under the user www-data.

Check the ownership of your web directory:

💡 You might also like: this article
ls -ld /var/www/html

You want to make sure the files belong to the correct user and group, and that permissions allow traversal. Set standard secure permissions for web directories (755) and files (644) by running these commands:

sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;

Assign ownership to the www-data user and group so Nginx can read the assets properly:

sudo chown -R www-data:www-data /var/www/html

If you host multiple sites, check out the official Nginx Beginner's Guide for best practices on managing virtual server blocks and directory structures securely.

Inspect SELinux and AppArmor Policies

Ubuntu ships with AppArmor enabled by default, while other distributions use SELinux. If security modules restrict Nginx from reading files outside standard paths, you'll hit a 403 wall even if standard Linux file permissions look completely fine.

Check if AppArmor is blocking Nginx operations by inspecting the system log:

sudo journalctl -u nginx --since "1 hour ago"

If AppArmor blocks access to a custom directory path you created outside /var/www, you'll need to update your AppArmor profiles or place your web files inside standard allowed directories. For detailed security policy management, refer to the Ubuntu AppArmor Documentation.

Verify Nginx Error Logs for Clues

Never guess when troubleshooting web servers. Nginx logs every permission denial and configuration mismatch directly to disk. Tail the error log in real-time while trying to load your site in a browser:

sudo tail -f /var/log/nginx/error.log

If you see an entry mentioning directory index of "/var/www/html/" is forbidden, it confirms you're missing an index file or have autoindex disabled. If you see Permission denied, it points directly back to file ownership or SELinux/AppArmor restrictions. For advanced log parsing and error handling strategies, consult the DigitalOcean Nginx Troubleshooting Guide.

Frequently Asked Questions

Why does my Nginx return a 403 error for only one specific folder?

This usually happens because that specific subdirectory lacks read permissions for the www-data user, or it's missing an index file while directory listing is disabled in your Nginx config.

Can a misconfigured proxy_pass cause a 403 error?

Yes. If you're using Nginx as a reverse proxy, a 403 error from the upstream application server (like Node.js or Apache) will be passed back to the client unless configured otherwise.

Is it safe to set web directory permissions to 777?

No. Setting directories to 777 gives write access to anyone on the system, creating a massive security vulnerability. Stick to 755 for directories and 644 for files.

Where can I find the main Nginx configuration file on Ubuntu?

The primary configuration file is located at /etc/nginx/nginx.conf, while individual site definitions live inside /etc/nginx/sites-available/.

EP

Elena Parker

Elena Parker is a prolific writer and researcher with expertise in digital media, emerging technologies, and social trends shaping the modern world.