美文网首页
server name 匹配的是啥

server name 匹配的是啥

作者: 小灰灰1999 | 来源:发表于2018-08-09 20:32 被阅读10次

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值
 

相关文章

网友评论

      本文标题:server name 匹配的是啥

      本文链接:https://www.haomeiwen.com/subject/abacbftx.html