How to fix Nginx 403 Forbidden Error
1 min read

How to fix Nginx 403 Forbidden Error

While there are many ways nginx 403 forbidden error can occur, we will discuss two of the common solution in this article.

First of all, we should log in to our server via ssh.

i.e ssh root@1.1.1.1

Once, we’re at our home directory, we can start trying the following solutions:

Config is pointing to the wrong index file:

To check why our server cannot find the index file, go to the root of our website.

cd /var/www/html/

in this directory, I have home.php (it can be different for you)

Let’s compare it to our configuration file for nginx.

vim /etc/nginx/site-available/default

Our config will look like this:

server {
	listen 80;
	listen [::]:80;
	server_name example.com;
	root /var/www/html;
	index index.html index.php;
	location ~ \\.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}
}

Here, everything looks great except the index because it is not pointing to home.php.

In this case, we can either rename our home.php file to index.php or add home.php to the list.

# add home.php to index list
index index.html index.php home.php;

# rename home.php to index.php
cd /var/www/html/;
mv home.php index.php;

Permission Issue:

Another potential cause of the 403 error can be incorrect permissions. We need to make sure that all the files/directories at the root of our website have the correct permissions. The recommended permission for the files and folders are 644 and 755 respectively.

To check our current permission:

ls -la /var/www/html;

drw-r--r--   2 root  root       4096 10 Oct 00:01 .

In the above example, we do not have to execute permission for the html directory.

To fix this issue we can either update the permission manually or use the following command to apply the correct scheme to all the files/folders:

# for folders
find /var/www/html -type d -exec chmod 755 {} \;

# for files
find /var/www/html -type f -exec chmod 644 {} \;

I hope these two solutions can fix your 403 forbidden issue. If you have any questions/suggestions feel free to tweet @ravensmove