【问题标题】:Setting headers with NGINX auth_request and oauth2_proxy使用 NGINX auth_request 和 oauth2_proxy 设置标头
【发布时间】:2013-10-22 08:55:04
【问题描述】:

我想使用auth_requestoauth2_proxy 在成功的身份验证请求上设置标头,然后将其传递给将处理实际请求的下一个内联代理。

我已经设置了 NGINX 和各种代理来做他们的事情,但是我不确定如何设置我用于身份验证请求的服务器的标头(图中的 AUTH PROXY),以便该标头是传递到下一个服务器(图中的 BACKEND SERVER)

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 调试日志中看到以下内容(这是来自身份验证服务器的响应的一部分):

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

这些似乎都不起作用。有什么想法可以完成这项任务吗?请注意,设置我要传递给后端服务器的标头的是身份验证代理,

【问题讨论】:

    标签: nginx proxy auth-request


    【解决方案1】:

    哎呀,想通了。正确的 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 将标头设置为变量,然后将该变量分配给标头。

    【讨论】:

    • 如何将 $user 传递给 cookie?
    • @ShivKumar 为此提出了一个新问题。
    猜你喜欢
    • 2018-03-08
    • 2018-04-15
    • 2022-12-21
    • 1970-01-01
    • 2021-02-22
    • 2018-06-12
    • 2012-12-16
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多