1. 程式人生 > >Nginx實現多虛擬主機配置

Nginx實現多虛擬主機配置

一、Nginx的應用概述

Nginx作為一款高效能的http 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器。主要有以下3方面的應用:

1、http伺服器

Nginx是一個http服務可以獨立提供http服務。可以做網頁靜態伺服器。

2、虛擬主機

可以實現在一臺伺服器虛擬出多個網站。例如個人網站使用的虛擬主機。

3、反向代理,負載均衡

當網站的訪問量達到一定程度後,單臺伺服器不能滿足使用者的請求時,需要用多臺伺服器叢集可以使用nginx做反向代理。並且多臺伺服器可以平均分擔負載,不會因為某臺伺服器負載高宕機而某臺伺服器閒置的情況。

二、什麼是虛擬主機      虛擬主機使用的是特殊的軟硬體技術,它把一臺伺服器主機分成一臺臺

虛擬的主機,每臺虛擬主機都可以具有獨立的域名,具有完整的Intemet伺服器功能(WWWFTPEmail等),同一臺主機上的虛擬主機之間是完全獨立的。從網站訪問者來看,每一臺虛擬主機和一臺獨立的主機完全一樣。利用虛擬主機,不必為每個要執行的網站提供一臺單獨的Nginx伺服器或單獨執行一組Nginx程序。虛擬主機提供了在同一臺伺服器、同一組Nginx程序上執行多個網站的功能。

可以實現在同一臺伺服器上執行多個網站,並且網站之間相互獨立各不干擾。

三、Nginx基於IP的虛擬主機配置

1、在Linux系統中虛擬出兩個網絡卡,設定為不同的IP地址

將/etc/sysconfig/network-scripts/ifcfg-eth0檔案複製一份,命名為ifcfg-eth0:1

cd /etc/sysconfig/network-scripts

cp ifcfg-eth0 ifcfg-eth0:1

修改其中內容:

DEVICE=eth0:1

IPADDR=192.168.72.49

其他項不用修改,然後執行service network restart 重啟網路服務

  1. 修改nginx配置檔案

    在nginx/conf/nginx.conf是nginx核心配置檔案,nginx對虛擬主機的配置,一個server就是一個虛擬主機。Nginx對於多虛擬主機的支援,主要是對server標籤的新增,指定location啟動路徑即可。

    1)先將/nginx/html檔案複製成兩份分別為html-48,html-49,修改html/index.html檔案,用於標記不同nginx首頁資訊。

    2)修改nginx.conf配置檔案,新增兩個server節點,指定ip

  1. server {  
  2.         listen       80;  
  3.         server_name  192.168.72.48;  
  4.         #charset koi8-r;  
  5.         #access_log  logs/host.access.log  main;  
  6.         location / {  
  7.             root   html-48;  
  8.             index  index.html index.htm;  
  9.         }         
  10.     }  
  11. server {  
  12.         listen       80;  
  13.         server_name  192.168.72.49;  
  14.         #charset koi8-r;  
  15.         #access_log  logs/host.access.log  main;  
  16.         location / {  
  17.             root   html-49;  
  18.             index  index.html index.htm;  
  19.         }          
  20.     }  
3、reload nginx配置檔案,命令:/nginx/bin/nginx -s reload

4、根據IP訪問首頁,效果圖

訪問192.168.72.48如下: 

訪問192.168.72.49,如下:

這就是實現了在同一臺硬體伺服器上,虛擬出兩個IP地址,使用1個nginx伺服器,分別對兩個ip進行訪問,分別跳轉到不同的html頁面。

四、Nginx基於埠的虛擬主機配置

1、同樣,複製兩個html檔案,修改index首頁資訊用於標記訪問的是那個虛擬機器

2、修改nginx.conf的對應server-port屬性值

  1. server {  
  2.         listen       81;  
  3.         server_name  192.168.72.49;  
  4.         #charset koi8-r;  
  5.         #access_log  logs/host.access.log  main;  
  6.         location / {  
  7.             root   html-81;  
  8.             index  index.html index.htm;  
  9.         }         
  10.     }  
  11. server {  
  12.         listen       82;  
  13.         server_name  192.168.72.49;  
  14.         #charset koi8-r;  
  15.         #access_log  logs/host.access.log  main;  
  16.         location / {  
  17.             root   html-82;  
  18.             index  index.html index.htm;  
  19.         }          
  20.     }  

3、訪問192.168.72.49:8081 和192.168.72.49:8081 兩個埠,可分別跳轉到兩個不同的index頁面,效果圖同上。

這就使得通過1個伺服器IP,虛擬出多個埠號釋出相同服務,瀏覽器根據輸入的某個埠號確定訪問對應埠釋出的服務。

五、Nginx基於域名的虛擬主機配置

Last but not least,基於域名的虛擬主機應用相對於前兩者是更常用,一般情況都是使用域名對網站進行訪問,很少有直接輸入該網站的伺服器IP。nginx基於域名的虛擬主機配置

1、首先設定host檔案,指定IP 對應的域名,使得原本需要通過DNS伺服器去解析域名所對應的IP,而host檔案相當於本地的一份ip-域名的對應資料快取,如果host中有這個對應關係,那訪問網站時,則直接跳轉到指定IP,而不再從DNS伺服器上解析。這裡可以藉助SwitchHosts工具修改如下。

192.168.72.49 xixi.max.com

2、修改nginx.conf配置

  1. server {  
  2.        listen       80;  
  3.        server_name  <atarget=_blankhref="http://www.max.com">www.max.com</a>;  
  4.        #charset koi8-r;  
  5.        #access_log  logs/host.access.log  main;  
  6.        location / {  
  7.            root   html-max;  
  8.            index  index.html index.htm;  
  9.        }  
  10.     }   
  11. server {  
  12.        listen       80;  
  13.        server_name  xixi.max.com;  
  14.        #charset koi8-r;  
  15.        #access_log  logs/host.access.log  main;  
  16.        location / {  
  17.            root   html-linxi;  
  18.            index  index.html index.htm;  
  19.        }  
  20.     }   

這裡通過修改server-name,nginx啟動會通過這個server-name域名到本地的host檔案中找到對應的IP,訪問到該IP下指定的location檔案地址。

訪問xixi.max.com

原部落格地址:http://blog.csdn.net/daybreak1209/article/details/51546332