Nginx:是否有可能从 auth_request 获得响应

Nginx: Is it possible to get response retuned from auth_request

我正在为 nginx 使用 auth 模块。 (http://nginx.org/en/docs/http/ngx_http_auth_request_module.html) 是否可以以某种方式存储来自 /auth 的响应,以便我可以将其作为请求主体发送到另一个端点。

location /private/ {
    auth_request /auth;
    proxy_pass ... 
    proxy_set_body 'Here I want to put /auth response. How?';
}

location = /auth {
    proxy_pass ...
}

简答:

不,你不能。

长答案:

您无法获得 body 返回给 auth_request 的响应。不过,您可以使用 auth_request_set 指令在响应中返回 header:

location / {
    auth_request /auth;
    auth_request_set $auth_foo $upstream_http_foo;
    proxy_pass ...
    proxy_set_body $auth_foo;
}

以上配置会将 $auth_foo 变量设置为 auth 子请求的 Foo header 的值。