欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx中root和alias指令的使用

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

1.基本信息

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

格式:

alias path
root path

2.區(qū)別

  • root會映射完整url,會將location匹配的部分,追加到path后面,即,root指定web的家目錄,在定義location的時候,文件的絕對路徑等于 root+location
  • alias:定義路徑別名,會把訪問的路徑重新定義到其指定的路徑,文檔映射的另一種機制
  • alias會出現(xiàn)在location上下文中,root可以出現(xiàn)在http,server,location,if in location
  • alias無默認值,root默認值為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指令會將 location 中的 /root 追加到 /data/nginx/html 路徑后面,所以路徑是:/data/nginx/html/root
    }

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

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

    location ~ /alias/(\w+\.txt){
        alias /data/nginx/html/first/$1;  # alias指令會使用 /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]#

# 配置驗證
[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

測試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]#


# 與第一個匹配 location /root
# 因為是root指令,所以/data/nginx/html后面又加上了location中的root.因為后面有反斜杠,所以加上了index.html
# 所以實際訪問的是 /data/nginx/html/root/index.html,而這個路徑是不存在的

測試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) 匹配
# 因為是root指令,會在/data/nginx/html/first/1.txt,后面加上匹配到的root/1.txt
# 實際訪問的地址
/data/nginx/html/first/1.txt/root/1.txt,而這個路徑也是不存在的

測試3

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

# 匹配到了 location /alias 這個匹配項
# alias 指令會使用 /data/nginx/html 替換掉 /alias,所以 訪問了 /data/nginx/html/index.html 得到了默認的首頁

測試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)這個匹配項
# alias 指令會使用 /data/nginx/html/first/$1 替換掉 /alias/1.txt,所以訪問到了/data/nginx/html/first/1.txt

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • keepalived+nginx實現(xiàn)網站高可用性

    keepalived+nginx實現(xiàn)網站高可用性

    本文主要介紹了keepalived+nginx實現(xiàn)網站高可用性,包含配置Keepalived實現(xiàn)雙主熱備集群架構,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-02-02
  • Nginx解決history模式下頁面刷新404問題示例

    Nginx解決history模式下頁面刷新404問題示例

    這篇文章主要為大家介紹了Nginx解決history模式下頁面刷新404問題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • 使用Nginx服務器如何實現(xiàn)動靜分離和反向代理

    使用Nginx服務器如何實現(xiàn)動靜分離和反向代理

    這篇文章主要介紹了使用Nginx服務器如何實現(xiàn)動靜分離和反向代理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • nginx修改server信息的方法詳解

    nginx修改server信息的方法詳解

    請求響應信息會暴露nginx版本信息,若攻擊者獲知服務器組件等信息及版本信息,會使下一步的攻擊和漏洞測試提供便利,所以本文給大家介紹了nginx修改server信息的方法,需要的朋友可以參考下
    2025-02-02
  • 詳解NGINX如何統(tǒng)計網站的PV、UV、獨立IP

    詳解NGINX如何統(tǒng)計網站的PV、UV、獨立IP

    做網站的都知道,平常經常要查詢下網站PV、UV等網站的訪問數(shù)據,這篇文章主要介紹了詳解NGINX如何統(tǒng)計網站的PV、UV、獨立IP ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Nginx配置80端口訪問8080及項目名地址方法解析

    Nginx配置80端口訪問8080及項目名地址方法解析

    這篇文章主要介紹了Nginx配置80端口訪問8080及項目名地址方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • nginx配置多個前端項目實現(xiàn)步驟

    nginx配置多個前端項目實現(xiàn)步驟

    本文主要介紹了nginx配置多個前端項目實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • nginx如何設置多個靜態(tài)訪問的文件夾

    nginx如何設置多個靜態(tài)訪問的文件夾

    這篇文章主要介紹了nginx如何設置多個靜態(tài)訪問的文件夾問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • CentOS 中Nginx的安裝方法

    CentOS 中Nginx的安裝方法

    最近研究LNMP,首先要在linux下配置nginx服務器,廢話少說,下面給大家分享下在CentOS 中Nginx的安裝方法
    2017-08-08
  • nginx反向代理如何替換URL

    nginx反向代理如何替換URL

    這篇文章主要介紹了nginx反向代理如何替換URL問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論