I have server configured this way:
server {
listen 80 default_server;
listen [::]:80 default_server;
location /health {
auth_basic "Restricted";
auth_basic_user_file "/etc/nginx/.htpasswd";
default_type application/json;
return 200 '{"status":"Ok"}';
}
}
/etc/nginx/.htpasswd
exists and contains desired credentials.
Now when I try to access this location it just passes all requests without auth check. E.g.:
➜ ~ curl http://localhost:23456/health
{"status":"Ok"}%
Did I miss something?
2
Answers
No you are not "missing" something in general but there is a but 🙂
NGINX works with something called "access-phases".
return
kicks in VERY VERY VERY early in the request processing. Having areturn
statement in a location block tells NGINX to immediately return with this. No matter whats in other phases after it.Further reading: http://nginx.org/en/docs/dev/development_guide.html#http_phases
As the
return
statement is part of thengx_http_rewrite_module
this is the phase where the return statement kicks in. According to the documentation theNGX_HTTP_ACCESS_PHASE
in which yourauth_basic
will be checked comes a lot later.So to make this work you have to use a little trick.
Or you can use
njs
with ajs_content
as well if you do not want to use a proxy.As the accepted answer already states, there is no clean solution to this. However, there’s an alternative workaround that doesn’t require an additional server block: