ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx中root和alias指令的解释

2021-06-14 16:58:56  阅读:203  来源: 互联网

标签:alias data nginx html conf root


1 基本信息

功能均为将url映射为文件路径,返回静态文件内容

格式

alias path
root path

2 区别

  • root会映射完整url,会将location匹配的部分,追加到path后面,即,root指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location

  • alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制

  • alias会出现在location上下文中,root可以出现在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

标签:alias,data,nginx,html,conf,root
来源: https://blog.csdn.net/weixin_56752399/article/details/117907732

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有