Enable directory listing in nginx

nginx logoEnabling 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.)

Join the Conversation

5 Comments

    1. Not a bug, actually. Putting a root inside a location block is perfectly valid if all the location blocks have root defined.

      This is perfectly valid :

      server {
              server_name  domain.com;
              access_log  /var/...........................;
              location / {
                      root   /path/to/root;
                      index  index.php index.html index.htm;
              }
              location /somedir {
                     root   /path/to/root/somedir;
                     autoindex on;
              }
      }

      This is not :

      server {
              server_name  domain.com;
              access_log  /var/...........................;
              location / {
                      root   /path/to/root;
                      index  index.php index.html index.htm;
              }
              location /somedir {
                     autoindex on;
              }
      }

      See : http://wiki.nginx.org/Pitfalls#Root_inside_Location_Block

Leave a comment

Leave a Reply