十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章給大家分享的是有關(guān)LNMP架構(gòu)中Nginx如何配置虛擬主機(jī)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),西鄉(xiāng)企業(yè)網(wǎng)站建設(shè),西鄉(xiāng)品牌網(wǎng)站建設(shè),網(wǎng)站定制,西鄉(xiāng)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,西鄉(xiāng)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
1、nginx虛擬主機(jī)簡(jiǎn)單介紹
同apache服務(wù)一樣,它也有三種不同的虛擬主機(jī),基于域名的虛擬主機(jī)、基于IP的虛擬主機(jī)與基于端口的虛擬主機(jī),至于其中的區(qū)別,請(qǐng)參考前面的 apache服務(wù)器的虛擬主機(jī)章節(jié)的相關(guān)介紹
2、nginx虛擬主機(jī)配置環(huán)境
系統(tǒng)環(huán)境
[root@centos6 scripts]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@centos6 scripts]# uname -r
2.6.32-431.el6.x86_64
nginx服務(wù)的版本
[root@centos6 scripts]# /application/nginx/sbin/nginx -v
nginx version: nginx/1.10.1
3、nginx虛擬主機(jī)配置準(zhǔn)備
生產(chǎn)環(huán)境的規(guī)范很重要,這是運(yùn)維的重點(diǎn),因此,配置前創(chuàng)建站點(diǎn)目錄
[root@centos6 ~]# mkdir /www/{www,bbs,blog} -p
[root@centos6 ~]# tree /www
/www
+-- bbs
+-- blog
+-- www
3 directories, 0 files
用于存放站點(diǎn)文件的目錄
---------------
授權(quán)目錄
--------------
[root@centos6 ~]# chown -R nginx.nginx /www
[root@centos6 ~]# ll /www/
total 12
drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 bbs
drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 blog
drwxr-xr-x. 2 nginx nginx 4096 Dec 4 18:38 www
------------------------------------------------------
接下來寫點(diǎn)東東進(jìn)去吧,用于后面的測(cè)試
-----------------------------------------------------
[root@centos6 ~]# echo "welcont to mingongge's web stie" >/www/www/index.html
[root@centos6 ~]# echo "welcont to mingongge's bbs stie" >/www/bbs/index.html
[root@centos6 ~]# echo "welcont to mingongge's blog stie" >/www/blog/index.html
[root@centos6 ~]# cat /www/www/index.html
welcont to mingongge's web stie
[root@centos6 ~]# cat /www/bbs/index.html
welcont to mingongge's bbs stie
[root@centos6 ~]# cat /www/blog/index.html
welcont to mingongge's blog stie
生產(chǎn)環(huán)境檢查也同樣很重要,檢查、檢查 、檢查,重要的事情說三遍!?。。?/strong>
4、nginx虛擬主機(jī)配置
配置nginx 虛擬主機(jī)有兩種方式,一種可以像前面apache服務(wù)這種,單獨(dú)配置一個(gè)虛擬主機(jī)的配置文件,另一種也可以在主配置文件 nginx.conf中添加server標(biāo)簽,接下來介紹的是第一種方式,通過在配置文件里添加包含關(guān)系,單獨(dú)配置虛擬主機(jī)的配置文件目錄與配置文件,這樣實(shí)際生產(chǎn)環(huán)境中比較常見,服務(wù)多了,也容易維護(hù)。
--------------------
配置主配置文件
-------------------
只需要在主配置文件nginx.conf最后一行加下如下配置
include extra/vhosts/*.conf;
-----------------------------
創(chuàng)建虛擬主機(jī)配置目錄
----------------------------
[root@centos6 conf]# pwd
/application/nginx/conf
[root@centos6 conf]# mkdir -p extra/vhosts
-----------------------------
創(chuàng)建虛擬主機(jī)配置文件
----------------------------
[root@centos6 conf]# cd extra/vhosts/
[root@centos6 vhosts]# mkdir bbs www blog
[root@centos6 vhosts]# cp ../../nginx.conf www/www.conf
[root@centos6 vhosts]# cp ../../nginx.conf bbs/bbs.conf
[root@centos6 vhosts]# cp ../../nginx.conf blog/blog.conf
通過主配置文件來修改,其實(shí)只需要里面的server標(biāo)簽的內(nèi)容,同樣也可以做一個(gè)模板文件出來,通過cp命令改名后,再修改其內(nèi)容,效果都一樣
-----------------------------
配置虛擬主機(jī)配置文件
----------------------------
WWW站點(diǎn)虛擬主機(jī)配置文件(比較簡(jiǎn)單)
server {
listen 80;
server_name www.mingongge.com;
location / {
root /www/www;
index index.html index.htm;
}
}
基它的相同,只需要改下站點(diǎn)目錄路徑與域名信息
BBS站點(diǎn)虛擬主機(jī)配置文件
server {
listen 80;
server_name bbs.mingongge.com;
location / {
root /www/bbs;
index index.html index.htm;
}
}
BLOG站點(diǎn)虛擬主機(jī)配置文件
server {
listen 80;
server_name blog.mingongge.com;
location / {
root /www/blog;
index index.html index.htm;
}
}
5、重啟服務(wù)與測(cè)試訪問
[root@centos6 vhosts]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful
[root@centos6 vhosts]# /application/nginx/sbin/nginx -s reload
[root@centos6 vhosts]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 2469 root 6u IPv4 15462 0t0 TCP *:http (LISTEN)
nginx 2519 nginx 6u IPv4 15462 0t0 TCP *:http (LISTEN)
[root@centos6 vhosts]# ps -ef|grep nginx
root 2469 1 0 18:47 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 2519 2469 0 19:14 ?00:00:00 nginx: worker process
打開瀏覽器測(cè)試訪問吧
本地DNS解析不要忘記了,否則無法通過域名來訪問的
感謝各位的閱讀!關(guān)于“LNMP架構(gòu)中Nginx如何配置虛擬主機(jī)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!