Http請求頭Host字段作用

HTTP/1.0不支持Host請求頭;而在HTTP/1.1中,Host請求頭部必須存在,否則會返回400 Bad Request
Host的作用是實現多個虛擬主機

假如在192.168.9.10機器上部署三個站點:www.baidu.com,www.taobao.com和www.jd.com
用nginx配置就是
http {

    server {
        server_name www.baidu.com;
    }
    server {
        server_name www.taobao.com;
    }
    server {
        server_name www.jd.com;
    }

}

1、curl -I "http://192.168.9.10/index.html" -H "host: www.baidu.com" -v
訪問www.baidu.com的index.html

2、curl -I "http://192.168.9.10/index.html" -H "host: www.taobao.com" -v
訪問www.taobao.com的index.html

3、curl -I "http://192.168.9.10/index.html" -H "host: www.jd.com" -v
訪問www.jd.com的index.html

假設在DNS配置了www.baidu.com,www.taobao.com和www.jd.com 都指向192.168.9.10
則curl -I "http://www.baidu.com/index.html" -v 會自動將www.baidu.com填充到Host字段中
curl -I "http://192.168.9.10/index.html" -v 會自動將192.168.9.10填充到Host字段中,由於nginx沒有配置192.168.9.10的server_name,所以此請求會報錯
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章