我正在使用nginx作为反向代理,并尝试从上游服务器(Apache)的响应中读取自定义标头,但未成功。 Apache的回应如下:
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子句中使用它:
location / { // ... // get My-custom-header value here // ... }
这可能吗? 提前致谢。
$ http _ name_of_the_header
在nginx中是否支持任意的请求头域。 在上面的例子中,变量名的最后一部分是将字符串转换为小写,用破折号替换下划线
参考文档在这里: http : //nginx.org/en/docs/http/ngx_http_core_module.html#var_http_
对于你的例子,变量将是$http_my_custom_header
。
我面临同样的问题。 我尝试了$http_my_custom_header
和$sent_http_my_custom_header
但它不适用于我。
虽然通过使用$upstream_http_my_custom_header
解决了这个问题。
使用$ http_MY_CUSTOM_HEADER
你可以写一些类似的东西
set my_header $http_MY_CUSTOM_HEADER; if($my_header != 'some-value') { #do some thing; }