nginx中root和alias指令的解释

2023-11-09

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nginx中root和alias指令的解释 的相关文章

随机推荐

  • ssm打印sql如何开启_ssm环境下配置log4j打印mybatis的sql语句

    首先附上官网的说明文档 mybatis Logging 环境spring4 3 0 springmvc4 3 0 mybatis3 4 0 按官方文档的说明 1 SLF4J 2 Apache Commons Logging 3 Log4j
  • 使用invoke方法解决跨线程访问的问题

    C 中禁止跨线程直接访问控件 InvokeRequired是为了解决这个问题而产生的 当一个控件的InvokeRequired属性值为真时 说明有一个创建它以外的线程想访问它 获取一个值 该值指示调用方在对控件进行方法调用时是否必须调用 I
  • js+bootstrap+jquery+vue实现房贷计算器

    代码链接 loan 使用vue js html css实现房贷的计算 版权声明 本文为CSDN博主 小样还想跑 的原创文章 遵循CC 4 0 BY SA版权协议 转载请附上原文出处链接及本声明
  • 23 种设计模式详解(全23种)

    设计模式的分类 总体来说设计模式分为三大类 创建型模式 共五种 工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式 结构型模式 共七种 适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式 行为型模式 共十一种
  • 人手一份核武器:Android手机装Kali Linux

    首先这是安卓手机的专属工具 因为Android基于Linux 所以就有了得天独厚的优势 1 先下载好Linux Deploy 前提是本手机已root 2 按下图配置 不过有地方需要说明 Distribute Suite已经改为sana 但无
  • Windows Server 2012 R2 设置 smtp 服务器

    Windows Server 2012 2012 R2 安装和配置 SMTP 服务器 安装 SMTP 服务器 以下是安装 SMTP 服务器功能的步骤 打开 服务器管理器 单击键盘上的 Windows 按钮 输入 服务器管理器 在 结果 窗口
  • FW-1设备配置命令

    DCFW 1800 config hostname FW 1 FW 1 config ip vrouter trust vr FW 1 config vrouter ip route 0 0 0 0 0 202 11 33 26 FW 1
  • cmd创建用户并初始化新用户桌面

    author skate time 2013 12 20 功能 在win2003上创建用户 并初始化新用户的桌面 echo InternetShortcut gt gt MysqlTool url echo URL C Program Fi
  • Qt之pro配置多个子工程/子模块

    简述 进行Qt项目开发的时候 尤其是大型项目 经常涉及多工程 多模块问题 其主要思想还是模块化 目的是为了降低程序复杂度 使程序设计 调试和维护等操作简单化 简述 配置 效果 多工程 多模块 更多参考 配置 效果 多工程 如果需要管理多工程
  • JavaMap集合&Stream流

    1 Map集合 1 1Map集合概述和特点 Map集合概述 interface Map
  • Python-Thread(通俗易懂)

    此类表示在单独的控制线程中运行的活动 有两种方法可以指定该活动 一是将可调用对象传递给构造函数 二是通过覆盖子类中的run 方法 如果你对线程不太理解 我们可以打个比方 把线程数看作车辆数 我们来完成一个简单的客运运输工作 以下为了方便理解
  • 第8届Python编程挑战赛初赛真题剖析-2022年全国青少年信息素养大赛

    导读 超平老师计划推出 全国青少年信息素养大赛Python编程真题解析 50讲 这是超平老师解读Python编程挑战赛系列的第1讲 全国青少年信息素养大赛 原全国青少年电子信息智能创新大赛 是 世界机器人大会青少年机器人设计与信息素养大赛
  • VC++ MapWinGis篇(二)

    添加高德图层 ArcGisProvider h pragma once include BaseProvider h class ArcGisBaseProvider public BaseProvider public ArcGisBas
  • Java RMI 远程代码执行漏洞

    0x01 漏洞描述 Java RMI 远程代码执行漏洞 Java RMI服务是远程方法调用 是J2SE的一部分 能够让程序员开发出基于JAVA的分布式应用 一个RMI对象是一个远程Java对象 可以从另一个Java虚拟机上 甚至跨过网络 调
  • 这篇文章带你了解sql语句是怎么执行的

    一条sql语句是怎么执行的 一 mysql架构分析 二 语句分析 2 1 查询语句 2 2 更新语句 三 总结 mysql有各种版本的架构图 但基本上都可以分为Server层和存储引擎层 一 mysql架构分析 下面是mysql的一个简要架
  • web压测工具http_load原理分析

    01 前言 http load是一款测试web服务器性能的开源工具 从下面的网址可以下载到最新版本的http load http www acme com software http load 这个软件一直在保持着更新 不像webbench
  • el-tree组件展示节点过多时造成页面卡顿、奔溃的解决办法

    解决el tree组件展示节点过多时造成页面卡顿 奔溃 前几天测试提了个BUG 文件列表展示5w个文件页面会卡顿甚至奔溃 项目用的是vue element ui框架 我是使用el tree进行渲染文件列表的 参考网上使用virtual sc
  • Log4j2注入漏洞万字剖析-汇总收藏版(攻击步骤、漏洞原理、2.15.0-RC1绕过原理以及2.15.0、2.16.0修复原理)

    系列文章 2 15 0之前版漏洞相关文章 Log4j2注入漏洞 CVE 2021 44228 万字深度剖析 一 开篇与基础知识 Log4j2注入漏洞 CVE 2021 44228 万字深度剖析 二 漏洞原理 Log4j2注入漏洞 CVE 2
  • ILRuntime(二)整合Hotfix到Unity中,脚本生成dll文件

    如果开发的时候按之前的一个Hotfix工程 一个Unity工程 开发会很麻烦 因此我们可以把Hotfix部分的代码放入到Unity当中 并增加一个标记 到时候把这些代码整合成一个dll文件即可 具体思路 ILRuntime的原理就是热更代码
  • nginx中root和alias指令的解释

    1 基本信息 功能均为将url映射为文件路径 返回静态文件内容 格式 alias path root path 2 区别 root会映射完整url 会将location匹配的部分 追加到path后面 即 root指定web的家目录 在定义l