Nginx 根据请求的来源需要基本身份验证(使用地理模块按国家筛选)

Nginx 根据请求的来源需要基本身份验证(使用地理模块按国家筛选),nginx,basic-authentication,Nginx,Basic Authentication,是否有一种方法可以根据请求的来源要求基本身份验证,并按国家过滤?我的目标是能够始终要求基本身份验证,除非请求来自X、Y或Z国家/地区。这个答案是在假设您已经安装了模块并正在工作的情况下得出的,有很多很好的指南可以安装它,所以我将跳过它 您可以通过以下方式映射geoIP: geoip_country /path/to/GeoIP/GeoIP.dat; map $geoip_country_code $not_auth_required { default no; ZZ yes;

是否有一种方法可以根据请求的来源要求基本身份验证,并按国家过滤?我的目标是能够始终要求基本身份验证,除非请求来自X、Y或Z国家/地区。

这个答案是在假设您已经安装了模块并正在工作的情况下得出的,有很多很好的指南可以安装它,所以我将跳过它

您可以通过以下方式映射geoIP:

geoip_country /path/to/GeoIP/GeoIP.dat;
map $geoip_country_code $not_auth_required {
    default no;
    ZZ yes;
    YY yes;
    XX yes;
}
然后为每个人设置基本身份验证,但以下人员除外:

server {
    listen 80;
    root   /foo/bar/baz;
    auth_basic           “Wrong Country";
    auth_basic_user_file /etc/nginx/.htpasswd; #typical user:$hash auth.

    if ($not_auth_require = yes) {
        auth_basic off;
    }


    location / {
         #do things
    }
}
理论上,这种逻辑应该起作用,因为你需要一个白名单,而不是黑名单。我希望我能帮忙