我想弄清楚如何将我的nginx conf中的用户代理列入白名单。 所有其他代理应显示密码提示。
在我的天真中,我试图把以下内容deny all :
if ($http_user_agent ~* SpecialAgent ) { allow; }
但是我被告知"allow" directive is not allowed here (!)。
我怎样才能使它工作?
我的configuration文件的一大块:
server { server_name site.com; root /var/www/site; auth_basic "Restricted"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; allow 123.456.789.123; deny all; satisfy any; #other stuff... }
谢谢你的帮助。
如果只需要通过用户代理进行白名单(而不是IP或其他条件),并且不想重新编译Nginx就可以包含auth_request模块,您还可以使用map指令来检查用户代理并有条件地禁用基本身份validation。
map $http_user_agent $auth_type { default "Restricted"; ~^Mozilla "off"; } server { location / { auth_basic $auth_type; } }
这将closuresMozilla用户代理的基本身份validation。
您可以使用auth_request模块来实现此目的。
http://nginx.2469901.n2.nabble.com/Is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses- the-access-and-td6752995.html按照@quanta的评论。