How can I enable HTTP Basic Auth for everything except for a certain file?
Here is my current server block configuration for the location:
location / { auth_basic "The password, you must enter."; auth_basic_user_file /etc/nginx/htpasswd; } location /README { auth_basic off; }
However, on /README
, it is still prompting for a password.
How can we fix this?
Thanks! Mark
auth_basic. auth_basic_user_file. The ngx_http_auth_basic_module module allows limiting access to resources by validating the user name and password using the “HTTP Basic Authentication” protocol. Access can also be limited by address, by the result of subrequest, or by JWT.
Try to use sign = , that helps you:
location = /README { auth_basic off; allow all; # Allow all to see content }
I am doing something similar using "map" instead of "if" to assign the auth_basic realm variable and htpasswd file:
map $http_host $siteenv { default dev; ~^(?<subdomain>.+)\.dev dev; ~^(?<subdomain>.+)\.devprofile devprofile; ~^(?<subdomain>.+)\.devdebug devdebug; ~^(?<subdomain>.+)\.test test; ~^(?<subdomain>.+)\.demo demo; ~^(?<subdomain>.+)\.stage stage; # Live ~^(?<subdomain>.+)\.live live; ~^.*\.(?P<subdomain>.+)\.[a-zA-Z]* live; } map $http_host $auth_type { default "Restricted"; ~^(?<subdomain>.+)\.dev "Development"; ~^(?<subdomain>.+)\.devprofile "Development"; ~^(?<subdomain>.+)\.devdebug "Development"; ~^(?<subdomain>.+)\.test "Testing"; ~^(?<subdomain>.+)\.stage "Stage"; ~^(?<subdomain>.+)\.demo "Demo"; # Live ~^(?<subdomain>.+)\.live "off"; ~^.*\.(?P<subdomain>.+)\.[a-zA-Z]* "off"; } server { .. etc .. auth_basic $auth_type; auth_basic_user_file /etc/nginx/conf.d/htpasswd-$siteenv; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With