虛擬主機使用的是特殊的軟硬" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > nginx配置虛擬主機的詳細步驟

nginx配置虛擬主機的詳細步驟

時間:2023-07-17 11:39:01 | 來源:網(wǎng)站運營

時間:2023-07-17 11:39:01 來源:網(wǎng)站運營

nginx配置虛擬主機的詳細步驟:虛擬主機提供了在同一臺服務(wù)器、同一組Nginx進程上運行多個網(wǎng)站的功能。本文通過三種方法給大家介紹配置虛擬主機的方法,感興趣的朋友跟隨小編一起看看吧




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



利用虛擬主機,不用為每個要運行的網(wǎng)站提供一臺單獨的Nginx服務(wù)器或單獨運行一組Nginx進程。虛擬主機提供了在同一臺服務(wù)器、同一組Nginx進程上運行多個網(wǎng)站的功能。
配置虛擬主機有三種方法:



方式一:多網(wǎng)卡多IP
兩個物理網(wǎng)卡,兩個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編輯配置文件,基于每個IP創(chuàng)建一個虛擬主機

# 為防止 /etc/nginx/conf.d/default.conf 配置文件影響,對其進行重命名[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)卡對應(yīng)的虛擬主機server { listen 192.168.126.41:80; location / { root /ip_ens32; index index.html; }}# ens34 網(wǎng)卡對應(yīng)的虛擬主機server { listen 192.168.126.42:80; location / { root /ip_ens34; index index.html; }}創(chuàng)建虛擬主機的網(wǎng)頁文件目錄及文件

[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檢查配置文件的語法

[root@nginx ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful重載nginx服務(wù)

[root@nginx ~]# systemctl reload nginx測試

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





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

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

ip addr add IP/MASK dev 網(wǎng)卡名# 刪除ip addr del IP/MASK dev 網(wǎng)卡名其余步驟同上面多網(wǎng)卡多IP的配置

基于端口







多使用于公司內(nèi)部,無法使用域名或沒有域名時

配置

[root@nginx ~]# vim /etc/nginx/conf.d/port.confserver { 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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@nginx ~]# systemctl reload nginx測試

[root@nginx ~]# curl 192.168.126.41:8181[root@nginx ~]# curl 192.168.126.41:8282





基于域名







配置

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

[root@nginx ~]# vim /etc/nginx/conf.d/test1.dxk.com.confserver { listen 80; server_name test1.dxk.com; location / { root /test1; index index.html; }}[root@nginx ~]# vim /etc/nginx/conf.d/test2.dxk.com.confserver { 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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@nginx ~]# systemctl reload nginx測試

# 配置域名解析[root@nginx ~]# echo -e "192.168.126.41 test1.dxk.com/n192.168.126.41 test2.dxk.com" >> /etc/hosts[root@nginx ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.126.41 test1.dxk.com192.168.126.41 test2.dxk.com[root@nginx ~]# curl test1.dxk.comtest1[root@nginx ~]# curl test2.dxk.comtest2











這里有個問題:

如果在配置域名解析時由于寫錯了,那么訪問該錯誤域名(未配置該錯誤域名的虛擬主機)時竟然還會返回網(wǎng)頁內(nèi)容。

[root@nginx ~]# vim /etc/hosts192.168.126.41 test1.dxk.com192.168.126.41 test3.dxk.com # 這里本應(yīng)該是 test2.dxk.com ,但是由于寫錯了,而且對應(yīng)test3.dxk.com域名的虛擬主機并不存在訪問該錯誤域名

[root@nginx ~]# curl test3.dxk.comtest1# 可以看到,還是會返回網(wǎng)頁信息因為在配置域名解析時,雖然域名寫錯了,但是IP是對的,那么此時服務(wù)端默認(rèn)會返回滿足是該IP且端口為80的排在第一個的虛擬主機的網(wǎng)頁信息給客戶端

[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虛擬主機的文章就介紹到這了

關(guān)鍵詞:詳細,步驟,主機,配置,虛擬

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉