Nginx 限制特定域名 UA 访问 - 不使用 if 语句
Nginx 限制特定域名 UA 访问 - 不使用 if 语句
本文介绍如何在 Nginx 中限制特定域名访问,仅允许包含 'qyuc' 的 User-Agent 访问。
使用 if 语句的解决方案:
location / {
if ($http_user_agent !~* "qyuc") {
return 403;
}
# other directives
}
上面这段代码是使用 if 语句的解决方案。下面是不使用 if 语句的解决方案:
不使用 if 语句的解决方案:
location / {
map $http_user_agent $allowed {
default 0;
~*qyuc 1;
}
if ($allowed = 0) {
return 403;
}
# other directives
}
这里使用了 map 指令,将 $http_user_agent 映射到一个变量 $allowed 上。如果 $http_user_agent 中包含 'qyuc',则 $allowed 的值为 1,否则为 0。然后使用 if 指令判断 $allowed 的值,如果为 0 则返回 403,否则执行其他指令。
原文地址: https://www.cveoy.top/t/topic/nwAN 著作权归作者所有。请勿转载和采集!