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