2012-09-14 36 views
34

我正在使用nginx作为反向代理并尝试从上游服务器(Apache)的响应中读取自定义标头,但未成功。 Apache的回应如下:一,如果从句nginx - 从上游服务器读取自定义标头

HTTP/1.0 200 OK 
Date: Fri, 14 Sep 2012 20:18:29 GMT 
Server: Apache/2.2.17 (Ubuntu) 
X-Powered-By: PHP/5.3.5-1ubuntu7.10 
Connection: close 
Content-Type: application/json; charset=UTF-8 
My-custom-header: 1 

我想从我的定制头读值,并使用它:

location/{ 
    // ... 
    // get My-custom-header value here 
    // ... 
} 

这可能吗?提前致谢。

回答

38

$ HTTP _ name_of_the_header

在nginx的不支持任意请求头字段。在上述示例中的可变名称的最后部分是转换为小写用下划线取代破折号字段名称

参考文档这里:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_

对于示例变量将是$http_my_custom_header

+2

其实,这是错误的,不应该标记答案。对于将来遇到这个问题的人来说,正确的变量是'$ http_'。以OP为例,'$ http_my_custom_header'(区分大小写!) – jduncanator

+0

我已更正此答案以匹配文档;) –

+6

事实证明,这也不正确。 OP要求读取响应头的值。你应该使用'$ sent_http_my_custom_header'。 http://nginx.org/en/docs/http/ngx_http_core_module.html#var_sent_http_ – Nathan

3

使用$ http_MY_CUSTOM_HEADER

你可以写一些,东西一样

set my_header $http_MY_CUSTOM_HEADER; 
if($my_header != 'some-value') { 
#do some thing; 
} 
+0

男人说 - 变量名称的最后一部分是字段名称转换为__lower case__用破折号替换为下划线。 –

23

我面临同样的问题。我尝试了$http_my_custom_header$sent_http_my_custom_header,但它不适合我。

虽然通过使用$upstream_http_my_custom_header解决了这个问题。

+0

这适用于我,在nginx 1.10.1上 – chhantyal

相关问题