Website Vidya
DeploymentStep-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
Website Vidya TeamLead 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.

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:

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.

Written by

Website Vidya Team

Technology educator and researcher at Website Vidya.

Discussion

0
Share your thoughts, ask a question, or suggest a tip...
Write →

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Deployment•

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
Deployment•

How to Install PHPMyAdmin on Ubuntu: 4 Simple Steps

If you’re working with MySQL or MariaDB on Ubuntu, managing databases purely from the command line can quickly feel overwhelming. That’s where phpMyAdmin comes in. PHPMyAdmin is a web-based application that provides a Graphical User Interfa...

RR
Rajesh Rai
Read More
Deployment•

25 Best Linux Commands Every Sysadmin Should Know

If you’ve spent any time managing Linux servers, you already know this truth: the command line is not optional. It’s where real troubleshooting happens, where performance issues are understood, and where outages are prevented before users e...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Snapd Consuming High CPU / RAM on Linux

If you’ve ever opened your system monitor and noticed snapd quietly eating up your CPU or RAM, you’re not alone. Many Linux users—especially on Ubuntu and Ubuntu-based distributions—eventually run into this issue and wonder what’s going on ...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Broken Packages in Ubuntu: 7 Simple Ways

If you’ve been using Ubuntu for a while, chances are you’ve run into package-related issues at least once. A failed update, an interrupted installation, or conflicting dependencies can easily leave your system in a messy state. When that ha...

RR
Rajesh Rai
Read More