1、项目要求如果前端无登录且请求agent信息kube-probe不是探针探测的则返回403,多条件if写法如下:
server {
server_name nginx.msg;
listen 80;
access_log logs/host.access.log main;
set $flag "0";
if ($http_cookie !~* "userid=[\S]+"){
set $flag "${flag}1";
}
if ($http_user_agent !~* "kube-probe/.*"){
set $flag "${flag}1";
}
location / {
if ($flag = "011"){
return 403;
}
proxy_pass http://127.0.0.1:5678;
}
}
测试步骤:
1、通过chrome浏览器模拟cookie和user-agent请求,如果无cookie且user-agent不是kube-probe/.*过来的是否返回403
设置cookie方法:按F12-Console然后输入document.cookie='userid=xxx',然后回车,即设置了cookie,userid=xxx
删除cookie方法:点击地址栏左侧圆圈位置-目前使用的cookie点开以后选中点删除即可
设置user-agent方法:点击浏览器左下角3个点-More Tools-Network Conditions-User agent去掉Select automatically选则下方Custom然后设置user agent的值