使用nginx如何根据$args的regexp限制某些IP地址
如
表示https://somewhere.invalid/login的URI位置
location /login {
allow 1.2.3.4;
deny all;
}
是有意义的
但是我如何允许/login到所有,但限制IP $args = "person=super">
情商https://somewhere.invalid/login?person=super
nginx不允许"if"中的语句块。
location /login {
allow all;
if ( $args ~ /person=super/ ) {
allow 1.2.3.4;
deny all;
}
}
如果位置块/是proxy_pass,会有什么不同吗?
正如文档所说,如果 ,allow
和deny
指令不能在中使用。上下文。允许的上下文http服务器,位置,和limit_except. 但是,您可以通过两个map
块检查$arg_person
和$remote_addr
变量值来实现相同的行为:
map $arg_person $deny {
super $checkip;
# default value will be an empty string
}
map $remote_addr $checkip {
1.2.3.4 '';
# you can add other allowed IPs here
default 1;
}
server {
...
location /login {
if ($deny) { return 403; }
...
}
}