Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auth_basic within location block doesn't work when return is specified?

i thought this would work but for some reason it skips the auth_basic and always returns 200. Same happens if i swap 200 for a 301 redirect.

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 another path.

 location /auth {
     auth_basic_user_file /etc/nginx/.htpasswd;
     auth_basic "Secret";

     return 200 'hello';
  }

Am i missing something ?

many thanks

fLo

like image 886
Flo Woo Avatar asked Nov 06 '16 08:11

Flo Woo


1 Answers

return-directives are executed before most other directives. To solve your problem you need to split this into two locations:

location /auth {
  auth_basic_user_file /etc/nginx/.htpasswd;
  auth_basic "Secret";
  try_files DUMMY @return200;
}

location @return200 {
  return 200 'hello';
}

The try_files-directive is evaluated after auth_basic. The second location is evaluated only as a result of try_files.

like image 149
Digitalkapitaen Avatar answered Nov 08 '22 06:11

Digitalkapitaen