深入淺析Nginx虛擬主機(jī)
一 虛擬主機(jī)
1.1 虛擬主機(jī)概念
對(duì)于Nginx而言,每一個(gè)虛擬主機(jī)相當(dāng)于一個(gè)在同一臺(tái)服務(wù)器中卻相互獨(dú)立的站點(diǎn),從而實(shí)現(xiàn)一臺(tái)主機(jī)對(duì)外提供多個(gè) web 服務(wù),每個(gè)虛擬主機(jī)之間是獨(dú)立的,互不影響的。
1.2 虛擬主機(jī)類型
通過(guò) Nginx 可以實(shí)現(xiàn)虛擬主機(jī)的配置,Nginx 支持三種類型的虛擬主機(jī)配置:
- 基于 IP 的虛擬主機(jī)(較少使用)
- 基于域名的虛擬主機(jī)
- 基于端口的虛擬主機(jī)
二 基于IP虛擬主機(jī)
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
提示:如上在同一臺(tái)主機(jī)添加多個(gè)IP地址。
2.2 創(chuàng)建站點(diǎn)目錄
[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 配置虛擬主機(jī)
[root@nginx ~]# vi /etc/nginx/conf.d/ipvhost.conf server { listen ; #監(jiān)聽(tīng)端口 server_name ipvhost.odocker.com ...; #配置虛擬主機(jī)名和IP location / { root /usr/share/nginx/ipvhost; #請(qǐng)求匹配路徑 index index.html; #指定主頁(yè) 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 確認(rèn)驗(yàn)證
瀏覽器訪問(wèn):ipvhost01.odocker.com。
clipboard
瀏覽器訪問(wèn):ipvhost02.odocker.com。
clipboard
三 基于域名虛擬主機(jī)
3.1 創(chuàng)建站點(diǎn)目錄
[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 配置虛擬主機(jī)
[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 確認(rèn)驗(yàn)證
瀏覽器訪問(wèn):webvhost01.odocker.com。
clipboard
瀏覽器訪問(wèn):webvhost02.odocker.com。
clipboard
四 基于端口虛擬主機(jī)
4.1 創(chuàng)建站點(diǎn)目錄
[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 配置虛擬主機(jī)
[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 確認(rèn)驗(yàn)證
瀏覽器訪問(wèn):portvhost01.odocker.com:8080
clipboard
瀏覽器訪問(wèn):portvhost02.odocker.com:8081
clipboard
到此這篇關(guān)于Nginx虛擬主機(jī)的文章就介紹到這了,更多相關(guān)Nginx虛擬主機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Nginx實(shí)現(xiàn)三種常見(jiàn)的虛擬主機(jī)配置方法
- Nginx多虛擬主機(jī)配置小結(jié)
- nginx基于IP的多虛擬主機(jī)實(shí)現(xiàn)
- Nginx虛擬主機(jī)的配置實(shí)現(xiàn)
- Apache和Nginx實(shí)現(xiàn)虛擬主機(jī)的3種方式小結(jié)
- Nginx虛擬主機(jī)的六種配置(最全)
- Nginx虛擬主機(jī)的配置步驟過(guò)程全解
- 關(guān)于Nginx中虛擬主機(jī)的一些冷門知識(shí)小結(jié)
- Nginx虛擬主機(jī)的搭建的實(shí)現(xiàn)步驟
- nginx配置虛擬主機(jī)的詳細(xì)步驟
- Ubuntu中Nginx虛擬主機(jī)設(shè)置的項(xiàng)目實(shí)踐
相關(guān)文章
Nginx配置動(dòng)態(tài)代理后通過(guò)curl訪問(wèn)報(bào)403問(wèn)題
本文主要介紹了Nginx配置動(dòng)態(tài)代理后通過(guò)curl訪問(wèn)報(bào)403問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例
這篇文章主要介紹了Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11nginx與apache限制ip并發(fā)訪問(wèn) 限制ip連接的設(shè)置方法
nginx限制ip并發(fā)數(shù),也是說(shuō)限制同一個(gè)ip同時(shí)連接服務(wù)器的數(shù)量,要使apache服務(wù)器做對(duì)同一IP地址的連接限制,需要mod_limitipconn來(lái)實(shí)現(xiàn)。一般需要手動(dòng)編譯。不過(guò)模塊作者也提供了一些編譯好的模塊,根據(jù)自己的apache版本可以直接使用2012-11-11Nginx加固的幾種方式(控制超時(shí)時(shí)間&限制客戶端下載速度&并發(fā)連接數(shù))
本文主要介紹了Nginx加固的幾種方式,包括控制超時(shí)時(shí)間,限制客戶端下載速度,并發(fā)連接數(shù)這幾種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03nginx服務(wù)器異常502 bad gateway原因排查
這篇文章主要介紹了nginx服務(wù)器異常502 bad gateway原因排查,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08