Nginx虛擬主機配置實踐(一)
成都創新互聯是專業的輪臺網站建設公司,輪臺接單;提供成都網站設計、成都網站制作,網頁設計,網站設計,建網站,PHP網站建設等專業做網站服務;采用PHP框架,可快速的進行輪臺網站開發網頁制作和功能擴展;專業做搜索引擎喜愛的網站,專業的做網站團隊,希望更多企業前來合作!一、虛擬主機的概念
在Web服務里虛擬主機就是一個獨立的網站站點,這個站點對應獨立的域名(也可能是IP或端口),具有獨立的程序及資源目錄,可以獨立的對外提供服務供用戶訪問。
二、虛擬主機的類型
基于域名的虛擬主機
基于端口的虛擬主機
基于IP的虛擬主機
說明:實際生產中用的最多的就是基于域名的虛擬主機,其他兩種了解即可。
三、基于一個域名虛擬主機的配置
Nginx主配置文件結構

創建一個最簡化的Nginx主配置文件
[root@nginx-oldboy conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
說明:Nginx的主配置文件是nginx.conf,nginx.conf.default與nginx.conf內容是一樣的。
執行上述命令之后,得到如下內容:

修改如下內容:
12 server_name www.afeilinux.com;
14 root html/wtf;

創建域名對應的站點目錄及文件
[root@nginx-oldboy nginx1.10]# mkdir -p html/wtf
[root@nginx-oldboy nginx1.10]# cd html/ && ls
50x.html index.html wtf
[root@nginx-oldboy html]# echo "第一次測試" > ./wtf/index.html
[root@nginx-oldboy html]# cat ./wtf/index.html
第一次測試
說明:上述命令的作用是創建一個html/wtf站點目錄,對應于主機配置文件里root根目錄的html/wtf設置(root html/wtf;)。然后生成一個默認的首頁文件index.html,文件內容是“第一次測試”。
nginx語法檢查并重新加載
[root@nginx-oldboy html]# nginx -t

[root@nginx-oldboy html]# nginx -s reload
說明:如果沒有啟動nginx,則無法reload。報錯內容如下:

查看監聽端口
[root@nginx-oldboy html]# netstat -lnp |grep nginx

修改hosts配置文件
[root@nginx-oldboy html]# echo "192.168.100.116 www.afeilinux.com" >> /etc/hosts
查看一下:
[root@nginx-oldboy html]# tail -n1 /etc/hosts
192.168.100.116 www.afeilinux.com
在windows端也要修改hosts配置文件。
測試域名站點

四、基于多個域名虛擬主機的配置
增加新域名對應的配置
上面已經有了一個www.afeilinux.com虛擬主機的配置,下面再增加一個www.afeilinux.org虛擬主機的配置。增加的主機一定要在nginx.conf的http{}區塊內,最好放在www.afeilinux.com虛擬主機配置的下面。
server {
listen 80;
server_name www.afeilinux.org;
location / {
root html/org;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

創建新虛擬機主機站點對應的目錄和文件
[root@nginx-oldboy nginx1.10]# mkdir ./html/org
[root@nginx-oldboy nginx1.10]# echo "第二次測試" > ./html/org/index.html
[root@nginx-oldboy nginx1.10]# cat !$
cat ./html/org/index.html
第二次測試
檢查下站點目錄結構
[root@nginx-oldboy nginx1.10]# tree
-bash: tree: command not found
解決方法:
[root@nginx-oldboy nginx1.10]# yum install -y tree
[root@nginx-oldboy nginx1.10]# tree html/

檢查語法并重新加載nginx配置
[root@nginx-oldboy ~]# nginx -t
[root@nginx-oldboy ~]# nginx -s reload
測試域名站點

五、規范和優化nginx配置文件
將虛擬主機配置成單獨的配置文件與nginx主配置文件nginx.conf分開
說明:
(1)適用于虛擬主機數量不多的情況;
(2)主配置文件包含的所有虛擬主機的子配置文件會統一放在extra目錄中;
(3)虛擬主機配置單獨的配置文件,使用參數include,它可以放在nginx主配置文件中任何位置。
[root@nginx-oldboy conf]# mkdir extra
[root@nginx-oldboy conf]# sed -n '10,21p' nginx.conf

[root@nginx-oldboy conf]# sed -n '10,21p' nginx.conf > extra/wtf.conf
[root@nginx-oldboy conf]# cat extra/wtf.conf
[root@nginx-oldboy conf]# sed -n '22,33p' nginx.conf > extra/org.conf
[root@nginx-oldboy conf]# cat extra/org.conf
更改主配置文件nginx.conf
刪除主配置文件nginx.conf中所有虛擬主機的配置,包含server{}標簽。
[root@nginx-oldboy conf]# sed -i '10,33d' nginx.conf
[root@nginx-oldboy conf]# cat nginx.conf

把虛擬主機獨立配置文件wtf.conf和org.conf的信息包含到nginx.conf里,這樣就把主配置文件和各個虛擬主機配置分離了。
include extra/wtf.conf
include extra/org.conf
[root@nginx-oldboy conf]# sed -i '10 i include extra/wtf.conf;\ninclude extra/org.conf;' nginx.conf

nginx語法檢測和重新加載
[root@nginx-oldboy conf]# nginx -t
[root@nginx-oldboy conf]# nginx -s reload
測試
[root@nginx-oldboy conf]# curl www.afeilinux.com
第一次測試
[root@nginx-oldboy conf]# curl www.afeilinux.org
第二次測試
[root@nginx-oldboy conf]# tree extra/

這樣虛擬主機配置文件就與nginx主配置文件分離開了!
另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文名稱:Nginx虛擬主機配置實踐(一)-創新互聯
當前鏈接:http://www.yijiale78.com/article16/cdpogg.html
成都網站建設公司_創新互聯,為您提供網站制作、App開發、用戶體驗、手機網站建設、App設計、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯