我写了一个关于Apacheconfiguration的类似问题 ,但我也对nginx感兴趣。
除nginxconfiguration中指定的用户代理之外,是否可以要求基本authentication?
是的,通过使用nginx map指令是可能的。
例如:
map $http_user_agent $auth { default 0; "~Chrome" 1; } server { ... other vhost config ... if ($auth = 1) { auth_basic "Authenticate"; auth_basic_user_file /path/to/authfile; } }
map指令查找第一个参数中指定的variables,并根据map块内的信息为第二个variables声明值。
然后我们使用$authvariables来查看我们是否想要进行身份validation。
您可以在http://nginx.org/en/docs/http/ngx_http_map_module.html map的nginx地图文档页面阅读更多关于如何使用map匹配不同的string
经过几个晚上的研究和试错,我发现条件authentication可以创build结合map指令与自定义variablesauth_basic指令:
map $http_user_agent $authentication { default "Access Restricted"; "~^PayPal IPN" "off"; # ... } server { # ... location / { auth_basic $authentication; auth_basic_user_file /etc/nginx/.htpasswd; # ... } }