nginx nested location and different basic authentication file

Francis Daly francis at daoine.org
Tue Sep 29 20:20:35 UTC 2015


On Sun, Sep 27, 2015 at 04:32:56AM -0400, cacrus wrote:

Hi there,

Your Subject: mentions "nested location"; but your config doesn't seem
to show any explicit nesting.

>         location  /parent/ {
>                 set $basic_file /nginx/conf/.htpasswd;
>                 if ($request_uri ~  (visualize|dashboard|settings)){
>                 set $basic_file /nginx/conf/.dev_pass;
>                 }

I was going to say "You're using _if_ inside _location_, which you
shouldn't do unless you know why you shouldn't do it".

But when I use something very similar in a nginx-1.9.1 system, it works
for me.

/parent/file.html requires the password from .htpasswd;
/parent/file.html?dashboard requires the password from .dev_pass.

So I'm unable to reproduce your problem, which means that I can't
demonstrate that any "fix" would actually make it work for you.

My first suggestion would be to move those four config lines to be
outside of all location{}s, so have them at server{} level.

When I did that in my test system, it stayed working.

If that does work for you, then you could try to achieve the same using
a map (at http{} level) as already suggested - set a variable for the
password-file to be one thing by default, and have a separate value for
the request_uri values that you care about, and then use that variable
in your config.

>                 proxy_pass         http://<some ip and port >/;
>                 auth_basic "Restricted";
>                 auth_basic_user_file $basic_file;
>         }
> 
> I would like  file .htpasswd to be used by default and in case of
> $request_uri having (visualize|dashboard|settings) , auth should happen via
> .dev_pass .
> 
> For some reason the authentication is happening only via  htpasswd  even if
> i have (visualize|dashboard|settings) in the URI . 
> 
> Any idea , what am i missing?

_if_ inside _location_ may not do what you expect. Especially if there
is more than one _if_ in there; but your example config does not show that.

But your config works for me in 1.9.1.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list