Search code examples
passwordsnginxprotected

NGINX Password protected dir with autoindex feature


I know these two questions (password protected dirs and autoindex) was get answered but not together.

I can do it at the same time but i have a problem with it. Take a look at it. This is my conf file of nginx.

location ~ /(archives|fallen) {
            autoindex  on;
            auth_basic            "Restricted Area for Private Use Only";
            auth_basic_user_file  passwords;
}

location / {
    root   /www/mirror;
    index  index.html index.htm index.php;
            autoindex  on;
            autoindex_exact_size off;
}

as you can see, archives and fallen dirs are password protected and autoindex for both is open. But, normal un-protected dirs can be autoindexed but password protected dirs are not.

If i enter password protected dirs it shows me an 404 error because ther is no index.html and it just disable autoindex feature. But in the other hand as i said before, unprotected dirs are autoindexed as usual.

Is anyone have any solution for this? Please let me know.


Solution

  • Found the solution. We have to write root and autoindex methods outside of the location. In generally speaking; we have to set them as global in a server { } tag.