nginx 获取header 请求参数_Nginx获取自定义头部header的值

本文介绍了如何在Nginx中配置获取和传递非标准的用户自定义Header参数,包括启用underscores_in_headers选项,使用$http_x_real_ip等变量,并通过proxy_set_header和proxy_pass_header指令在不同层级的代理间传递Header信息。
摘要由CSDN通过智能技术生成

http://blog.csdn.net/xbynet/article/details/51899286?_t=t

http://shift-alt-ctrl.iteye.com/blog/2331455

http://blog.csdn.net/bao19901210/article/details/52537279

后得到如下:

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://domain.com/api/test,则需要通过proxy_pass_header X_CUSTOM_HEADER来传递

注意nginx 1.11.x后的版本才支持 request_id 内置变量

示例:

http{

underscores_in_headers on;

upstream myServer {

server 127.0.0.1:8082;

}

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; } } }

示例:

网络架构:

源站  1层nginx代理  2层nginx代理 CDN 客户端

2层代理  nginx.conf

underscores_in_headers on;

log_format main '$http_x_forwarded_for`$remote_addr`$proxy_add_x_forwarded_for`[$time_local]`"$request"`'

'$status`$body_bytes_sent`"$http_referer"`'

'"$http_user_agent"`"$request_time"`'

'$request_id`$upstream_response_time`$upstream_addr`$upstream_connect_time`$upstream_status';

2层代理站点配置:

location中设置 proxy_set_header

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值