How To Fix Nginx 403 Forbidden Error When Serving Static Files?

Published September 8, 2024

Problem: Nginx 403 Forbidden Error for Static Files

The Nginx 403 Forbidden error happens when trying to serve static files, blocking access to website resources. This problem can affect a website's function, as it stops users from seeing images, stylesheets, or other static content.

Diagnosing the Problem

Checking Nginx configuration

To fix the Nginx 403 Forbidden error, review your Nginx configuration. Check the location block for static files in your nginx.conf or server block file. This block sets how Nginx handles requests for static content.

Example of a location block:

location /static {
    root /var/www/mysite;
    try_files $uri =404;
}

Verify if the root or alias directive is set correctly. The root directive sets the base directory for requests, while the alias directive sets a different path for the specified location.

Tip: Troubleshooting Nginx Configuration

Use the nginx -t command to test your Nginx configuration for syntax errors. This command checks the configuration files and reports any issues, helping you identify and fix problems quickly.

Examining file and directory permissions

Next, check the file and directory permissions of your static content. Use the ls -l command to see the ownership and permissions of files and directories.

Example:

ls -l /var/www/mysite/static

Confirm that the Nginx user (often www-data or nginx) has read access to the files and execute permission for the directories. The execute permission on directories lets the user access the directory's contents.

To find the Nginx user, look in your nginx.conf file for the user directive or use this command:

ps aux | grep nginx

If the permissions are wrong, you may need to change them using the chmod and chown commands.

Solutions to Fix the 403 Forbidden Error

Adjusting Nginx user configuration

To fix the 403 Forbidden error, you may need to change the user directive in your nginx.conf file. This directive sets which user Nginx runs as, affecting its access to files and directories.

  1. Open your nginx.conf file (usually in /etc/nginx/):
sudo nano /etc/nginx/nginx.conf
  1. Find the user directive near the top of the file. It might look like this:
user www-data;
  1. If needed, change the user to match the owner of your static files. For example:
user your_username;
  1. Save the file and exit the editor.

  2. Restart Nginx to apply the changes:

sudo systemctl restart nginx

Tip: Verify Nginx User

To check which user Nginx is running as, use the command:

ps aux | grep nginx

This will show you the processes running Nginx and the user associated with each process.

Correcting file and directory permissions

Adjust the ownership and permissions of your static files to allow Nginx to access them:

  1. Change the ownership of static files to the Nginx user:
sudo chown -R nginx:nginx /path/to/your/static/files
  1. Set read and execute permissions:
sudo chmod -R 755 /path/to/your/static/files

This command gives read and execute permissions to the owner, and read-only access to others.

Updating Nginx location block

Review and fix the root or alias directive in your Nginx location block:

  1. Open your server block configuration file:
sudo nano /etc/nginx/sites-available/your_site
  1. Find the location block for static files and check the root or alias directive:
location /static {
    root /var/www/your_site;
    # or
    alias /path/to/your/static/files;
}
  1. Make sure the path to your static files is correct.

  2. Save the file and exit the editor.

  3. Test the Nginx configuration:

sudo nginx -t
  1. If the test is successful, reload Nginx:
sudo systemctl reload nginx