最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年07月11日 11:40:23   作者:途徑日暮不賞丶  
虛擬主機提供了在同一臺服務器、同一組Nginx進程上運行多個網(wǎng)站的功能。本文通過三種方法給大家介紹配置虛擬主機的方法,感興趣的朋友跟隨小編一起看看吧

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

在這里插入圖片描述

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

配置虛擬主機有三種方法:

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

在這里插入圖片描述

方式一:多網(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)卡對應的虛擬主機
server {
  listen 192.168.126.41:80;

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

# ens34 網(wǎ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 -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重載nginx服務

[root@nginx ~]# systemctl reload nginx

測試

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

在這里插入圖片描述在這里插入圖片描述

方式二:單網(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.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

測試

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

在這里插入圖片描述在這里插入圖片描述

基于域名

在這里插入圖片描述

配置

一般一個域名對應一個配置文件,便于管理

[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

測試

# 配置域名解析
[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

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

這里有個問題:

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

[root@nginx ~]# vim /etc/hosts
192.168.126.41 test1.dxk.com
192.168.126.41 test3.dxk.com   # 這里本應該是 test2.dxk.com ,但是由于寫錯了,而且對應test3.dxk.com域名的虛擬主機并不存在

訪問該錯誤域名

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

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

因為在配置域名解析時,雖然域名寫錯了,但是IP是對的,那么此時服務端默認會返回滿足是該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

這是需要注意的

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

相關文章

  • 解讀nginx反向代理location和proxy_pass的映射關系

    解讀nginx反向代理location和proxy_pass的映射關系

    這篇文章主要介紹了解讀nginx反向代理location和proxy_pass的映射關系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • nginx禁用特定ip的方法詳解

    nginx禁用特定ip的方法詳解

    Nginx是一款輕量級的Web服務器、反向代理服務器及電子郵件代理服務器,其特點是占有內(nèi)存少,并發(fā)能力強,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務器中表現(xiàn)較好,本文主要介紹利用nginx來禁用特定ip的方法,需要的朋友可以參考下
    2023-09-09
  • CentOS系統(tǒng)rpm安裝Nginx和配置

    CentOS系統(tǒng)rpm安裝Nginx和配置

    大家好,本篇文章主要講的是CentOS系統(tǒng)rpm安裝Nginx和配置,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Nginx啟動時80端口被占用的問題解決

    Nginx啟動時80端口被占用的問題解決

    本文主要介紹了在啟動Nginx時遇到80端口被占用的問題及解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12
  • 詳解Nginx虛擬主機配置中server_name的具體寫法

    詳解Nginx虛擬主機配置中server_name的具體寫法

    這篇文章主要介紹了Nginx虛擬主機配置中server_name的具體寫法,server_name服務器名是虛擬主機中必須配置的重要參數(shù),需要的朋友可以參考下
    2016-03-03
  • 加速nginx性能: 開啟gzip和緩存

    加速nginx性能: 開啟gzip和緩存

    nginx 是一個高性能的 Web 服務器,之前也寫過一些關于 nginx 的文章。為了提高博客的響應速度,可以從設置 nginx 的 gzip 和緩存這2方面入手。為字體開啟 gzip 和緩存能大大減少帶寬的消耗
    2017-03-03
  • Nginx配置文件nginx.conf的基本配置實例詳解

    Nginx配置文件nginx.conf的基本配置實例詳解

    Nginx(engine x)是一個輕量級的高性能的HTTP和反向代理web服務器及電子郵件(IMAP/POP3)代理服務器,下面這篇文章主要給大家介紹了關于Nginx配置文件nginx.conf基本配置的相關資料,需要的朋友可以參考下
    2022-09-09
  • nginx代理postgresql的實現(xiàn)示例

    nginx代理postgresql的實現(xiàn)示例

    本文主要介紹了nginx代理postgresql的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-10-10
  • Nginx設置HTTPS的方法步驟

    Nginx設置HTTPS的方法步驟

    本文主要介紹了NGINX設置HTTPS的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Nginx 配置 ModSecurity 網(wǎng)絡應用防火墻實現(xiàn)

    Nginx 配置 ModSecurity 網(wǎng)絡應用防火墻實現(xiàn)

    這篇文章主要介紹了Nginx 配置 ModSecurity 網(wǎng)絡應用防火墻實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-12-12

最新評論

凤台县| 斗六市| 琼海市| 武胜县| 灌南县| 腾冲县| 女性| 德安县| 永州市| 靖边县| 乾安县| 霸州市| 乳山市| 涟水县| 桓仁| 景东| 宁南县| 古蔺县| 汶上县| 喀喇沁旗| 苏州市| 怀安县| 柯坪县| 崇左市| 杨浦区| 斗六市| 寻乌县| 富蕴县| 黎川县| 南投市| 洮南市| 上林县| 顺平县| 兴国县| 武宣县| 平罗县| 土默特右旗| 渑池县| 财经| 桦川县| 台州市|