gpt4 book ai didi

regex - NGINX:检查 $remote_user 是否等于位置的第一部分

转载 作者:行者123 更新时间:2023-11-29 02:55:15 24 4
gpt4 key购买 nike

我不知道如何配置检查 $remote_user 是否等于 location 的第一部分。

location / {
auth_basic "ElasticSearch";
auth_basic_user_file /etc/nginx/search_passwords;

location ~/([^/]*)/ { # named capture did not work either
if ($remote_user = $1) { # ERROR: Unknown variable: $1 / $NAMEDWHATEVER
break;
}
}
}

我的用例:

我正在尝试通过使用 nginx 反向代理进行身份验证来“保护”ElasticSearch 集群。我想允许多个用户,但新用户的修改应该尽可能简单;最好的情况 - 只需更改 htpasswd 文件。

我正在尝试采用 nginx-elasticsearch-proxy但无法检查 remote_nameindex-name 是否相等。

最佳答案

应该是可以的,你也可以在正则匹配的时候定义一个变量名。

在下面的代码片段中,我将正则表达式的结果分配给变量 $username。然后我可以检查它,如果匹配 $remote_user(确保这个设置正确,以防像我一样调试它)然后我在响应中添加了一个额外的 header ,这样你就可以看到使用 Firebug 网络控制台 或仅使用 curl 命令。

看看这个是否对您有帮助。

location / {
auth_basic "ElasticSearch";
auth_basic_user_file /etc/nginx/search_passwords;

location ~ /(?<username>([^/]*))/ { # capture and store in the username variable
if ($remote_user = $username ) {
add_header 'matched' 'true $username';
break;
}
}
}

另见 How to I get variables from location in nginx?了解更多信息。

关于regex - NGINX:检查 $remote_user 是否等于位置的第一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com