Methods of payment Abuse

What to do if the index file is not specified in the Nginx settings

28.11.2023, 23:29

If the index file is not specified in the Nginx settings, then when accessing the root directory, the server will return error 403 Forbidden. To fix this problem, you need to add the index directive to the server configuration and specify the desired file (usually this index.html or index.php ).

What does 403 Forbidden mean?

The 403 Forbidden error in Linux means that the user who accesses the server does not have access rights to the requested resource. This may be caused by incorrect file or directory access rights, server security settings, or Nginx configuration.

To fix this error, you need to make sure that the access rights to files and directories are set correctly and check the server configuration

What does it look like in practice

Example:

server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}

In this example, we indicated that when accessing the root directory, the server will search for the file index.html and display its contents. If such a file is missing, the 404 Not Found error will be returned.

After making changes to the server configuration, Nginx must be restarted to apply the settings:

sudo systemctl restart nginx

If you request a URL like /vstats/, but the index file is not specified in the Nginx settings, you will fail and you will get 404. You can add the index directive to your location:

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

Or immediately in the server, in Nginx all locations inherit the directives set in the server