auth_basic within location block doesn't work when return is specified?
Igor A. Ippolitov
iippolitov at nginx.com
Mon Nov 7 11:14:33 UTC 2016
Changed own example in the last moment and made a mistake:
try files should address non existent file and then do an internal
redirect. E.g.
try_files nosuchfile.txt @ret;
Only the last argument may be a redirect.
On 07.11.2016 11:37, Igor A. Ippolitov wrote:
> This behavior is cause by general request processing logic. You may
> look at ngx_http_core_module.h which defines request processing phases.
> You may notice that a rewrite phase ('return' acts as a rewrite,
> actually) is run before access phase. So you have your request
> returned before access rules are checked.
> At the same time, try_files phase as after the access phase. So you
> may try using:
>
> location /auth {
>
> auth_basic_user_file /etc/nginx/.htpasswd;
>
> auth_basic "Secret";
>
> # try_files will be used only for a valid authenticated user
>
> try_files @redir =403; #403 will never be returned from here.
>
> }
>
> location @redir {
>
> return 200 'hello';
>
> }
>
>
> This looks a little bit hacky, but is pretty reasonable e.g. if you
> want to return 404.
>
> Just curious, why won't you auth protect your final destination?
>
> On 06.11.2016 18:09, Scott McGillivray wrote:
>>
>> i thought this would work but for some reason it doesn't.
>>
>> |location /auth { auth_basic_user_file /etc/nginx/.htpasswd;
>> auth_basic "Secret"; return 200 'hello'; } |
>>
>>
>> When i specify the return, 200 or 301, it just skips the auth_basic
>> and processes the return statement.
>>
>> If i comment out the return statement it works OK. Ideally i want
>> just an |/auth| endpoint that once authenticated it will 301 redirect
>> to $host, e.g. return 301 http://$host <http://$host>
>>
>> Can someone explain why this behaves this way and what is the correct
>> configuration.
>>
>> many thanks
>>
>>
>>
>> _______________________________________________
>> nginx mailing list
>> nginx at nginx.org
>> http://mailman.nginx.org/mailman/listinfo/nginx
>
>
>
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20161107/b75de575/attachment.html>
More information about the nginx
mailing list