什麼是虛擬主機
虛擬主機是一種特殊的軟硬體技術, 它可以將網路上的每一台電腦分成多個虛擬主機, 每個虛擬主機可以獨立對外提供www服務, 這樣就可以實現一台主機對外提供多個web服務, 每個虛擬主機之間是獨立的, 互不影響的。
通過nginx可以實現虛擬主機的配置, nginx支援三種類型的虛擬主機配置, 1、基於ip的虛擬主機, 2、基於功能變數名稱的虛擬主機 3、基於埠的虛擬主機
nginx設定檔的結構
nginx的設定檔結構如下:
......events {.......}http{.......server{.......}server{.......}}每個server就是一個虛擬主機。
一:.基於ip的虛擬主機配置
Linux作業系統允許添加IP別名, IP別名就是在一塊物理網卡上綁定多個lP位址。 這樣就能夠在使用單一網卡的同一個伺服器上運行多個基於IP的虛擬主機。
1.需求
一台nginx伺服器綁定兩個ip:192.168.101.3、192.168.101.103
訪問不同的ip請求不同的html目錄, 即:
訪問http://192.168.101.3將訪問“html3”目錄下的html網頁
訪問http://192.168.101.103將訪問“html103”目錄下的html網頁
2.準備環境
創建192.168.101.3虛擬機器, 保證本地電腦和虛擬網路通暢。
在192.168.101.3上安裝nginx。
3.html目錄創建
將原來nginx的html目錄拷貝兩個目錄 “html3”和“html103”, 為了方便測試需要修改每個目錄下的index.html內容使之個性化。
cd /usr/local/nginx
cp -r html html3
cp -r html html103
4.綁定多ip
方法一:
使用標準的網路配置工具(比如ifconfig和route命令)添加lP別名:
當前ip配置情況:
在eth0網卡再綁定一個ip:192.168.101.103
/sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.101.103 dev eth0:1
方法二:(推薦使用)
1、將/etc/sysconfig/network-scripts/ifcfg-eth0檔複製一份, 命名為ifcfg-eth0:1
修改其中內容:
DEVICE=eth0:1
IPADDR=192.168.25.103
其他項不用修改
2、重啟系統
5.配置虛擬主機
修改/usr/local/nginx/conf/nginx.conf檔, 添加兩個虛擬主機, 如下:
#user nobody;worker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置虛擬主機192.168.101.3server {#監聽的ip和埠, 配置192.168.101.3:80listen 80;#虛擬主機名稱稱這裡配置ip位址server_name 192.168.101.3;#所有的請求都以/開始, 所有的請求都可以匹配此locationlocation / {#使用root指令指定虛擬主機目錄即網頁存放目錄#比如訪問http://ip/test.html將找到/usr/local/html3/test.html#比如訪問http://ip/item/test.html將找到/usr/local/html3/item/test.htmlroot /usr/local/nginx/html3;#指定歡迎頁面, 按從左到右順序查找index index.html index.htm;}}#配置虛擬主機192.168.101.103server {listen 80;server_name 192.168.101.103;location / {root /usr/local/nginx/html103;index index.html index.htm;}}}6.測試
訪問http://192.168.101.3
訪問http://192.168.101.103
2.基於埠的虛擬主機
1.需求
nginx對外提供80和8080兩個埠監聽服務。
請求80埠則請求html80目錄下的html
請求8080埠則請求html8080目錄下的html
2.準備環境
創建192.168.101.3虛擬機器, 保證本地電腦和虛擬網路通暢。
在192.168.101.3上安裝nginx。
3.html目錄創建
將原來nginx的html目錄拷貝兩個目錄 “html80”和“html8080”, 為了方便測試需要修改每個目錄下的index.html內容使之個性化。
4.配置虛擬主機
修改/usr/local/nginx/conf/nginx.conf檔, 添加兩個虛擬主機, 如下:
#user nobody;worker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;#配置虛擬主機server {#監聽的ip和埠, 配置80listen 80;#虛擬主機名稱稱這裡配置ip位址server_name 192.168.101.3;#所有的請求都以/開始, 所有的請求都可以匹配此locationlocation / {#使用root指令指定虛擬主機目錄即網頁存放目錄#比如訪問http://ip/test.html將找到/usr/local/html3/test.html#比如訪問http://ip/item/test.html將找到/usr/local/html3/item/test.htmlroot /usr/local/nginx/html80;#指定歡迎頁面,5.測試
訪問http://192.168.101.3
訪問http://192.168.101.3:8080
3.基於功能變數名稱的虛擬主機配置
1.需求
兩個功能變數名稱指向同一台nginx伺服器,使用者訪問不同的功能變數名稱顯示不同的網頁內容。
兩個功能變數名稱是aaa.test.com和bbb.test.com
nginx伺服器使用虛擬機器192.168.101.3
2.準備環境
創建192.168.101.3虛擬機器,保證本地電腦和虛擬網路通暢。
在192.168.101.3上安裝nginx。
通過host檔指定aaa.test.com和bbb.test.com對應192.168.101.3虛擬機器:
修改window的hosts檔:(C:WindowsSystem32driversetc)
3.html目錄創建
在192.168.101.3上創建/usr/local/aaa_html,此目錄為aaa.test.com功能變數名稱訪問的目錄
在192.168.101.3上創建/usr/local/bbb_html,此目錄為bbb.test.com功能變數名稱訪問的目錄
目錄中的內容使用nginx自帶的html檔,將/usr/local/nginx/html中的內容拷貝分別拷貝到上邊兩個目錄中,並且將aaa_html目錄中的index.html內容改為:“Welcome to aaa nginx!”
將bbb_html目錄中的index.html內容改為“Welcome to bbb nginx!”
4.配置虛擬主機
修改/usr/local/nginx/conf/nginx.conf檔,添加兩個虛擬主機,如下:
#配置虛擬主機aaa.test.comserver {#監聽的ip和埠,配置本機ip和埠listen 192.168.101.3:80; #虛擬主機名稱稱是aaa.test.com,請求功能變數名稱aaa.test.com的url將由此server配置解析server_name aaa.test.com; #所有的請求都以/開始,所有的請求都可以匹配此locationlocation / {#使用root指令指定虛擬主機目錄即網頁存放目錄#比如訪問http://ip/test.html將找到/usr/local/aaa_html/test.html#比如訪問http://ip/item/test.html將找到/usr/local/aaa_html/item/test.htmlroot /usr/local/aaa_html; #指定歡迎頁面,按從左到右順序查找index index.html index.htm;}}#配置虛擬主機bbb.test.comserver {listen 192.168.101.3:80;server_name bbb.test.com;location / {root /usr/local/bbb_html;index index.html index.htm;}}5.測試
訪問aaa.test.com、bbb.test.com
3.基於功能變數名稱的虛擬主機配置
1.需求
兩個功能變數名稱指向同一台nginx伺服器,使用者訪問不同的功能變數名稱顯示不同的網頁內容。
兩個功能變數名稱是aaa.test.com和bbb.test.com
nginx伺服器使用虛擬機器192.168.101.3
2.準備環境
創建192.168.101.3虛擬機器,保證本地電腦和虛擬網路通暢。
在192.168.101.3上安裝nginx。
通過host檔指定aaa.test.com和bbb.test.com對應192.168.101.3虛擬機器:
修改window的hosts檔:(C:WindowsSystem32driversetc)
3.html目錄創建
在192.168.101.3上創建/usr/local/aaa_html,此目錄為aaa.test.com功能變數名稱訪問的目錄
在192.168.101.3上創建/usr/local/bbb_html,此目錄為bbb.test.com功能變數名稱訪問的目錄
目錄中的內容使用nginx自帶的html檔,將/usr/local/nginx/html中的內容拷貝分別拷貝到上邊兩個目錄中,並且將aaa_html目錄中的index.html內容改為:“Welcome to aaa nginx!”
將bbb_html目錄中的index.html內容改為“Welcome to bbb nginx!”
4.配置虛擬主機
修改/usr/local/nginx/conf/nginx.conf檔,添加兩個虛擬主機,如下:
#配置虛擬主機aaa.test.comserver {#監聽的ip和埠,配置本機ip和埠listen 192.168.101.3:80; #虛擬主機名稱稱是aaa.test.com,請求功能變數名稱aaa.test.com的url將由此server配置解析server_name aaa.test.com; #所有的請求都以/開始,所有的請求都可以匹配此locationlocation / {#使用root指令指定虛擬主機目錄即網頁存放目錄#比如訪問http://ip/test.html將找到/usr/local/aaa_html/test.html#比如訪問http://ip/item/test.html將找到/usr/local/aaa_html/item/test.htmlroot /usr/local/aaa_html; #指定歡迎頁面,按從左到右順序查找index index.html index.htm;}}#配置虛擬主機bbb.test.comserver {listen 192.168.101.3:80;server_name bbb.test.com;location / {root /usr/local/bbb_html;index index.html index.htm;}}5.測試
訪問aaa.test.com、bbb.test.com