Enable directory listing in nginx


Enabling directory listing in a folder in nginx seems simple enough with just an autoindex on; directive inside the location directive. However, for some reason, it didn’t work for me.

I finally got it to work by moving the root directive out of location.
So, if you have something like this :

[code]server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/………………………;
location / {
root /path/to/root;
index index.php index.html index.htm;
}
location /somedir {
autoindex on;
}
}[/code]

Change it to :

[code]server {
listen 80;
server_name domain.com www.domain.com;
access_log /var/………………………;
root /path/to/root;
location / {
index index.php index.html index.htm;
}
location /somedir {
autoindex on;
}
}[/code]

Directory indices should show flawlessly now.
(A live example can be found here.)