nginx: auth_basic for everything except a specific location

后端 未结 4 1865
再見小時候
再見小時候 2021-02-05 04:03

How can I enable HTTP Basic Auth for everything except for a certain file?

Here is my current server block configuration for the location:

locat         


        
4条回答
  •  渐次进展
    2021-02-05 04:41

    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;
    
      ~^(?.+)\.dev dev;
      ~^(?.+)\.devprofile devprofile;
      ~^(?.+)\.devdebug devdebug;
      ~^(?.+)\.test test;
      ~^(?.+)\.demo demo;
      ~^(?.+)\.stage stage;
    
      # Live
      ~^(?.+)\.live live;
      ~^.*\.(?P.+)\.[a-zA-Z]* live;
    }
    
    map $http_host $auth_type {
      default       "Restricted";
    
      ~^(?.+)\.dev "Development";
      ~^(?.+)\.devprofile "Development";
      ~^(?.+)\.devdebug "Development";
      ~^(?.+)\.test "Testing";
      ~^(?.+)\.stage "Stage";
      ~^(?.+)\.demo "Demo";
    
      # Live
      ~^(?.+)\.live "off";
      ~^.*\.(?P.+)\.[a-zA-Z]* "off";
    }
    
    server {
      .. etc ..
    
      auth_basic            $auth_type;
      auth_basic_user_file  /etc/nginx/conf.d/htpasswd-$siteenv;
    }
    

提交回复
热议问题