nginx - NGINX 根据 $arg 限制 IP 地址
问题描述
使用 nginx,我如何根据 $args 的正则表达式限制某些 IP 地址
例如
对于https://somewhere.invalid/login URI 位置
location /login {
allow 1.2.3.4;
deny all;
}
说得通
但是我如何允许 /login 只受 IP 限制,其中 $args = "person=super"
eq https://somewhere.invalid/login?person=super
nginx 不允许在“if”块中使用“allow”语句。
location /login {
allow all;
if ( $args ~ /person=super/ ) {
allow 1.2.3.4;
deny all;
}
}
如果位置块 / 是 proxy_pass 有什么区别吗?
解决方案
正如文档所说 allow
,deny
指令不能在if上下文中使用。允许的上下文是http、server、location和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; }
...
}
}
推荐阅读
- ecmascript-6 - Webpack,babelrc 动态导入不起作用
- docusignapi - Docusign - eNotary - 1 个公证人多重签名者
- javascript - innerHTML 函数不会改变我的 td 的内容
- amazon-web-services - AWS-CLI 访问 Linux 机器上的 S3
- excel - 将形状的自定义快捷方式应用于我所在的当前单元格
- c++ - Linux 共享库中缺少函数
- bash - 直到循环不起作用,脚本继续
- html - 如何在不使用 EJS 的情况下将数据传输到模板?
- firebase - 由于 Proguard,Unity Android 构建在启动时崩溃
- elasticsearch - 在 Elasticsearch 中查找最相似的文档