与Nginx的auth_request_set和more_set_input_headers行为不一致

我正在尝试将auth_request模块与more_set_input_headers结合使用, 以便将我的用户自动login到Web应用程序中。

基本上,它是这样工作的:

  1. 用户有一些会话cookie来validation它们。
  2. 我有一个validationcookie的PHP脚本(auth.php),并返回他们正确的用户名作为响应头。
  3. Nginx使用auth_request调用auth.php,并将用户名设置为一个variables。
  4. Nginx然后调用Web应用程序,请求标头设置为正确的用户名。
  5. networking应用程序读取标题,并将用户login。

这工作,但奇怪的不一致。 问题是,当用户访问/ app /上的web应用程序时,它可以工作,但是当应用程序在/app/index.php上访问时,它永远不会收到来自nginx的标题。

我创build了一个模拟configuration,重现错误。

Nginx的网站configuration:

server { server_name www.example.com index index.php index.html; # --- Internal auth location /auth { internal; root /var/www/sf; location /auth/auth.php { fastcgi_pass unix:/var/run/php5-fpm.sock; include includes/fastcgi_params; fastcgi_pass_request_body off; fastcgi_param CONTENT_LENGTH 0; } location /auth { deny all; } } location / { auth_request /auth/auth.php; auth_request_set $auth_header $upstream_http_x_auth_header; more_set_input_headers 'X-Test-Header: $auth_header'; location /app { root /var/www/sf; # Allow these locations location = /app/ { allow all; } location /app/index.php { fastcgi_pass unix:/var/run/php5-fpm.sock; include includes/fastcgi_params; } # Deny everything else location /app/ { deny all; } } } } 

/var/www/sf/auth/auth.php:

 <?php // Mock precondition checker function is_allowed() { return true; } // Set the default response code http_response_code(403); if(!is_allowed()) exit(); // Return our header and the OK response code header("X-Auth-Header: myusername"); http_response_code(200); 

/var/www/sf/app/index.php:

 <?php if(empty($_SERVER["HTTP_X_TEST_HEADER"])) exit("No header was supplied by nginx."); exit("Nginx supplied header value: ". $_SERVER["HTTP_X_TEST_HEADER"]); 

对/ app /执行GET请求时的响应

 Nginx supplied header value: myusername 

对/app/index.php执行GET请求时的响应:

 No header was supplied by nginx. 

有没有人有一个想法,在这里发生了什么?

我使用Debian Wheezy和DotDeb的nginx-extras包(1.6.0-1〜dotdeb.1)。

(小记:当你用'more_set_input_headers'X-Test-Header:foobar';'replace'more_set_input_headers'X-Test-Header:$ auth_header';'时,web应用程序总是收到一个头文件)

这种行为很可能是由于more_set_input_headers处理程序在访问阶段( auth_request工作)之前执行的事实,因此只有在内部redirect的情况下才会更改请求。

解决问题的办法是停止使用more_set_input_headers (无论如何,这是非常错误的,请求头不应该改变),并使用本地fastcgi_param而不是:

 fastcgi_param HTTP_X_TEST_HEADER $auth_header; 

http://mailman.nginx.org/pipermail/nginx/2014-June/044281.html复制&#x3002;