Enable Directory Listing in Nginx
I switched from Apache to Nginx a few months ago and have been learning many things. One recent task I encountered was how to enable directory listing of a directory when an index file was not present.
This is what I typically see by default when trying to view a path that does not contain an index file in Nginx webserver.
Nginx’s HttpAutoIndexModule handles this.
Auto indexing can be enabled in http
, server
or location
context. I specifically wanted to allow auto indexing on only a particular subdirectory of my website.
I opened up Nginx’s configuration for the site I’m using which was found in /etc/nginx/sites-enabled/techish.net.conf
Under the http context, I created a location context and told it to use auto indexing if it did not find an index file.
location /pub/test {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
autoindex on
- turns auto indexing onautoindex_exact_size off
- I want file sizes rounded (KB, MB, GB, etc.). The default is off which uses Bytes.autoindex_localtime on
- Enables the file times to be shown locally. By default this is disabled and uses GMT.
A quick reload of Nginx and then I browse to https://techish.net/pub/test and directory browsing is working. No more 403 Forbidden errors.