Are you also encountering a "403 Forbidden" message while accessing your web server running on Nginx? Then you might not have enabled directory listing in the Nginx config file.

403 forbidden error mesage on nginx

To fix these issues, simply follow the steps mentioned below.

Enable Directory Listing on Nginx

To enable directory listing on Nginx, you need to set the autoindex option to on in the Nginx config file.

To start, open the config file for the chosen web host (when multiple web servers are configured), but if you're using the default web server running on port 80, then simply run the following command to open the Nginx default config file.

  • sudo nano /etc/nginx/sites-available/default

Once the file is opened, you need to look for the location directive, which should resemble something like the following:

location / {
	# First attempt to serve request as file, then
	# as directory, then fall back to displaying a 404.
	try_files $uri $uri/ =404;
}

Now, simply add the autoindex on setting between the location directive.

location / {
	autoindex on;
	# First attempt to serve request as file, then
	# as directory, then fall back to displaying a 404.
	try_files $uri $uri/ =404;
}

Output:

enabling directory listing on nginx

Save and close the configuration file, then restart the Nginx server.

  • sudo systemctl restart nginx

Return to your browser and refresh the webpage to view the directory listing on your current web host.

directory listing in nginx

Disable Directory Listing on Nginx

Disabling directory listing is often a good idea, as exposing files and directories for the web host might create security issues or make it easy for a culprit to find a security flaw.

To disable it, you need to set the autoindex option to off, or you can simply comment on it or remove it from your Nginx configuration file.

location / {
	autoindex off;   #=>   Either set it to off, comment on it, or remove it.
	# First attempt to serve request as file, then
	# as directory, then fall back to displaying a 404.
	try_files $uri $uri/ =404;
}

Once done, make sure to restart your Nginx server by running:

  • sudo systemctl restart nginx

That's all it takes to enable or disable directory listing on nginx.