天天看點

Nginx實戰基礎篇三 Nginx上虛拟主機的實作過程

利用虛拟主機技術,可以把一台真正的主機分成許多"虛拟"的主機,每一台虛拟主機都具有獨立的域名和IP位址,具有完整的Internet伺服器(www, FTP,email)功能。 虛拟主機之間完全獨立,在外界看來,每一台虛拟主機和一台獨立的主機完全一樣。效果一樣但費用卻大不一樣了。由于多台 虛拟主機 共享一台真實主機的資源,每個 虛拟主機使用者承受的硬體費用、網絡維護費用、通信線路的費用均大幅度降低,Internet真正成為人人用得起的網絡!

目前生産環境中,大多數服務提供商都采用了虛拟主機的方式為客戶提供web服務,虛拟主機包括基于IP的虛拟主機,基于端口的虛拟主機和基于名稱的虛拟主機,由于目前最流行的是基于名稱的虛拟主機,也就是可以通過相同端口、相同IP對應多個域名站點,本實驗以這種方式為主進行講解。

<b>一、建立站點目錄,首頁、權限</b>

[root@rhel6u3-7 ~]# uname -r //檢視系統核心版本号 2.6.32-279.el6.i686 [root@rhel6u3-7 ~]# cat /etc/redhat-release //檢視系統版本号 Red Hat Enterprise Linux Server release 6.3 (Santiago) [root@rhel6u3-7 ~]#

[root@rhel6u3-7 nginx]# pwd 

/usr/local/nginx 

[root@rhel6u3-7 nginx]# mkdir server  sites  //建立server字段配置檔案目錄為server,站點主目錄為sites 

[root@rhel6u3-7 nginx]# mkdir sites/www  sites/www1  //在站點主目錄中建立子站點目錄 

[root@rhel6u3-7 nginx]# echo "This is www.rsyslog.org" &gt;sites/www/index.html  //建立測試首頁 

[root@rhel6u3-7 nginx]# echo "This is www1.rsyslog.org" &gt;sites/www1/index.html //建立測試首頁 

[root@rhel6u3-7 nginx]# chown nginx. server/  sites/  -R //設定目前的屬主和屬組為nginx 

[root@rhel6u3-7 nginx]# vim conf/nginx.conf 

……… //在http子產品中添加server字段,其次在server字段中添加location字段即可 

    server { 

        listen       80;   //設定虛拟主機監聽端口為80 

        server_name  www.rsyslog.org;  //設定虛拟主機域名 

        location / { 

            root   sites/www;  //設定虛拟主機主目錄相對路徑 

            index  index.html index.htm; //設虛拟主機預設首頁 

        } 

location /status {  // 檢視nginx目前的狀态情況,需要子產品 “--with-http_stub_status_module”支援 

                stub_status on; 

                access_log /usr/local/nginx/logs/www_status.log; //設定日志存放位置并命名 

                auth_basic "NginxStatus"; } 

include /usr/local/nginx/server/www1.rsyslog.org;  //設定include語句指向www1站點server字段配置檔案位置 

…….. 

<b>三、編輯網站</b><b>www1.rsyslog.org</b><b>的</b><b>server</b><b>配置檔案</b>

[root@rhel6u3-7 ~]# cd /usr/local/nginx/server/ 

[root@rhel6u3-7 server]# vim www1.rsyslog.org 

server { 

     listen       80; 

     server_name  www1.rsyslog.org; 

      location / { 

            root   sites/www1; 

            index  index.html index.htm; 

     location /status {  // 檢視nginx目前的狀态情況,需要子產品 “--with-http_stub_status_module”支援 

                access_log /usr/local/nginx/logs/www1_status.log; 

    } 

<b>四,在</b><b>DNS</b><b>的區域檔案中添加兩條</b><b>A</b><b>記錄指向網站主機名</b><b></b>

<b></b>

//在DNS的區域檔案中添加兩個網站的A記錄 

www             A       192.168.100.107 

www1            A       192.168.100.107 

<b>五、啟動</b><b>nginx</b><b>服務,為了友善測試關閉防火牆并将</b><b>selinux</b><b>設定為</b><b>premissive</b><b>模式</b>

[root@rhel6u3-7 server]# /etc/rc.d/init.d/nginx restart 

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

Stopping nginx:                                           [  OK  ] 

Starting nginx:                                            [  OK  ] 

[root@rhel6u3-2 ~]# /etc/rc.d/init.d/iptables stop 

[root@rhel6u3-2 ~]# setenforce 0 

<b>六、通過</b><b>windows</b><b>系統測試,首先将網卡</b><b>DNS</b><b>設定為</b><b>192.168.100.102</b><b>,然後通過</b><b>nslookup</b><b>指令解析是否成功。</b>

<a href="http://blog.51cto.com/attachment/201302/222243553.png" target="_blank"></a>

<b>通過</b><b>IE</b><b>浏覽器通路</b>

<a href="http://blog.51cto.com/attachment/201302/222259594.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201302/222317134.png" target="_blank"></a>

<b>在網址後面加上</b><b>status</b><b>可以檢視網站目前的運作狀态</b>

<a href="http://blog.51cto.com/attachment/201302/222336780.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201302/222351801.png" target="_blank"></a>

本文轉自淩激冰51CTO部落格,原文連結:http://blog.51cto.com/dreamfire/1141018,如需轉載請自行聯系原作者

繼續閱讀