利用虛拟主機技術,可以把一台真正的主機分成許多“虛拟”的主機,每一台虛拟主機都具有獨立的域名和IP位址,具有完整的Internet伺服器(www,FTP,email)功能。虛拟主機之間完全獨立,在外界看來,每一台虛拟主機和一台獨立的主機完全一樣。效果一樣但費用卻大不一樣了。由于多台虛拟主機共享一台真實主機的資源,每個虛拟主機使用者承受的硬體費用、網絡維護費用、通信線路的費用均大幅度降低,Internet真正成為人人用得起的網絡!
虛拟主機共分為三種:基于IP的虛拟主機,基于端口的虛拟主機和基于名稱的虛拟主機。前兩種由于受到成本和客戶使用習慣的限制,相對使用的沒有基于名稱的虛拟主機多,以下我們介紹一下三種虛拟主機的配置。
1. 基于名稱的虛拟主機
首先,建立虛拟主機的目錄,
<code>[root@master nginx]</code><code># mkdir -p /usr/local/nginx/virtual_sites/www.example.com</code>
<code>[root@master nginx]</code><code># cd /usr/local/nginx/virtual_sites/</code>
<code>[root@master www.example.com]</code><code># echo "Welcome to > index.html</code>
修改nginx的配置檔案,添加server字段,
<code>[root@master nginx]# grep -B2 -A2 www.example.com conf/nginx.conf</code>
<code> </code><code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name www.example.com;</code>
<code> </code><code>root virtual_sites/www.example.com;</code>
<code> </code><code>index index.html index.htm;</code>
<code> </code><code>}</code>
之後,重新開機nginx服務,
<code>[root@master nginx]</code><code># service nginx restart</code>
<code>nginx: the configuration </code><code>file</code> <code>/usr/local/nginx/conf/nginx</code><code>.conf syntax is ok</code>
<code>nginx: configuration </code><code>file</code> <code>/usr/local/nginx/conf/nginx</code><code>.conf </code><code>test</code> <code>is successful</code>
<code>Stopping nginx: [ OK ]</code>
<code>Starting nginx: [ OK ]</code>
<code>[root@master nginx]</code><code>#</code>
用戶端通路該虛拟主機,這裡并沒有配置DNS,使用了/etc/hosts作為域名解析,
<code>[root@slave ~]</code><code># links www.example.com</code>
<a href="http://s3.51cto.com/wyfs02/M02/6E/C1/wKiom1WFRSzCOeajAAKlSt0vRjc195.jpg" target="_blank"></a>
2. 基于IP的虛拟主機
<code> </code><code>listen 192.168.1.150:80;</code>
<code> </code><code>root virtual_sites/150.com;</code>
添加虛拟IP位址,并重新開機nginx,
<code>[root@master nginx]</code><code># ifconfig eth1:0 192.168.1.150</code>
使用netstat檢視,nginx是否啟動成功,
<code>[root@master nginx]</code><code># netstat -antup </code>
<code>Active Internet connections (servers and established)</code>
<code>Proto Recv-Q Send-Q Local Address Foreign Address State PID</code><code>/Program</code> <code>name </code>
<code>tcp 0 0 192.168.1.151:8080 0.0.0.0:* LISTEN 1501</code><code>/nginx</code>
<code>tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1501</code><code>/nginx</code>
<code>tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 801</code><code>/sshd</code>
<code>tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 877</code><code>/master</code>
<code>tcp 0 0 192.168.1.129:22 192.168.1.106:59326 ESTABLISHED 1191</code><code>/sshd</code>
<code>tcp 0 0 :::22 :::* LISTEN 801</code><code>/sshd</code>
<code>tcp 0 0 ::1:25 :::* LISTEN 877</code><code>/master</code>
<code>udp 0 0 0.0.0.0:68 0.0.0.0:* 1007</code><code>/dhclient</code>
用戶端來驗證一把,
<code>[root@slave ~]</code><code># links 192.168.1.150</code>
<a href="http://s3.51cto.com/wyfs02/M02/6E/BE/wKioL1WFSDnTmeTbAAKt3ci1lmo456.jpg" target="_blank"></a>
3. 基于端口的虛拟主機
<code> </code><code>listen 8080;</code>
<code> </code><code>root virtual_sites/151.com;</code>
配置起來很簡單,配置完畢,重新開機nginx服務,
<code>Starting nginx:</code>
檢視端口是否已監聽8080,
<code>[root@master nginx]</code><code># netstat -antup</code>
<code>tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1526</code><code>/nginx</code>
<code>tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1526</code><code>/nginx</code>
用戶端進行通路驗證,
<code>[root@slave ~]</code><code># links 192.168.1.151:8080</code>
<a href="http://s3.51cto.com/wyfs02/M02/6E/C1/wKiom1WFR5qTNJYVAAKtZbNEDnE672.jpg" target="_blank"></a>
版權聲明:原創作品,如需轉載,請注明出處。否則将追究法律責任
本文轉自 bigstone2012 51CTO部落格,原文連結:http://blog.51cto.com/lavenliu/1663866