Website Vidya
Linux & ServersStep-by-Step Guide

How to Fix 403 Forbidden Error in NGINX Ubuntu

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...

RR
Rajesh RayLead Systems Educator
Published:
5 min read
How to Fix 403 Forbidden Error in NGINX Ubuntu
AI Executive SummaryCORE CIS AI

5 min read

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...

Key Highlights
  • About 403 Error
  • 4 Ways Fix 403 Forbidden Error
Grounded in full engineering runbookCORE CIS Ai Engine

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.

Advertisement

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.

Advertisement

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:

Advertisement
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:

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.

RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

PA
AT
AB
+5
Discourse & Reader Insights (8)
PA
ParminderFeb 18, 2021

Your article is very helpful to fix out the issue that I faced in my ubuntu 16.0 "<strong>Bionic Beaver</strong>" operating system. Thanks for this lovely article.

Rajesh Ray(Author reply)• Feb 25, 2021

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. :)

AT
Ashish ThakurMar 22, 2021

What a wonderful article. Keep up the great work.

AB
AbhimandMar 22, 2021

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.

HK
Heena KhanJun 15, 2021

Thanks for this fabulous article. You saved my day.

Rajesh Ray(Author reply)• Jun 15, 2021

That means a lot, Heena! I'm glad to hear that you find the article useful.

RO
RoyalJun 18, 2021

Great article! Directly permission was incorrect. You saved my day. Thanks and keep up the great work.

Rajesh Ray(Author reply)• Jun 18, 2021

I’m glad to know that you found it useful.

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Linux & Servers•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout