nginx - 如何将 $request_uri 添加到 auth_request 模块?

标签 nginx nginx-config auth-request

这是我的 nginx 配置文件:

location ~* ^/admin-panel/rest/(.*) {
    auth_request        /admin/admin_authentication/check_access?url=$request_uri;

    proxy_set_header    Host        $host;
    proxy_set_header    X-Real-IP   $remote_addr;

    resolver 127.0.0.11 ipv6=off;

    proxy_pass        http://nginx:8000/$1$is_args$args;
}

我想将 $request_uri 作为 GET 参数发送到我的身份验证服务。但我收到这样的错误:

2019/06/23 06:30:07 [error] 6#6: *5 auth request unexpected status: 
404 while sending to client, client: 192.168.224.1, server: , request: 
"POST /admin-panel/rest/update HTTP/1.1", host: "localhost"
2019/06/23 06:30:07 [error] 6#6: *8 auth request unexpected status: 
404 while sending to client, client: 192.168.224.1, server: , request: 
"POST /admin-panel/rest/update HTTP/1.1", host: "localhost"
2019/06/23 06:31:56 [error] 6#6: *1 auth request unexpected status: 
404 while sending to client, client: 192.168.224.1, server: , request: 
"POST /admin-panel/rest/update HTTP/1.1", host: "localhost"
2019/06/23 06:31:57 [error] 6#6: *3 auth request unexpected status: 
404 while sending to client, client: 192.168.224.1, server: , request: 
"POST /admin-panel/rest/update HTTP/1.1", host: "localhost"

当我删除 auth_request 中的 ?url=$request_uri 部分时,一切正常

最佳答案

通过使用lua-nginx-module (或 openresty docker 镜像),您可以使用 access_by_lua_block 而不是 auth_request,如下所示:

location ~* ^/admin-panel/rest/(.*) {
    access_by_lua_block {
        local res = ngx.location.capture("/admin/admin_authentication/check_access?url=" .. ngx.var.request_uri)

        if res.status == ngx.HTTP_OK then
            return
        end

        if res.status == ngx.HTTP_FORBIDDEN then
            ngx.exit(res.status)
        end

        ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
    }

    proxy_set_header    Host        $host;
    proxy_set_header    X-Real-IP   $remote_addr;

    resolver 127.0.0.11 ipv6=off;

    proxy_pass        http://nginx:8000/$1$is_args$args;
}

实际上,此代码使用 access_by_lua_block 实现 auth_request 并使用 Lua 连接运算符 .. 创建 URL。

关于nginx - 如何将 $request_uri 添加到 auth_request 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56721623/

相关文章:

spring-boot - 我可以在 oauth/check_token 端点中附加一些信息并在授权服务器上检索它吗?

nginx auth_request 错误身份验证请求意外状态: 302 while sending to client

docker - 如何自动停止docker容器?

regex - Nginx 位置匹配多个扩展名,除非路径以特定单词开头

nginx - 在 Ubuntu 10.04 上使用 Nginx 设置 Tornado 以供生产使用

nginx - 如何配置nginx的default.conf

docker - 在内容更改时重新加载Nginx容器

ruby-on-rails - Nginx 和 Unicorn 上的 resources_url 出现意外行为

nginx - 如何在 terraform 中创建 nginx 入口 - aks