`
liuluo129
  • 浏览: 114966 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

proxy_set_header设置Host为$proxy_host,$host与$local_host的区别

阅读更多
先来看下proxy_set_header的语法
语法: proxy_set_header field value;
默认值:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
上下文: httpserverlocation

允许重新定义或者添加发往后端服务器的请求头。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;

如果不想改变请求头“Host”的值,可以这样来设置:

proxy_set_header Host       $http_host;

但是,如果客户端请求头中没有携带这个头部,那么传递到后端服务器的请求也不含这个头部。 这种情况下,更好的方式是使用$host变量——它的值在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名:

proxy_set_header Host       $host;

此外,服务器名可以和后端服务器的端口一起传送:

proxy_set_header Host       $host:$proxy_port;

如果某个请求头的值为空,那么这个请求头将不会传送给后端服务器:

proxy_set_header Accept-Encoding "";
0
2
分享到:
评论

相关推荐

    nginx 反向代理软件

    # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Scheme $scheme; # proxy_pass http://192.168.2.37; #} location / { proxy...

    ASP.NET Core应用程序部署至生产环境中(CentOS7)

    Centos7发布说明 环境说明: 服务器系统:CentOS 7.2.1511 相关工具:Xshel、Xftp 服务器软件软件:.netcore、nginx、supervisor ... proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }

    tomcat8 + nginx + memcached + cas 实现负载均衡的配置包

    proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; ...

    Nginx+Tomcat负载均衡

    proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect...

    zbxtable-web:zbxtable-web

    English | zbxtable-web ZbxTable前端 入门 npm i npm run start 造访 建造 nodejs> 10 npm i npm run build 生成的代码将存储在/ app / build文件夹中 ... proxy_set_header Host $http_host;

    windows版curl

    --proxy-passwd=PASS set PASS as proxy password. -s, --save-headers save the HTTP headers to file. -U, --user-agent=AGENT identify as AGENT instead of Wget/VERSION. FTP options: --retr-...

    windows版wget命令

    --proxy-passwd=PASS set PASS as proxy password. -s, --save-headers save the HTTP headers to file. -U, --user-agent=AGENT identify as AGENT instead of Wget/VERSION. FTP options: --retr-...

    php.ini-development

    previously set variable or directive (e.g. ${foo}) ; Expressions in the INI file are limited to bitwise operators and parentheses: ; | bitwise OR ; ^ bitwise XOR ; & bitwise AND ; ~ bitwise NOT ; ! ...

    CISCO 技术大集合

    同时由于路由器Router3除了与路由器Router2相连外,不再与其他路由器相连,所以也可以为它赋予一条默认路由以代替以上的二条静态路由, ip route 0.0.0.0 0.0.0.0 192.1.0.65 即只要没有在路由表里找到去特定目的...

    wget-1.11.4-1

    --proxy-password=PASS set PASS as proxy password. --referer=URL include `Referer: URL' header in HTTP request. --save-headers save the HTTP headers to file. -U, --user-agent=AGENT identify as ...

    计算机网络第六版答案

    From Wikipedia: Diplomatic protocol is commonly described as a set of international courtesy rules. These well-established and time-honored rules have made it easier for nations and people to live ...

    python3.6.5参考手册 chm

    PEP 495: Local Time Disambiguation PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP ...

Global site tag (gtag.js) - Google Analytics