001_nginx常用参数查询

一、underscores_in_headers on;

  • Nginx 默认把名称包含下划线的 Headers 视为无效,直接移除。如果你希望让这类型的信息生效,那你要把 underscores_in_headers 指令设置成 on,否则这样的头信息将不会把他发送给后端服务器。参考URL:http://n3xtchen.github.io/n3xtchen/nginx/2016/02/19/nginx-port-forwording

二、proxy_set_header设置

(1)常用的语法

<1>proxy_set_header Host $host;       参考:http://liuluo129.iteye.com/blog/1943311
<2>proxy_set_header X-Forwarded-Host $host;
<3>proxy_set_header X-Forwarded-Server $host;
<4>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
<5>proxy_set_header X-Real-IP $remote_addr;

(2)Nginx内置变量解释

<1>$host
   in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request
<2>$remote_addr=>client address
<3>$proxy_add_x_forwarded_for
   the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

解释=>

先来看下proxy_set_header的语法
语法:proxy_set_header field value;
默认值:	
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
上下文:	http, server, location
允许重新定义或者添加发往后端服务器的请求头。value可以包含文本、变量或者它们的组合。 当且仅当当前配置级别中没有定义proxy_set_header指令时,会从上面的级别继承配置。 默认情况下,只有两个请求头会被重新定义:
proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;
nginx对于upstream默认使用的是基于IP的转发,因此对于以下配置:
 
upstream backend {  
    server 127.0.0.1:8080;  
}  
upstream crmtest {  
    server crmtest.aty.sohuno.com;  
}  
server {  
        listen       80;  
        server_name  chuan.aty.sohuno.com;  
        proxy_set_header Host $http_host;  
        proxy_set_header x-forwarded-for  $remote_addr;  
        proxy_buffer_size         64k;  
        proxy_buffers             32 64k;  
        charset utf-8;  
  
        access_log  logs/host.access.log  main;  
        location = /50x.html {  
            root   html;  
        }  
    location / {  
        proxy_pass backend ;  
    }  
          
    location = /customer/straightcustomer/download {  
        proxy_pass http://crmtest;  
        proxy_set_header Host $proxy_host;  
    }  
}  
 
当匹配到/customer/straightcustomer/download时,使用crmtest处理,到upstream就匹配到crmtest.aty.sohuno.com,这里直接转换成IP进行转发了。假如crmtest.aty.sohuno.com是在另一台nginx下配置的,ip为10.22.10.116,则$proxy_host则对应为10.22.10.116。此时相当于设置了Host为10.22.10.116。如果想让Host是crmtest.aty.sohuno.com,则进行如下设置:
 
proxy_set_header Host crmtest.aty.sohuno.com;

 解决的问题=>

location /transportation.portal {
proxy_set_header Host stargate.arun.me;(这里需要显示的指定头)
proxy_set_header X-Forwarded-Host $host;  #参考:http://www.jianshu.com/p/6e86903d74f7,https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host

X-Forwarded-Host 一个事实标准,用来标识客户端在HTTP请求头中请求的原始host,因为主机名或者反向代理的端口可能与处理请求的原始服务器不同===>
X-Forwarded-Host: en.wikipedia.org:8080
X-Forwarded-Host: en.wikipedia.org

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

X-Forwarded-For 一个事实标准,用来标识客户端通过HTTP代理或者负载均衡器连接的web服务器的原始IP地址===>
X-Forwarded-For: client1, proxy1, proxy2
X-Forwarded-For: 129.78.138.66, 129.78.64.103

proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://xxx-base-stargate-ops-web-1.vm.elenet.me;(stargate.arun.me也是一个Nginx)
# proxy_pass http://stargate.alpha.elenet.me;
}

posted @ 2017-05-05 03:12  arun_yh  阅读(731)  评论(0编辑  收藏  举报