nginx - 从上游服务器读取自定义标头 [英] nginx - read custom header from upstream server
问题描述
我使用 nginx 作为反向代理,并尝试从上游服务器 (Apache) 的响应中读取自定义标头,但没有成功.Apache 响应如下:
I am using nginx as a reverse proxy and trying to read a custom header from the response of an upstream server (Apache) without success. The Apache response is the following:
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
我想从 My-custom-header 读取值并在 if 子句中使用它:
I want to read the value from My-custom-header and use it in a if clause:
location / {
// ...
// get My-custom-header value here
// ...
}
这可能吗?提前致谢.
推荐答案
不仅可能,而且很容易:
It's not only possible, it's easy:
在 nginx 中,响应标头值可通过变量(每个标头一个)获得.有关详细信息,请参阅 http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER那些变量.
in nginx the response header values are available through a variable (one per header). See http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER for the details on those variables.
在您的示例中,变量为 $sent_http_My_custom_header.
In your examle the variable would be $sent_http_My_custom_header.
这篇关于nginx - 从上游服务器读取自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!