nginx 的 content阶段的root指令与alias指令

root 与alias指令

1
2
3
4
5
6
7
8
Syntax: alias path;
Default: —
Context: location
 
 
Syntax: root path;
Default: root html;
Context: http, server, location, if in location

 功能:将URI映射为文件路径,以静态文件返回

   差别:root 会将完整的URI映射进文件路径中;alias只会将location后面的文件映射进location中

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
    server_name root.com;
    error_log logs/root_error.log info;
    access_log logs/root.log main;
    location /root {
        root html; #表示映射本地html/root
    }
    location /alias {
        alias html; #访问root.com/alias 命中本地/data/web/html
    }
    location ~ /root/(\w+\.txt) {
        root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt
    }
    location ~ /alias/(\w+\.txt) {
        alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt
    }
}

  日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@python vhast]# curl root.com/root
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/alias
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/alias/1.txt
first
 
 
 
 
日志
192.168.183.4 - - [10/Jul/2019:16:25:01 +0800] "GET /root HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root" "-" "/data/web/html" "/data/web/html/root"
192.168.183.4 - - [10/Jul/2019:16:30:16 +0800] "GET /root/1.txt HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt/root/1.txt"
192.168.183.4 - - [10/Jul/2019:16:31:51 +0800] "GET /alias HTTP/1.1" 301 169 "-" "curl/7.29.0" "-" "/alias" "-" "/data/web/html" "/data/web/html"
192.168.183.4 - - [10/Jul/2019:16:32:09 +0800] "GET /alias/1.txt HTTP/1.1" 200 6 "-" "curl/7.29.0" "-" "/alias/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt

  三个变量

request_filename :待访问文件的完整路径
document_root : 由URI和root/alis规则生成的文件夹路径
realpath_root:将document_root中的软连接等换成真实路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@python vhast]# cat root.conf
server {
    server_name root.com;
    error_log logs/root_error.log info;
    access_log logs/root.log main;
    location /root {
        root html; #表示映射本地html/root
    }
    location /alias {
        alias html; #访问root.com/alias 命中本地/data/web/html
    }
    location ~ /root/(\w+\.txt) {
        root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt
    }
    location ~ /alias/(\w+\.txt) {
        alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt
    }
    location /RealPath/ {
        alias html/realpath/;
        return 200 "$request_filename:$document_root:$realpath_root!\n";
    }
}

  

测试
1
2
root@python vhast]# curl root.com/RealPath/
/data/web/html/realpath/:/data/web/html/realpath/:!

  

 
 

 

posted @   烟雨楼台,行云流水  阅读(374)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示