You are not logged in.

#1 2018-12-09 15:44:30

pntruongan
Member
Registered: 2011-01-31
Posts: 63

[SOLVED]NGINX: autindex with basic auth but allow direct file access

Hi everyone

I'm trying out Nginx in place of apache. In apache, I have special folder and sub folder that I want to enable Directory Listing with basic authentication. I have it done with

autoindex on; auth_basic; auth_basic_user_file 

. However, I also store files in that directory that I would like direct access without any authentication. In apache I get that done with a <Filematch> directive. to match any file, then allow from all to override the authentication. I haven't been able to find any similar configuration for nginx. Any thought on this would be much appreciated.

Last edited by pntruongan (2018-12-20 02:21:44)

Offline

#2 2018-12-09 16:53:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED]NGINX: autindex with basic auth but allow direct file access

Just add another location block specifying the files or a pattern that matches the files to specify settings for them.

https://www.digitalocean.com/community/ … ion-blocks

Last edited by Trilby (2018-12-09 17:01:13)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2018-12-09 17:01:14

pntruongan
Member
Registered: 2011-01-31
Posts: 63

Re: [SOLVED]NGINX: autindex with basic auth but allow direct file access

Trilby wrote:

Just add another location block specifying the files or a pattern that matches the files to specify settings for them.

Well, there's a lot of files in that folder and its sub folder, so much that it's almost impossible to specify a pattern. Is there some kind of pattern to match files but not sub folder?

Offline

#4 2018-12-09 17:02:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED]NGINX: autindex with basic auth but allow direct file access

pntruongan wrote:

Is there some kind of pattern to match files but not sub folder?

Yes.  Just write a regular expression excluding (additional) /'s.

Or just invert the logic, and apply the settings in your first post just to the specific directories you want them applied to rather than to everything under them.

Last edited by Trilby (2018-12-09 17:03:05)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2018-12-11 17:24:49

pntruongan
Member
Registered: 2011-01-31
Posts: 63

Re: [SOLVED]NGINX: autindex with basic auth but allow direct file access

Trilby wrote:
pntruongan wrote:

Is there some kind of pattern to match files but not sub folder?

Yes.  Just write a regular expression excluding (additional) /'s.

Or just invert the logic, and apply the settings in your first post just to the specific directories you want them applied to rather than to everything under them.

Inverting the logic seem good. It was easier to write a pattern ending with forward slash / and protect that location with basic authentication. I will try it out. Thank you very much.

Offline

#6 2018-12-20 02:20:42

pntruongan
Member
Registered: 2011-01-31
Posts: 63

Re: [SOLVED]NGINX: autindex with basic auth but allow direct file access

OK, so this is h

location ~ ^/directory_name(.*/)$ { #This is to catch any path start with directory name and end with a slash /
        alias /path/to/that/directory_name$1; # alias to serve the exact folder
        
        auth_basic "ABC";
        auth_basic_user_file /path/to/password/file/.htpasswd;
       autoindex on;
}

outside this location block I have another block setup to serve all file with no password but without no autoindex. That seem to cut it, I leave it here for anyone happens to need it

Offline

Board footer

Powered by FluxBB