Fix a 403 forbidden error in Nginx

The causes of a 403 error can be diverse. In principle it means that you do not have access to the content that you want to access through the URL that you have written. This may simply be due to the fact that the server does not have the way to return content to you, that is, it does not have the way to resolve the route that you have indicated.

A typical error is that in the sequence of files that are going to capture the request to the server there is none that can process it. This can be configured in the configuration file for your virtual host in nginx, which can have several different locations, but is usually found at /etc/nginx/sites-available/example.com

If the URL is a folder, first you have to check that in the list of “document root” files you have one to process it. If it is an index.php file, you will have to indicate it, placing the order of preference. It would be something like

index index.php index.html;

Then, in the “location” configuration you have to correctly indicate the file that will be in charge of processing the request. Generally it would be something like:

location / { try_files $uri $uri/; }

But if it’s a framework, you could tell that request to be processed by an index.php file that does the routing work.

location / { try_files $uri $uri/ /index.php?$query_string; }

Basically with these two “location” directives you tell Nginx how to search for candidates to process the request. If you don’t have anyone handling the request, you’d actually get a 404 error, but it could be the case that you’re trying to tell some file that doesn’t exist on the server or doesn’t have a way to process the request to handle the request.

See also  Cookies in PHP

If the files that gives you the error are “.php” you should check that the PHP configuration is correct on the server. Here it already depends on how you have installed PHP on your system, but you have this configuration in the block:

location ~ \.php$ { include snippets/fastcgi-php.conf; # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; }

It can also happen to you that some routes or files are being rejected, with some location of type “deny all”.

location ~ /\.ht { deny all; }

The permissions on files They can also be a problem, as well as the owner user. If they are not well assigned, they could be a reason for failure. Remember that for security the correct permissions should be 644 for files and 755 for directories. The user who owns the files is usually www-data. This configuration can be set with chmod and chown. You have explanations in these articles: and if you do it by FTP you have this other article. On the subject of the owner you can read the faq: .

You can check all of the above, but also take into account that in reality the 403 may really be due to trying to access really protected content, so the correct thing to do is just for the server to return the 403. Or that it is a directory and no index.html or index.php file to serve.

Loading Facebook Comments ...
Loading Disqus Comments ...