Nginx虛擬主機配置實踐(一)
成都創(chuàng)新互聯(lián)是專業(yè)的輪臺網(wǎng)站建設(shè)公司,輪臺接單;提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行輪臺網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!一、虛擬主機的概念
在Web服務(wù)里虛擬主機就是一個獨立的網(wǎng)站站點,這個站點對應(yīng)獨立的域名(也可能是IP或端口),具有獨立的程序及資源目錄,可以獨立的對外提供服務(wù)供用戶訪問。
二、虛擬主機的類型
基于域名的虛擬主機
基于端口的虛擬主機
基于IP的虛擬主機
說明:實際生產(chǎn)中用的最多的就是基于域名的虛擬主機,其他兩種了解即可。
三、基于一個域名虛擬主機的配置
Nginx主配置文件結(jié)構(gòu)
創(chuàng)建一個最簡化的Nginx主配置文件
[root@nginx-oldboy conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
說明:Nginx的主配置文件是nginx.conf,nginx.conf.default與nginx.conf內(nèi)容是一樣的。
執(zhí)行上述命令之后,得到如下內(nèi)容:
修改如下內(nèi)容:
12 server_name www.afeilinux.com;
14 root html/wtf;
創(chuàng)建域名對應(yīng)的站點目錄及文件
[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
第一次測試
說明:上述命令的作用是創(chuàng)建一個html/wtf站點目錄,對應(yīng)于主機配置文件里root根目錄的html/wtf設(shè)置(root html/wtf;)。然后生成一個默認的首頁文件index.html,文件內(nèi)容是“第一次測試”。
nginx語法檢查并重新加載
[root@nginx-oldboy html]# nginx -t
[root@nginx-oldboy html]# nginx -s reload
說明:如果沒有啟動nginx,則無法reload。報錯內(nèi)容如下:
查看監(jiān)聽端口
[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配置文件。
測試域名站點
四、基于多個域名虛擬主機的配置
增加新域名對應(yīng)的配置
上面已經(jīng)有了一個www.afeilinux.com虛擬主機的配置,下面再增加一個www.afeilinux.org虛擬主機的配置。增加的主機一定要在nginx.conf的http{}區(qū)塊內(nèi),最好放在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;
}
}
創(chuàng)建新虛擬機主機站點對應(yīng)的目錄和文件
[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
第二次測試
檢查下站點目錄結(jié)構(gòu)
[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
測試域名站點
五、規(guī)范和優(yōu)化nginx配置文件
將虛擬主機配置成單獨的配置文件與nginx主配置文件nginx.conf分開
說明:
(1)適用于虛擬主機數(shù)量不多的情況;
(2)主配置文件包含的所有虛擬主機的子配置文件會統(tǒng)一放在extra目錄中;
(3)虛擬主機配置單獨的配置文件,使用參數(shù)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{}標(biāo)簽。
[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主配置文件分離開了!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:Nginx虛擬主機配置實踐(一)-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://chinadenli.net/article16/cdpogg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App開發(fā)、用戶體驗、手機網(wǎng)站建設(shè)、App設(shè)計、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容