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

Kle*_*ack 3 regex nginx elasticsearch

我不知道如何配置检查是否$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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的用例:

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

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

daw*_*wez 5

这应该是可能的,并且您还可以在与正则表达式匹配时定义变量的名称。

在下面的代码片段中,我将正则表达式的结果分配给变量$username。然后我可以检查它,如果匹配$remote_user(确保这个设置正确,以防像我一样调试它)然后我在响应中添加了一个额外的标头,这样你就可以看到它,Firebug network console或者只是通过使用该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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

另请参阅如何从 nginx 中的位置获取变量?了解更多信息。