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

nginx中root和alias指令的使用

 更新時(shí)間:2024年08月17日 09:40:16   作者:臨江仙我亦是行人  
這篇文章主要介紹了nginx中root和alias指令的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1.基本信息

功能均為將url映射為文件路徑,返回靜態(tài)文件內(nèi)容

格式:

alias path
root path

2.區(qū)別

  • root會(huì)映射完整url,會(huì)將location匹配的部分,追加到path后面,即,root指定web的家目錄,在定義location的時(shí)候,文件的絕對(duì)路徑等于 root+location
  • alias:定義路徑別名,會(huì)把訪問(wèn)的路徑重新定義到其指定的路徑,文檔映射的另一種機(jī)制
  • alias會(huì)出現(xiàn)在location上下文中,root可以出現(xiàn)在http,server,location,if in location
  • alias無(wú)默認(rèn)值,root默認(rèn)值為root html

3.示例

[root@centos8 conf.d]#cat /apps/nginx/conf.d/root_alias.conf
server {
    server_name path.test.com;
    root /data/nginx;
    error_log logs/myerror.log info;
    location /root {
        root /data/nginx/html;   # root指令會(huì)將 location 中的 /root 追加到 /data/nginx/html 路徑后面,所以路徑是:/data/nginx/html/root
    }

    location /alias {
        alias /data/nginx/html;   # alias指令會(huì)使用 /data/nginx/html 替換掉 location 中定義的 /alias 路徑
    }

    location ~ /root/(\w+\.txt) {
        root /data/nginx/html/first/$1;  # 實(shí)際訪問(wèn)的是 /data/nginx/html/first/1.txt/root/1.txt
    }

    location ~ /alias/(\w+\.txt){
        alias /data/nginx/html/first/$1;  # alias指令會(huì)使用 /data/nginx/html/first/$1 替換掉 /alias/(\w+\.txt)
    }

    location /RealPath/ {
        alias /data/nginx/html/realpath/;
        return 200 '$request_filename:$document_root:$realpath_root\n';
    }
}
[root@centos8 conf.d]#

# 配置驗(yàn)證
[root@centos8 conf.d]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@centos8 conf.d]#
[root@centos8 conf.d]#nginx -s reload
[root@centos8 conf.d]#mkdir /data/nginx/html/first -pv
mkdir: created directory '/data/nginx/html'
mkdir: created directory '/data/nginx/html/first'
[root@centos8 conf.d]#echo "This index.html test page" > /data/nginx/html/index.html
[root@centos8 conf.d]#echo "This is a 1.txt" > /data/nginx/html/first/1.txt
[root@centos8 conf.d]#cat /data/nginx/html/first/1.txt
This is a 1.txt
[root@centos8 conf.d]#cat /data/nginx/html/index.html
This index.html test page

測(cè)試1

[root@centos8 conf.d]#curl path.test.com/root/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@centos8 conf.d]#


# 與第一個(gè)匹配 location /root
# 因?yàn)槭莚oot指令,所以/data/nginx/html后面又加上了location中的root.因?yàn)楹竺嬗蟹葱备?所以加上了index.html
# 所以實(shí)際訪問(wèn)的是 /data/nginx/html/root/index.html,而這個(gè)路徑是不存在的

測(cè)試2

[root@centos8 conf.d]#curl path.test.com/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@centos8 conf.d]#

# 與location ~ /root/(\w+\.txt) 匹配
# 因?yàn)槭莚oot指令,會(huì)在/data/nginx/html/first/1.txt,后面加上匹配到的root/1.txt
# 實(shí)際訪問(wèn)的地址
/data/nginx/html/first/1.txt/root/1.txt,而這個(gè)路徑也是不存在的

測(cè)試3

[root@centos8 conf.d]#curl path.test.com/alias/
This index.html test page
[root@centos8 conf.d]#

# 匹配到了 location /alias 這個(gè)匹配項(xiàng)
# alias 指令會(huì)使用 /data/nginx/html 替換掉 /alias,所以 訪問(wèn)了 /data/nginx/html/index.html 得到了默認(rèn)的首頁(yè)

測(cè)試4

[root@centos8 conf.d]#curl path.test.com/alias/1.txt
This is a 1.txt
[root@centos8 conf.d]#

# 匹配到了 location ~ /alias/(\w+\.txt)這個(gè)匹配項(xiàng)
# alias 指令會(huì)使用 /data/nginx/html/first/$1 替換掉 /alias/1.txt,所以訪問(wèn)到了/data/nginx/html/first/1.txt

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx泛解析到子目錄后自動(dòng)判斷有無(wú)public目錄詳解

    Nginx泛解析到子目錄后自動(dòng)判斷有無(wú)public目錄詳解

    這篇文章主要給大家介紹了關(guān)于Nginx泛解析到子目錄后自動(dòng)判斷有無(wú)public目錄的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 詳解Nginx配置WebSocket(支持wss與ws連接)

    詳解Nginx配置WebSocket(支持wss與ws連接)

    本文主要介紹了詳解Nginx配置WebSocket(支持wss與ws連接),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-09-09
  • Nginx反向代理后端服務(wù)的操作步驟

    Nginx反向代理后端服務(wù)的操作步驟

    反向代理是一種代理服務(wù)器,位于客戶(hù)端與服務(wù)器之間,后端服務(wù)器處理請(qǐng)求后將響應(yīng)發(fā)送回反向代理服務(wù)器,反向代理服務(wù)器再將響應(yīng)返回給客戶(hù)端,本文將詳細(xì)介紹Nginx如何反向代理后端服務(wù),涵蓋其基本概念、配置方法、負(fù)載均衡、SSL/TLS支持等多個(gè)方面,需要的朋友可以參考下
    2024-06-06
  • Nginx配置支持IPV6地址的方法示例

    Nginx配置支持IPV6地址的方法示例

    本文主要介紹了如何搭建并測(cè)試Nginx以支持IPV6地址的過(guò)程,包括下載安裝包、編譯安裝、配置和啟動(dòng)Nginx等步驟,同時(shí),文章還解決了在測(cè)試IPV6地址時(shí)遇到的兩個(gè)問(wèn)題:curl解析錯(cuò)誤和阿里云、騰訊云IPV6地址配置問(wèn)題
    2024-11-11
  • Nginx配置中使用Lua腳本的實(shí)現(xiàn)步驟

    Nginx配置中使用Lua腳本的實(shí)現(xiàn)步驟

    在阿里云API網(wǎng)關(guān)和字節(jié)跳動(dòng)邊緣計(jì)算平臺(tái)中,Nginx+Lua的組合已成為處理復(fù)雜業(yè)務(wù)邏輯的標(biāo)準(zhǔn)解決方案,下面我們就來(lái)介紹一下Nginx配置中使用Lua腳本的實(shí)現(xiàn)步驟,感興趣都可以了解一下
    2025-09-09
  • nginx下如何設(shè)置上傳文件大小

    nginx下如何設(shè)置上傳文件大小

    這篇文章主要介紹了nginx下如何設(shè)置上傳文件大小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Nginx配置Vue項(xiàng)目Hash/History模式路由跳轉(zhuǎn)錯(cuò)誤的解決方案

    Nginx配置Vue項(xiàng)目Hash/History模式路由跳轉(zhuǎn)錯(cuò)誤的解決方案

    這篇文章主要為大家詳細(xì)介紹了Nginx配置Vue項(xiàng)目Hash/History模式路由跳轉(zhuǎn)錯(cuò)誤的相關(guān)解決方案,文中的示例代碼講解詳細(xì),需要的小伙伴可以了解下
    2025-09-09
  • Nginx禁止國(guó)外IP訪問(wèn)我的網(wǎng)站的實(shí)現(xiàn)

    Nginx禁止國(guó)外IP訪問(wèn)我的網(wǎng)站的實(shí)現(xiàn)

    本文主要介紹了Nginx禁止國(guó)外IP訪問(wèn)我的網(wǎng)站的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法

    Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法

    本文主要介紹了Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法,報(bào)錯(cuò)413是因?yàn)镹ginx對(duì)上傳大小做了限制,所以我們需要配置文件,下面就來(lái)解決這個(gè)問(wèn)題,感興趣的可以了解一下
    2024-03-03
  • 詳解Nginx幾種常見(jiàn)實(shí)現(xiàn)301重定向方法上的區(qū)別

    詳解Nginx幾種常見(jiàn)實(shí)現(xiàn)301重定向方法上的區(qū)別

    本篇文章主要介紹了詳解Nginx幾種常見(jiàn)實(shí)現(xiàn)301重定向方法上的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論

富锦市| 台东市| 竹山县| 施甸县| 南投市| 江永县| 西藏| 西峡县| 睢宁县| 嵊州市| 无极县| 高青县| 江源县| 鄂尔多斯市| 镇远县| 南川市| 隆子县| 化德县| 新巴尔虎左旗| 元朗区| 松江区| 洛南县| 阳朔县| 田东县| 青岛市| 简阳市| 利川市| 闵行区| 西宁市| 绥化市| 红河县| 旌德县| 陕西省| 沙田区| 普安县| 阳城县| 桦南县| 玉环县| 岳普湖县| 大渡口区| 凤山县|