我希望使用请求和代理为成功的身份验证请求设置一个标头,然后将其传递到将处理实际请求的下一个代理内联。
我已经设置了NGINX和各种代理来完成他们的任务,但是我不知道如何设置我为auth请求使用的服务器的头(图中的auth代理),以便将头传递给下一个服务器(图中的后端服务器)。
NGINX ---- auth request ----> AUTH PROXY
|
| <--- 201 <------ SUCCESS
|
----> underlying request ----> BACKEND SERVER
我的NGINX配置看起来像
server {
listen 9123;
resolver 10.3.0.2;
resolver_timeout 30;
location / {
auth_request /_auth;
proxy_set_header x-user $http_x_user;
proxy_pass http://backend_server;
}
location = /_auth {
internal;
proxy_pass https://auth;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
}
当我提出实际请求时,我在NGINX调试日志中看到以下内容(这是auth服务器响应的一部分):
2013/10/14 17:46:42 [debug] 31222#0: *4 http proxy header: "Content-Type: text/html; charset=utf-8"
2013/10/14 17:46:42 [debug] 31222#0: *4 http proxy header: "Date: Mon, 14 Oct 2013 17:46:42 GMT"
2013/10/14 17:46:42 [debug] 31222#0: *4 http proxy header: "Server: nginx/1.2.5"
2013/10/14 17:46:42 [debug] 31222#0: *4 http proxy header: "Vary: Cookie"
2013/10/14 17:46:42 [debug] 31222#0: *4 http proxy header: "x-user: 1"
我想获取x-user
头并将其传递到后端服务器。
我在location /
块中尝试过不同的组合,但它们都没有起作用。例如。
proxy_set_header x-user $upstream_http_x_user;
proxy_set_header x-user $http_x_user;
proxy_set_header x-user $sent_http_x_user;
proxy_pass_header x-user
这些似乎都不起作用。我有什么办法能完成这个任务吗?请注意,是auth代理设置了我想传递给后端服务器的头,
发布于 2013-10-14 18:09:40
喔,想出办法了。正确的NGINX配置如下所示:
location / {
auth_request /_auth;
auth_request_set $user $upstream_http_x_user;
proxy_set_header x-user $user;
proxy_pass http://backend_server;
}
问题是不能将头直接分配给另一个标头,您必须使用auth_request_set
将标头设置为变量,然后将该变量分配给标头。
https://stackoverflow.com/questions/19366215
复制相似问题