Nginx获取自定义头部header的值

简介:

Nginx读取自定义header

在参考了资料:
http://stackoverflow.com/questions/8393772/how-to-get-non-standard-http-headers-on-nginx
http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
http://serverfault.com/questions/297225/nginx-passing-back-custom-header
https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip/

后得到如下:
1、nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持:

  • underscores_in_headers on;

2、比如我们自定义header为X-Real-IP,通过第二个nginx获取该header时需要这样:

  • $http_x_real_ip; (一律采用小写,而且前面多了个http_)

3、如果需要把自定义header传递到下一个nginx:

  • 如果是在nginx中自定义采用proxy_set_header X_CUSTOM_HEADER $http_host;

  • 如果是在用户请求时自定义的header,例如curl –head -H “X_CUSTOM_HEADER: foo”

示例:

http{
  upstream myServer {  
      server 127.0.0.1:8082;
  }
  underscores_in_headers on;
  server {
      listen       80;
      server_name  localhost;

      location  / {
   proxy_set_header Some-Thing $http_x_custom_header;;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_pass http://myServer;
      }
  } }


转自:http://blog.csdn.net/xbynet/article/details/51899286

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1842944如需转载请自行联系原作者


qianghong000

相关文章
|
Java 测试技术 Maven
Spring Boot项目打包配置详解
Spring Boot项目打包配置详解
698 0
|
存储 负载均衡 Kubernetes
Openresty动态更新(无reload)TCP Upstream的原理和实现
本文介绍了对Openresty或Nginx的TCP Upstream的动态更新(无需Reload)的一种实现方式,这种实现对于正在尝试做Nginx扩展的开发者是一种参考。文中我们对nginx结合lua对一次请求的处理流程和可扩展方式也进行了说明,重要的是给出了实际代码帮助开发者理解。目前社区中比如Kong、nginx-ingress-controller等基于Nginx扩展的项目都是类似的思路。
11928 1
Openresty动态更新(无reload)TCP Upstream的原理和实现
|
Ubuntu 数据安全/隐私保护
Ubuntu下/etc/sudoers的设置和sudo免密码执行及设置无效的原因
Ubuntu下免密码执行sudo及设置无效的原因
3934 0
|
消息中间件 运维 Kubernetes
工作中用Go: Go中异步任务怎么写
工作中用Go: Go中异步任务怎么写
3720 0
工作中用Go: Go中异步任务怎么写
|
应用服务中间件 nginx
如何优雅打印nginx header和body
场景 参考https://segmentfault.com/a/1190000000606867可以获取response的报文体,由于业务测试有获取响应头Header或响应体Body的需求,这里是通过header_filter_by_lua来分配响应报文头给变量实现的。
10558 0
|
应用服务中间件 nginx Apache
|
应用服务中间件 nginx
nginx输入请求的header到日志
nginx输入请求的header到日志
948 1