1. 程式人生 > >結合http詳解基於域名的虛擬主機訪問詳細原理及過程

結合http詳解基於域名的虛擬主機訪問詳細原理及過程

linux

服務器如何響應

[[email protected] blog]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5784/nginx

啟動nginx服務,系統就監聽了本機的80端口(80端口是本機的所有網卡),所以只要客戶端請求任意一塊網卡的IP 80端口,nginx都會響應,客戶端請求任意一塊網卡ip的80端口,nginx服務都會看請求報文中其中有一個叫host:www.etiantian.org ,nginx服務器接收到請求報文後請求的域名,端口 ,會從nginx.conf配置文件中找虛擬主機,如果客戶端的請求報文中沒有虛擬主機名只有1個ip地址名字,那麽nginx服務器只能把nginx.conf配置文件中默認第一個虛擬主機名以響應報文的形式發給客戶端。本文中下文中第一個默認虛擬主機名為www.etiantian.org

也即是如果客戶端直接輸入nginx的ip地址,那麽nginx服務器響應報文的時候默認響應第一個虛擬主機名字給客戶端。

[[email protected] conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}


本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1957731

結合http詳解基於域名的虛擬主機訪問詳細原理及過程