Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: auth_basic for everything except a specific location

Tags:

nginx

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

like image 940
X__ Avatar asked Feb 14 '12 17:02

X__


People also ask

What is Auth_basic in nginx?

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.


2 Answers

Try to use sign = , that helps you:

location = /README {        auth_basic off;        allow all; # Allow all to see content  } 
like image 194
Sergei Lomakov Avatar answered Sep 22 '22 22:09

Sergei Lomakov


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; } 
like image 40
Jason Fisher Avatar answered Sep 18 '22 22:09

Jason Fisher