The 403 Forbidden error is the most common error encountered while working on Nginx web server. But most of the time, it is not related to Nginx itself. 403 Forbidden error means that you don’t have permission to access certain directory or a web page. This error can be caused due to many reason. And in this article we will identify the source of the error and then we will learn how to Fix it.
About 403 Error
Let’s understand “403 Forbidden” error in details.
“403 Forbidden” is an error which indicates that you have requested for something that NGINX cannot deliver. This error is actually an HTTP status code which simply means that the web server has received and understood the request which is made, but cannot process further.
4 Ways Fix 403 Forbidden Error
1. Wrong Directory or File permissions
Incorrect file permissions are one of the most common cause of this “403 Forbidden” error. In case of NGINX, the standard permission settings for directories and and file are 755 and 644 respectively. Moreover, the NGINX user also needs to be the owner of the directory and files.
Identify the NGINX User
To begin, we first need to identify the NGINX user. To check the user, run:
ps -ef | grep nginx
Output:
userwv+ 6016 26683 0 19:28 ttyS0 00:00:00 grep --color=auto nginx root 26734 1 0 Mar20 ? 00:00:00 nginx: master process /usr/sbin; www-data 26739 26734 0 Mar20 ? 00:01:53 nginx: worker process
In our case, we can identify the NGINX worker processe in the third row of the first column.
We can see that the NGINX worker process is running as the user www-data
.
Set File Ownership
Since we already know that the standard permission for the directory in case of Nginx is 755 and that of the file is 644. We need to set the file ownership.
For example, if the root folder of your website is /var/www/html/example.com/public_html/
, run:
sudo chown -R www-data:www-data /var/www/html/example.com/public_html/
Set Directory Permissions
Now we need to set 755 permissions on each directory in this location.
sudo chmod 755 [directory name]
In our case, we need to set permission on example.com directory. To achieve that, run:
sudo chmod 755 example.com
Now, navigate to the root directory of the website by running:
cd example.com
Now change the file permission to 644 by running:
sudo chmod 644 *
2. Incorrect Index File
The “403 Forbidden” error can appear if the index file is not set properly.
Navigate to the NGINX configuration file and check whether you have updated correct index file or not. To do that, open our NGINX configuration file for example.com, by running:
sudo vim /etc/nginx/sites-available/example.com
For example, if you’re running a WordPress based website, inside the server block your index should be:
index index.php;
If you’re running an HTML based website, your default file must contain .html
extension. e.g.,
index index.html;
Chances are there that you might have forgotten to add this line inside the server
block. If you have included this line of code, you must check whether you have spelled it correctly or not.
Moreover, you also must keep in mind that these file names are case-sensitive. If the default file name is index.html but the file is named mentioned in NGINX configuration is Index.html, this will throw “403 Forbidden” error.
If you are running some web application that NGINX is not able to recognize, you can edit configuration file and add the file extension.
For example, if you’re running a python based application, you can add index.py to the list of recognized index files:
index index.py;
In case you’re running a web application that uses multiple programming languages, you can add:
index index.php index.html index.py;
Now, update the changes in the configuration file and restart NGINX by running:
sudo nginx -s reload
or
sudo service nginx restart
Autoindex
It is an alternative solution which is useful if you don’t have any index file or no index file is found by NGINX.
The the server will scan and list all of the contents of your website’s root directory by autoindex
method. But for security reasons, the directory index is turned off in NGINX by default.
You can turn on this feature by just adjusting a few lines of code. You just need to turn on and turn of the autoindex
and autoindex_exact_size
respectively.
autoindex on; autoindex_exact_size off;
Now you can add these configurations to the location block. The final result will look like:
location / { [pre-existing configurations, if applicable] autoindex on; autoindex_exact_size off; }
To activate the directory indexing for some other directories, you can add the forward slash (/) and then the name of the directory. For example:
location /some_directory { autoindex on; autoindex_exact_size off; }
Now, save the changes and reload the NGINX.
sudo nginx -s reload
3. Directory restrictions by IP
Inside your nginx.conf
file, check if you have applied allow/deny rule that may be blocking your network. For example:
location / { # block Oxygen's Computer. deny 192.168.1.1; # allow anyone else in 192.168.1.0/24 allow 192.168.1.0/24; # drop rest of the connections deny all; }
4. No index files
This could be a silliest mistake if you don’t have any file name as ‘index (index.php, index.html, index.py)’ inside the root directory of your website. This could also be a reason that your website is throwing a 403 Forbidden Error.
Additional Resources
You might also want to check some other fixes:
- Error Establishing a Database Connection in WordPress Fix
- Fix 502 Bad Gateway Nginx Error in Ubuntu
- Fix: 504 Gateway Timeout Nginx Error
- PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)
- Your PHP installation appears to be missing the MySQL extension which is required by WordPress
I hope you these 4 ways will help you to Fix 403 Forbidden Error.
Which of these ways helped you get rid of this error? Let us know in the comment section below.
Your article is very helpful to fix out the issue that I faced in my ubuntu 16.0 “Bionic Beaver” operating system. Thanks for this lovely article.
I am glad to know that. Feel free to reach out to me for any server-related issues. I will be happy to help and resolve the issue. 🙂
What a wonderful article. Keep up the great work.
It took me a whole day to understand this annoying error. Thanks for this solution that helped get rid of this in no time. Great work! Looking forward to more such resources.
Thanks for this fabulous article. You saved my day.
That means a lot, Heena! I’m glad to hear that you find the article useful.
Great article! Directly permission was incorrect. You saved my day. Thanks and keep up the great work.
I’m glad to know that you found it useful.