How to fix 403 Forbidden Nginx error
Tags: nginx, nginxerror
Published: 2023-11-19 05:23:54

> 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tpher3l637jtsi48vsg.jpg)
Fixing nginx 403 forbidden error can be frustrated and time consuming process. If you know exactly what is nginx 403 forbidden error and  reasons behind it, you can fix this error in time without any delay and get back your application running state.

## **What is Nginx 403 forbidden Error?**
When client or user requested for the certain resource the nginx understood the request but do not authorise to access the requested resource. In general words user or client does not have enough privilege to access the requested resource. so Nginx responded back with http response code 403 with message forbidden 

## **What are the reasons for nginx 403 forbidden error.**
Reasons for the 403 forbidden Nginx error in nginx is listed below

1. Insufficient permission
2. Incorrect document root for the project
3. Missing index with index file name
4. directory listing is disabled 
5. Ip based restriction in nginx configuration
6. WAF policy restrictions
7. Request methods restrictions
8. Authentications Issues
9. Browser Cache and cookies 
10. Running nginx with wrong user 
11. SELinux and Apparmor policy
12. Wrong and Incorrect configuration for nginx.

In this [comprehensive guide](https://devopsflux.com/how-to-fix-nginx-403-forbidden-error/) we will discuss all the possible ways to fix the nginx 403 forbidden error

## **1. Insufficient permission**
Most common error for 403 error is insufficient permission to nginx , nginx should have adequate  permission for read and execution as well as ownership of directory and file 
if you find incorrect permission and you can fix on your project requirement by using below example commands

```
chmod  -R 755 /usr/share/nginx/html/appl
chown -R nginx:nginx /usr/share/nginx/html/appl
or if the user is www-data then
chown -R www-data:www-data /usr/share/nginx/html/appl
```

## **2. Incorrect document root for the project**
Giving a wrong document root for the application also results in 403 forbidden error. In order to fix the this issue always make sure that you are using correct document root for the application.
for example 

```
Incorrect document root
server {
  location / {
  
          # incorrect path for the project directory 
          root /usr/share/projects;
          index  index.html index.htm;
    }
``` 
correct document root 
```
Correct document root
server {
  location / {
  
          # correct path for the application directory 
          root /usr/share/projects/application;
          index  index.html index.htm;
    }
```

## **3. Missing index with index file name**
Missing an index file with index file name is very common issue , sometimes we give empty directory which does not contain any files. To fix this ensure that you are giving the index with correct index file name, it is not necessary that index file name should start with index.html index.php index.htm it can start with any name.

## **4. Directory listing is disabled**
Sometimes we required to display directory content in browser but due to incorrect configuration or **autoindex off directive** , we are getting 403 forbidden. By  default directory listing is disable for any directory in Nginx. To fix this we have to use **autoindex on diective** in configuration.

Incorrect configuration
```
Incorrect configuration for directory listing
server {
  location / {
  
          #give path for the  directory 
          root /usr/share/projects/app;
          autoindex off;
    }
}
```
Correct configuration

```
Correct configuration for directory listing
server {
  location / {
  
          #give path for the  directory 
          root /usr/share/projects/app;
          autoindex on;
    }
}
```

## **5. IP based restriction in nginx configuration**
Based on project requirement we are restricting the request from certain IP and CIDR block. So for this we are using IP blacklisting and whitelisting. If you are using **deny directive** in nginx configuration it is for blacklisting, If you are using **allow directive** in nginx configuration it is for whitelisting or allowing the IP, if the  client IP is not in allow list it can results 403 error. In order to fix this issue please add IP in  allow directive for whitelisting the client IP or CIDR block requested by client.

```
server {
    allow 192.x.x.x;
    allow 192.x.x.x/x;
    deny all;
}
```

## **6. WAF policy restrictions**
In order to protect the application from any kind of web based attack or any attack organizations apply firewall. one of the firewall is for  the web based application that is called WAF(Web Application Firewall). Policies is being applied for the request validation and Application access. if the URL is not whitelisted or request payload criteria is not defined in WAF policy , it can results in Nginx 403 Forbidden. Nginx OSS has mod security firewall and Nginx Plus has App protect WAF. so make sure you are using correct policy for your applications. 

## **7. Request methods restrictions**
For protection of application only certain http methods are allowed such as GET, POST or PUT, Please ensure that if you want access certain application or API , use adequate request type and also adjust your configuration based on project or application requirement.

## **8. Authentications Issues**
Using the wrong credentials for authentication for the protected resource, will return in 403 error. Use proper credentials to access protected resource.

## **9. Browser Cache and cookies**
Even after restarting nginx and checking all troubleshooting methods, we are still getting 403 forbidden. To tackle this problem we have to clear browser cache and clear the cookies, try from different devices to check this.

## **10. Running nginx with wrong user**
Running the nginx with a user which does not have proper privilege to access certain resource. To fix this issue run the nginx with user which have proper and adequate privilege.

## **11. SELinux and Apparmor policy**
If you are using SELiux and Apparmor ,due to policy of these two tools nginx is not be able to access certain directory or files.
Please check the policy of these two tools and adjust the policy based on your requirements.

## **12. Wrong and Incorrect configuration for nginx.**
Wrong and incorrect configuration for nginx for your application can be the reasons of 403 forbidden error in nginx. Please review your configuration of nginx for your application. If you are not able to figure it out take help of some nginx expert and fix your issues.