nginx-从上游服务器读取自定义标头 [英] nginx - read custom header from upstream server

查看:142
本文介绍了nginx-从上游服务器读取自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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
    // ...
}

这可能吗?提前致谢.

Is this possible? Thanks in advance.

推荐答案

$ http _ name_of_the_header_key

即,如果标头中包含origin = domain.com,则可以使用$http_origin获取" domain.com "

i.e if you have origin = domain.com in header, you can use $http_origin to get "domain.com"

在nginx中,不支持任意请求标头字段.在上面的示例中,变量名称的最后一部分是将字段名称转换为小写字母,并用短划线替换为下划线

In nginx does support arbitrary request header field. In the above example last part of a variable name is the field name converted to lower case with dashes replaced by underscores

此处的参考文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_

在您的示例中,变量为$http_my_custom_header.

For your example the variable would be $http_my_custom_header.

这篇关于nginx-从上游服务器读取自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆