深入淺析Nginx虛擬主機
一虛擬主機
1.1虛擬主機概念
對于Nginx而言,每一個虛擬主機相當于一個在同一臺服務器中卻相互獨立的站點,從而實現(xiàn)一臺主機對外提供多個 web 服務,每個虛擬主機之間是獨立的,互不影響的。
1.2虛擬主機類型
通過 Nginx 可以實現(xiàn)虛擬主機的配置,Nginx 支持三種類型的虛擬主機配置:
- 基于 IP 的虛擬主機(較少使用)
- 基于域名的虛擬主機
- 基于端口的虛擬主機
二基于IP虛擬主機
2.1配置多IP地址
[root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0 [root@nginx ~]# ip addr | grep 172 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
提示:如上在同一臺主機添加多個IP地址。
2.2創(chuàng)建站點目錄
[root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/ [root@nginx ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html [root@nginx ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html
2.3配置虛擬主機
[root@nginx ~]# vi /etc/nginx/conf.d/ipvhost.conf server { listen ; #監(jiān)聽端口 server_name ipvhost.odocker.com ...; #配置虛擬主機名和IP location / { root /usr/share/nginx/ipvhost; #請求匹配路徑 index index.html; #指定主頁 access_log /var/log/nginx/ipvhost.access.log main; error_log /var/log/nginx/ipvhost.error.log warn; } } server { listen ; server_name ipvhost.odocker.com ...; location / { root /usr/share/nginx/ipvhost; index index.html; access_log /var/log/nginx/ipvhost.access.log main; error_log /var/log/nginx/ipvhost.error.log warn; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 [root@nginx ~]# nginx -s reload #重載配置文件
2.4確認驗證
瀏覽器訪問:ipvhost01.odocker.com。
clipboard
瀏覽器訪問:ipvhost02.odocker.com。
clipboard
三基于域名虛擬主機
3.1創(chuàng)建站點目錄
[root@nginx ~]# mkdir /usr/share/nginx/webvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/ [root@nginx ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html [root@nginx ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html
3.2配置虛擬主機
[root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf server { listen ; server_name webvhost.odocker.com; location / { root /usr/share/nginx/webvhost; index index.html; access_log /var/log/nginx/webvhost.access.log main; error_log /var/log/nginx/webvhost.error.log warn; } } server { listen ; server_name webvhost.odocker.com; location / { root /usr/share/nginx/webvhost; index index.html; access_log /var/log/nginx/webvhost.access.log main; error_log /var/log/nginx/webvhost.error.log warn; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 [root@nginx ~]# nginx -s reload #重載配置文件
3.3確認驗證
瀏覽器訪問:webvhost01.odocker.com。
clipboard
瀏覽器訪問:webvhost02.odocker.com。
clipboard
四基于端口虛擬主機
4.1創(chuàng)建站點目錄
[root@nginx ~]# mkdir /usr/share/nginx/portvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/ [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html
4.2配置虛擬主機
[root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf server { listen ; server_name portvhost.odocker.com; location / { root /usr/share/nginx/portvhost; index index.html; access_log /var/log/nginx/portvhost.access.log main; error_log /var/log/nginx/portvhost.error.log warn; } } server { listen ; server_name portvhost.odocker.com; location / { root /usr/share/nginx/portvhost; index index.html; access_log /var/log/nginx/access_portvhost.log main; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 [root@nginx ~]# nginx -s reload #重載配置文件
4.3確認驗證
瀏覽器訪問:portvhost01.odocker.com:8080
clipboard
瀏覽器訪問:portvhost02.odocker.com:8081
clipboard
到此這篇關于Nginx虛擬主機的文章就介紹到這了,更多相關Nginx虛擬主機內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。