Error 403 Forbidden

How to fix the Error 403 Forbidden on Nginx web server.

The 403 Forbidden error is caused by:

  • The Index module of Nginx is disabled
  • Missing index file such as index.html or index.php

Fix Error 403 Forbidden

Create the index file

Create the index file in your web site project folder:

cd /var/www/mydomain/public

Create the file and save it:

vi index.html

Point to the index file

Edit the nginx.conf file to point to your index.html or index.php file:

server {
    location / {
        index index.html index.php;
    }
}

The index parameter will first serve the index.html file if it's available. If not, it would server index.php.

If you use the try_files paramter for WordPress, Laravel and other PHP-based frameworks, you can use:

server {
    location / {
        try_files $uri /index.html index.php;
    }
}

Incorrect permissions

The 403 Forbidden may also be caused by incorrect permissions or incorrect ownership (ie. the web site folder is not owned by the same username that serves Nginx).

Go to your web site project folder and set the correct permissions:

  • 755 for directories
  • 644 for files

Fix folder ownerships

Find the username that runs Nginx:

ps aux | grep nginx

Set the correct ownerships of the web site folder:

sudo chown -R www-data:www-data /var/www/mydomain

Replace www-data with the correct username and group.

Fix folder permissions

Web site folder permissions may also cause the 403 Forbidden.

chmod 755 /var/www/mydomain/public

Set Autoindex On

You can set the AutoIndex parameter on to prevent the 403 Forbidden error from showing since, by default, Nginx web server disables directory listing.

The autoindex parameter can be set from the nginx.conf file:

server {
    location / {
        autoindex on;
        autoindex_exact_size off;
    }
}

Info: It's recommend to not use autoindex on as this may publicly list all files in your web site directories.

Restart Nginx:

sudo systemctl restart nginx