0

I'm trying to switch over to nginx from Apache, but one thing that I would like is the ability to allow directory listing for a parent directory (the root, in my case) only. For example, I would like to allow directory listing in the root directory, but I wouldn't like to have listings for any subdirectories (i.e. /somedir).

2 Answers 2

3

Yes. Note that autoindex defaults to off, so you simply allow it in whichever folder you need listings.

server {
    listen   80;
    server_name  example.com
    root   /path/to/root;
    location / {
            index index.php index.html index.htm;
    }
    location /somedir {
           autoindex on;
    }
}

Update

If you want to enable directory listings for directories but not its subdirectories, the location would be more like this, using the = operator (exact match).

    location = /somedir {
           autoindex on;
    }

You might need to cater for a trailing slash. I'd have to try that out to check.

5
  • I'm still not getting the desired behavior. What I was trying to achieve is to have directory listings in a directory, but to not have them in the directories that are in the one that has the listings. Commented May 1, 2017 at 2:29
  • Please edit your question to more clearly describe your aim. Your comment above doesn't make any sense to me at all. Include an example if it helps. Don't try to explain in comments, edit your question.
    – Tim
    Commented May 1, 2017 at 2:30
  • I figured that comment wouldn't make sense, it's difficult to describe. Commented May 1, 2017 at 2:38
  • Do you mean "I want to allow directory listings for a directory, but not its subdirectories"?
    – Tim
    Commented May 1, 2017 at 2:41
  • Yes, and I think that the new answer better reflects that. Commented May 1, 2017 at 2:42
0

The trick is to set 'autoindex on;' for / using regular expressions, and to use regular expressions for every other block (regex takes priority over simple matches).

sudo nano /etc/nginx/sites-available/example.com 

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location ~ ^/$ {
                autoindex on;
                #try_files $uri $uri/ =404;
                autoindex_exact_size off;
                autoindex_format html;
                autoindex_localtime on;
        }

        # blocks every other sub directory
        location ~ ^/.+$ { 
                autoindex off;
                try_files $uri $uri/ =404;
        }

        # overrides block for subdir3 and it's subdirs.
        location ^~ /subdir3/ { 
                autoindex on;
                try_files $uri $uri/ =404;
        }
}

This shows a directory listing for the top level only. Everything below that requires an index.html or other index file from the list. An exception is made for /subdir3 which will show a directory listing too, and so will subdirectories of /subdir3.

-Note: The other autoindex_xxx options are, well, optional. They are highly recommended though.

## After changing directives, be sure to restart your server. (command may vary per your linux distro/OS)
sudo service nginx restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .