在Nginx中部署基於IP的虛擬主機(轉)

在Nginx中部署基於IP的虛擬主機

1、虛擬主機概念html

      虛擬主機是在網絡服務器上劃分出必定的磁盤空間供用戶放置站點、應用組件等,提供必要的站點功能、數據存放和傳輸功能。所謂虛擬主機,也叫「網站空間」, 就是把一臺運行在互聯網上的服務器劃分紅多個「虛擬」的服務器,每個虛擬主機都具備獨立的域名和完整的Internet服務器(支持WWW、FTP、 E-mail等)功能,從用戶角度來看,每臺虛擬主機和一臺獨立的服務器徹底相同,在IP地址日益緊張的今天,基於域名的虛擬主機要比基於IP的虛擬主機 使用的更加普遍。linux

 

2、系統環境nginx

     系統平臺:RHEL 5.4瀏覽器

     Nginx版本:nginx-1.0.15服務器

3、配置基於IP的虛擬主機網絡

      Linux、FreeBSD操做系統都容許添加IP別名。IP別名即:能夠在一塊物理網卡上綁定多個IP地址。這樣就可以在使用單一網卡的同一個服務器上運行多個基於IP的虛擬主機。設置IP別名也很是容易,只須配置系統上的網絡接口,讓它監聽額外的lP地址。在Linux系統上,可使用標準的網絡配置工具(好比ifconfig和route命令)添加IP別名。app

1)、先用ifconfig命令查看該服務器的lP地址。tcp

[root@linux nginx]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:99:E4:21  
          inet addr:10.0.0.133  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::a00:27ff:fe99:e421/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7324 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2051 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:719461 (702.5 KiB)  TX bytes:308638 (301.4 KiB)工具


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:765 errors:0 dropped:0 overruns:0 frame:0
          TX packets:765 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:48577 (47.4 KiB)  TX bytes:48577 (47.4 KiB)

[root@linux nginx]#
2)、在eth0網卡設備上添加兩個lP別名10.0.0.189和10.0.0.190,經過ifconfig和route命令來進行:oop

[root@linux nginx]# ifconfig eth0:1 10.0.0.189 broadcast 10.255.255.255 netmask 255.0.0.0 up

[root@linux nginx]# route add -host 10.0.0.189 dev eth0:1    

[root@linux nginx]# ifconfig eth0:2 10.0.0.190 broadcast 10.255.255.255 netmask 255.0.0.0 up

[root@linux nginx]# route add -host 10.0.0.190 dev eth0:2  

3)、再執行ifconfig命令,就能夠看到eth0網卡設備上綁定了兩個lP別名

[root@linux nginx]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:99:E4:21  
          inet addr:10.0.0.133  Bcast:10.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::a00:27ff:fe99:e421/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7490 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2151 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:735277 (718.0 KiB)  TX bytes:342506 (334.4 KiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:99:E4:21  
          inet addr:10.0.0.189  Bcast:10.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:2    Link encap:Ethernet  HWaddr 08:00:27:99:E4:21  
          inet addr:10.0.0.190  Bcast:10.255.255.255  Mask:255.0.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:771 errors:0 dropped:0 overruns:0 frame:0
          TX packets:771 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:48897 (47.7 KiB)  TX bytes:48897 (47.7 KiB)

[root@linux nginx]#

4)、 從另一臺服務器Ping 10.0.0.189和10.0.0.190兩個IP,若是可以Ping通,則證實配置無誤。可是,經過ifconfig和route配置的IP別名在服 務器重啓後會消失,不過能夠將這兩條ifconng和route命令添加到/etc/rc.local文件中,讓系統開機時自動運行,如下是相關命令:
vi /etc/rc.local
在文件末尾增長如下內容,而後保存便可

ifconfig eth0:1 10.0.0.189 broadcast 10.255.255.255 netmask 255.0.0.0 up

route add -host 10.0.0.189 dev eth0:1    

ifconfig eth0:2 10.0.0.190 broadcast 10.255.255.255 netmask 255.0.0.0 up

route add -host 10.0.0.190 dev eth0:2  

5)、下面開始配置基於IP的虛擬主機,在Nginx配置文件(nginx.conf)中,分別對10.0.0.13三、10.0.0.18九、10.0.0.190三個IP配置三個純靜態HTML支持的虛擬主機。

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    #第一個虛擬主機
    server {
        listen       10.0.0.133:80;               #監聽的IP和端口
        server_name  10.0.0.133;              #主機名稱

        access_log  logs/host1.access.log  main;                #訪問日誌文件存放路徑

        location /
        {
            root /usr/local/nginx/html/host1;              #HTML網頁文件存放的目錄
            index  index.html index.htm;                    #默認首頁文件,順序從左到右,若是找不到index.html文件,則查找index.htm文件做爲首頁文件
        }
}
    #第二個虛擬主機
    server {
        listen       10.0.0.189:80;
        server_name  10.0.0.189;

        access_log  logs/host2.access.log  main;

        location /
        {
            root /usr/local/nginx/html/host2;
            index  index.html index.htm;
        }
}
    #第三個虛擬主機
    server {
        listen       10.0.0.190:80;
        server_name  10.0.0.190;

        access_log  logs/host3.access.log  main;

        location /
        {
            root /usr/local/nginx/html/host3;
            index  index.html index.htm;
        }
}

      從上面的配置文件中能夠看出,一段server{……}就是一個虛擬主機,若是要配置多個虛擬主機,創建多段server{……}配置便可,很是方便。監聽的IP和端口也能夠不寫IP地址,只寫端口,把它配置成"listen 80",則表示監聽該服務器上全部IP的80端口,可經過server_name區分不一樣 的虛擬主機。

4、測試

1)、用腳本重啓nginx。

[root@linux conf]# service nginx restart

2)、在/usr/local/nginx/html/下分別創建三個目錄host1,host2,host3。分別在三個目錄中放一個index.html 文件,分別寫上本身的IP地址;

3)、用瀏覽器訪問相應IP地址。

nginx

 

nginx

 

nginx

相關文章
相關標籤/搜索