nginx虛擬主機配置(3)

1. 虛擬主機概念

在Web服務裏面虛擬主機就是一個獨立的網站站點,這個站點對應獨立的域名(也可能是IP或端口),具有獨立的程序及資源目錄,可以獨立地對外提供服務供用戶訪問。

2. 虛擬主機類型

2.1. 基於域名的虛擬主機

不同虛擬主機對應一個獨立的域名

2.2. 基於端口的虛擬主機

不同虛擬主機端口號不同。也可以是同一個域名或同一個IP,但端口號不同

2.3. 基於IP的虛擬主機

不同虛擬主機IP地址也不同

3. 虛擬主機配置

3.1. 基於域名的虛擬主機

切換到nginx配置目錄下面

cd /application/nginx/conf

過濾掉包含#號和空行,生成新的配置文件

egrep -v "#|^$" nginx.conf.default > nginx.conf

更改配置文件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; # 端口,默認就是80
        server_name  www.ljmict.com;  # 域名
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

創建域名www.ljmict.com對應的站點目錄及文件

mkdir /application/nginx/html/www
echo "http://www.ljmict.com" > ../html/www/index.html

查看www.ljmict.com對應的首頁文件index.html

cat ../html/www/index.html
# 查看結果
http://www.ljmict.com

檢查修改過的nginx文件配置是否有語法問題

 /application/nginx/sbin/nginx -t

重新啓動nginx服務

 /application/nginx/sbin/nginx -s reload

客戶端測試
在客戶端測試之前,需要更改客戶端的hosts文件,MACLinuxhosts文件都在/etc/hosts文件,如果是windows系統hosts文件在C:\Windows\System32\drivers\etc目錄下。

echo "172.16.1.10 www.ljmict.com" >> /etc/hosts
curl www.ljmict.com
# 結果
http://www.ljmict.com

在瀏覽器中訪問
nginx虛擬主機配置(3)

3.1.1. 配置多個基於域名的虛擬主機

更改nginx配置文件

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.ljmict.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }   
    }
    server {
        listen       80;
        server_name  bbs.ljmict.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  blog.ljmict.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

創建對應的站點目錄及文件

mkdir /application/nginx/html/bbs
echo "http://bbs.ljmict.com" > /application/nginx/html/bbs/index.html
mkdir /application/nginx/html/blog
echo "http://blog.ljmict.com" > /application/nginx/html/blog/index.html

重新啓動nginx服務

/application/nginx/sbin/nginx -s reload

添加hosts記錄

echo "172.16.1.10 bbs.ljmict.com" >> /etc/hosts
echo "172.16.1.10 blog.ljmict.com" >> /etc/hosts

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

3.2. 基於端口的虛擬主機

更改nginx配置文件

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.ljmict.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;  # 端口更改成81
        server_name  bbs.ljmict.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       82;  # 端口更改成82
        server_name  blog.ljmict.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

重啓nginx服務

/application/nginx/sbin/nginx -s reload

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

3.3. 基於IP的虛擬主機

如果要配置基於IP的虛擬主機,就需要讓每個虛擬主機有不同的IP地址,在這裏我臨時在ens37網卡上增加2個不同的IP

ip addr add 172.16.1.11/24 dev ens37
ip addr add 172.16.1.12/24 dev ens37
# 注意:根據自己的網卡名稱來配置IP

更改nginx配置文件

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  172.16.1.10;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  172.16.1.11; # 臨時配置的IP
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
    server {
        listen       82;
        server_name  172.16.1.12; # 臨時配置的IP
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
    }
}

重新啓動nginx服務

/application/nginx/sbin/nginx -s reload

客戶端訪問
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)
nginx虛擬主機配置(3)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章