How to Fix 403 Forbidden in Nginx?

What does “403 forbidden” in Nginx mean?

403 forbidden in the Nginx web server means “a requested resource could not be served”. It could be intentional or unintentional. It is not en error but an HTTP status which occurs in the following cases:

  1. The user has blocked the resources or site as a whole.
  2. The user tries to access the file set to access internally.
  3. The user tries to access the resource that has invalid ownership or permissions.
  4. The user tries to access a directory but “autoindex” is set to “off”. (Not recommended)

If you are sure that issue number 1 and 2 is not the case, then definitely there is either file permission issue or for the following resource the autoindex is set to off.

How to fix 403 forbidden Nginx?

403 Forbidden
403 Forbidden (This image is copyrighted to AtulHost)

To get rid of the 403 forbidden problems from Nginx, you need to check for the following.

0. Pre-checks

Correct the index file. In your Nginx configuration file under server block. Whatever file you want to serve by default must be addressed in the index configs.

For example, for PHP file just add index.php after index directive.

index index.html index.htm index.php;

If the above settings are correct then proceed ahead.

1. Get the right Owners & Permissions

99% of the time issue is invalid owners or permissions. Correcting them quickly fixes the problem.

Correct the file Owners

In cases where you host a website or run an application, 403 forbidden Nginx error is common due to invalid ownership of the directory and their files. You can correct it by using below or similar code. In my case the owner was nginx. It can be any user like www-data for the website.

sudo chown -R nginx:nginx [directory_path]
  • “chown” change the owner.
  • “-R” means recursive changes in directory and files.
  • “[directory_path]” exact location like /var/www/html or /usr/share/nginx/html.

Correct the File Permissions

If you give invalid file permission or restricted permissions to a path or their files, it won’t be accessible on the front-end. Make sure the directory has the right permission. 755 and 644 are the most common permissions we use on file servers or while hosting a website.

sudo chmod 755 [directory_path]
  • “chmod” change mode.
  • “775” is directory or file permission.

In majority of the cases above solutions fixes 403 forbidden error occurs in Nginx web server. Still if the error is same check out autoindex settings in your nginx’s configuration file.

2. Set the Autoindex On

Note: This feature is not recommended unless you know what you’re doing.

The auto index is an alternative solution to allow the directory index. Directory index means that if no index file (like index.html, index.htm) is found, the web server will list all of the contents of the directory. For security reasons, the directory index is turned off by default in Nginx.

But in some cases, webmasters need to show directory as an index to allow users to download a file. So how can we set that, let’s check it out…

For root or home location…

location / {
    autoindex on;
    autoindex_exact_size off;
}

For any specific directory…

location /downloads {
    autoindex on;
    autoindex_exact_size off;
}

Once changes is saved restart the Nginx using following command.

systemctl restart nginx

We wish everything is working fine now, in case you still face any issue, let us know in the comments section below.

Published by AtulHost

Atul Kumar Pandey is a creative blogger who loves experiments with the latest tech trends like automation, artificial intelligence, data science, cloud computing, edge computing, hardware as well as networking, and the internet of things.

Leave a comment

Your email address will not be published. Required fields are marked *