濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > nginx配置虛擬主機(jī)的詳細(xì)步驟

nginx配置虛擬主機(jī)的詳細(xì)步驟

熱門(mén)標(biāo)簽:在百度地圖標(biāo)注車(chē)輛 撫順移動(dòng)400電話(huà)申請(qǐng) 威海人工外呼系統(tǒng)供應(yīng)商 烏海智能電話(huà)機(jī)器人 藍(lán)點(diǎn)外呼系統(tǒng) 寧夏房產(chǎn)智能外呼系統(tǒng)要多少錢(qián) 400電話(huà)申請(qǐng)方案 貴陽(yáng)教育行業(yè)電話(huà)外呼系統(tǒng) 做外呼系統(tǒng)的公司違法嗎

虛擬主機(jī)使用的是特殊的軟硬件技術(shù),它把一臺(tái)運(yùn)行在因特網(wǎng)上的服務(wù)器主機(jī)分成一臺(tái)臺(tái)“虛擬”的主機(jī),每臺(tái)虛擬主機(jī)都可以是一個(gè)獨(dú)立的網(wǎng)站,可以具有獨(dú)立的域名,具有完整的Intemet服務(wù)器功能(WWW、FTP、Email等),同一臺(tái)主機(jī)上的虛擬主機(jī)之間是完全獨(dú)立的。從網(wǎng)站訪(fǎng)問(wèn)者來(lái)看,每一臺(tái)虛擬主機(jī)和一臺(tái)獨(dú)立的主機(jī)完全一樣。

利用虛擬主機(jī),不用為每個(gè)要運(yùn)行的網(wǎng)站提供一臺(tái)單獨(dú)的Nginx服務(wù)器或單獨(dú)運(yùn)行一組Nginx進(jìn)程。虛擬主機(jī)提供了在同一臺(tái)服務(wù)器、同一組Nginx進(jìn)程上運(yùn)行多個(gè)網(wǎng)站的功能。

配置虛擬主機(jī)有三種方法:

  • 基于域名的虛擬主機(jī) : 不同的域名、相同的IP(此方式應(yīng)用最廣泛)
  • 基于端口的虛擬主機(jī) : 不使用域名、IP來(lái)區(qū)分不同站點(diǎn)的內(nèi)容,而是用不同的TCP端口號(hào)
  • 基于IP地址的虛擬主機(jī) : 不同的域名、不同的IP ( 需要加網(wǎng)絡(luò)接口 ,應(yīng)用的不廣泛) 基于IP地址

方式一:多網(wǎng)卡多IP

兩個(gè)物理網(wǎng)卡,兩個(gè)IP

# 兩張物理網(wǎng)卡ens32和ens34
[root@nginx network-scripts]# ifconfig ens32 | awk 'NR==2 {print $2}'  
192.168.126.41

[root@nginx network-scripts]# ifconfig ens34 | awk 'NR==2 {print $2}'  
192.168.126.42

編輯配置文件,基于每個(gè)IP創(chuàng)建一個(gè)虛擬主機(jī)

# 為防止 /etc/nginx/conf.d/default.conf 配置文件影響,對(duì)其進(jìn)行重命名
[root@nginx ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default	 

[root@nginx ~]# vim /etc/nginx/conf.d/ip.conf
# ens32網(wǎng)卡對(duì)應(yīng)的虛擬主機(jī)
server {
  listen 192.168.126.41:80;

  location / {
    root /ip_ens32;
    index index.html;
  }
}

# ens34 網(wǎng)卡對(duì)應(yīng)的虛擬主機(jī)
server {
  listen 192.168.126.42:80;

  location / {
    root /ip_ens34;
    index index.html;
  }
}

創(chuàng)建虛擬主機(jī)的網(wǎng)頁(yè)文件目錄及文件

[root@nginx ~]# mkdir /ip_ens32
[root@nginx ~]# mkdir /ip_ens34

[root@nginx ~]# echo "ens32" > /ip_ens32/index.html
[root@nginx ~]# echo "ens34" > /ip_ens34/index.html

檢查配置文件的語(yǔ)法

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重載nginx服務(wù)

[root@nginx ~]# systemctl reload nginx

測(cè)試

[root@nginx ~]# curl 192.168.126.41
ens32
[root@nginx ~]# curl 192.168.126.42
ens34

方式二:?jiǎn)尉W(wǎng)卡多IP

為一個(gè)物理網(wǎng)卡配置多個(gè)ip

ip addr add IP/MASK dev 網(wǎng)卡名

# 刪除
ip addr del IP/MASK dev 網(wǎng)卡名

其余步驟同上面多網(wǎng)卡多IP的配置

基于端口


多使用于公司內(nèi)部,無(wú)法使用域名或沒(méi)有域名時(shí)

配置

[root@nginx ~]# vim /etc/nginx/conf.d/port.conf
server {
  listen 81;

  location / {
    root /port_81;
    index index.html;
  }
}

server {
  listen 82;

  location / {
    root /port_82;
    index index.html;
  }
}

[root@nginx ~]# mkdir /port_{81..82}
[root@nginx ~]# echo "81" > /port_81/index.html
[root@nginx ~]# echo "82" > /port_82/index.html

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx ~]# systemctl reload nginx

測(cè)試

[root@nginx ~]# curl 192.168.126.41:81
81
[root@nginx ~]# curl 192.168.126.41:82
82

基于域名

配置

一般一個(gè)域名對(duì)應(yīng)一個(gè)配置文件,便于管理

[root@nginx ~]# vim /etc/nginx/conf.d/test1.dxk.com.conf
server {
  listen 80;
  server_name test1.dxk.com;

  location / {
    root /test1;
    index index.html;
  }
}

[root@nginx ~]# vim /etc/nginx/conf.d/test2.dxk.com.conf
server {
  listen 80;
  server_name test2.dxk.com;

  location / {
    root /test2;
    index index.html;
  }
}

[root@nginx ~]# mkdir /test{1..2}
[root@nginx ~]# echo "test1" > /test1/index.html
[root@nginx ~]# echo "test2" > /test2/index.html

[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@nginx ~]# systemctl reload nginx

測(cè)試

# 配置域名解析
[root@nginx ~]# echo -e "192.168.126.41 test1.dxk.com\n192.168.126.41 test2.dxk.com" >> /etc/hosts
[root@nginx ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.126.41 test1.dxk.com
192.168.126.41 test2.dxk.com

[root@nginx ~]# curl test1.dxk.com
test1
[root@nginx ~]# curl test2.dxk.com
test2



這里有個(gè)問(wèn)題:

如果在配置域名解析時(shí)由于寫(xiě)錯(cuò)了,那么訪(fǎng)問(wèn)該錯(cuò)誤域名(未配置該錯(cuò)誤域名的虛擬主機(jī))時(shí)竟然還會(huì)返回網(wǎng)頁(yè)內(nèi)容。

[root@nginx ~]# vim /etc/hosts
192.168.126.41 test1.dxk.com
192.168.126.41 test3.dxk.com   # 這里本應(yīng)該是 test2.dxk.com ,但是由于寫(xiě)錯(cuò)了,而且對(duì)應(yīng)test3.dxk.com域名的虛擬主機(jī)并不存在

訪(fǎng)問(wèn)該錯(cuò)誤域名

[root@nginx ~]# curl test3.dxk.com
test1

# 可以看到,還是會(huì)返回網(wǎng)頁(yè)信息

因?yàn)樵谂渲糜蛎馕鰰r(shí),雖然域名寫(xiě)錯(cuò)了,但是IP是對(duì)的,那么此時(shí)服務(wù)端默認(rèn)會(huì)返回滿(mǎn)足是該IP且端口為80的排在第一個(gè)的虛擬主機(jī)的網(wǎng)頁(yè)信息給客戶(hù)端

[root@nginx ~]# ll /etc/nginx/conf.d/
-rw-r--r--. 1 root root  112 Jul  3 21:23 test1.dxk.com.conf
-rw-r--r--. 1 root root  112 Jul  3 21:22 test2.dxk.com.conf

這是需要注意的

到此這篇關(guān)于nginx虛擬主機(jī)的文章就介紹到這了,更多相關(guān)nginx虛擬主機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:慶陽(yáng) 那曲 銅川 泰州 周口 朝陽(yáng) 蕪湖 松原

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《nginx配置虛擬主機(jī)的詳細(xì)步驟》,本文關(guān)鍵詞  nginx,配置,虛擬主機(jī),的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《nginx配置虛擬主機(jī)的詳細(xì)步驟》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于nginx配置虛擬主機(jī)的詳細(xì)步驟的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    白朗县| 瓦房店市| 汉寿县| 上思县| 浮山县| 阿荣旗| 乐清市| 西充县| 绥阳县| 霍林郭勒市| 兴安盟| 清徐县| 龙川县| 嘉黎县| 余姚市| 施秉县| 呼伦贝尔市| 铁岭市| 甘肃省| 长汀县| 承德市| 读书| 曲阳县| 新郑市| 泾源县| 阆中市| 兴安盟| 永安市| 敦化市| 上林县| 留坝县| 巩留县| 昭通市| 应用必备| 永宁县| 玉环县| 定兴县| 纳雍县| 青冈县| 五常市| 新郑市|