server name 匹配的是啥

server name 究竟是匹配host的值还是http_host的值,两者有什么差别?

$host is a variable of the Core module.

$host

This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.

This variable may have a different value from http_host in such cases: 1) when the Host input header is absent or has an empty value,host equals to the value of server_name directive; 2)when the value of Host contains port number, host doesn't include that port number.host's value is always lowercase since 0.8.17.

这个是在请求的header里的host值,如果请求的header中未定义Host值时,会用name of server processing的值代替;

测试:

#nginx version 1.8.4
#test.vip.com
server
  {
    listen       80;
    server_name  test.vip.com test2.vip.com;

    location /{
        return 200 $http_host"+"$host;
    }
}
 
#when set the request of the Host in the header
curl  -x "10.199.134.223:80" "http://test.vip.com/"     -H "Host:test2.vip.com"
test2.vip.com"+"test.vip.com
 
#the host is test.vip.com,why?as from the nginx.org,it shoud be "test2.vip.com"????
 
 
curl  -x "10.199.134.223:80" "http://test.vip.com/"     
test.vip.com"+"test.vip.com
 

$http_host is also a variable of the same module but you won't find it with that name because it is defined generically as $http_HEADER(ref).

$http_HEADER

The value of the HTTP request header HEADER when converted to lowercase and with 'dashes' converted to 'underscores', e.g. http_user_agent,http_referer...;

Summarizing:

$http_host equals always the HTTP_HOST request header.

host equalshttp_host,lowercase and without the port number(if present),except when HTTP_HOST is absent or is an empty value. In that case,$host equals the value of the server_name directive of the server which processed the request.

sever name test

#test.vip.com return the $http_host ane $host
#default_server return the "$http_host"
 
curl  -x "10.199.134.223:80" "http://test.vip.com/"    -H "Host:test2.vip.com"        
test2.vip.com"+"test.vip.com(test)
 
curl  -x "10.199.134.223:80" "http://test2.vip.com/"    -H "Host:test.vip.com"  
test.vip.com"+"test2.vip.com(test)
 
从以上两条测试看:$http_host 获取的是header里的host值,$HOST获取的是url里的host值;
 
 
 
 
curl  -x "10.199.134.223:80" "http://test.vip.com/"     
test.vip.com"+"test.vip.com(test)
 
#当请求头里的Host没指定的时候,http_host默认获取url里的host值
 
 
curl   "http://10.199.134.223/"    -H "Host:test.vip.com"                                     
test.vip.com"+"test.vip.com(test)
 
#当url里的host没指定时,host值为header里的host值
 
 
 
#那么server name 匹配哪一个?
curl  -x "10.199.134.223:80" "http://test.vip.com/"    -H "Host:test2.vip.com"        
test2.vip.com"+"test.vip.com(test)

curl  -x "10.199.134.223:80" "http://test2.vip.com/"    -H "Host:test.vip.com"  
test.vip.com"+"test2.vip.com(test)
 
curl  -x "10.199.134.223:80" "http://test2.vip.com/"    -H "Host:1.1.1.1"             
1.1.1.1"+"test2.vip.com(test)

curl  -x "10.199.134.223:80" "http://1.1.1.1/"    -H "Host:test.vip.com"                              
test.vip.com(default)
 
curl   "http://10.199.134.223:80/"    -H "Host:test.vip.com" 
test.vip.com"+"test.vip.com(test)
curl   "http://10.199.134.223/"    -H "Host:test.vip.com"    
test.vip.com"+"test.vip.com (test)
 
 
 
 
#以上两条测试,看起来server name匹配的是请求url里的host值
 

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,938评论 0 0
  • 练字时间:60分钟 练字内容:乏 今天把横折和走之分开了,有点四不像,感觉比连着写还要丑,另外横折的抗肩太刻意,明...
    唐卡1102阅读 221评论 0 0
  • 后来她走了,她何曾离开过呢?她:"一九九七年早春,阿媛去世,一九九八年岁末,钟书去世,我们三就彼此失散了,就这么轻...
    一头吹萨克斯的鹿阅读 279评论 1 1
  • Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种...
    Cooci_和谐学习_不急不躁阅读 412评论 0 2