nginx中root和alias指令的使用

 更新时间:2024年08月17日 09:40:16   作者:临江仙我亦是行人  
这篇文章主要介绍了nginx中root和alias指令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Nginx泛解析到子目录后自动判断有无public目录详解

    Nginx泛解析到子目录后自动判断有无public目录详解

    这篇文章主要给大家介绍了关于Nginx泛解析到子目录后自动判断有无public目录的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来跟着小编一起学习学习吧。
    2017-08-08
  • 详解Nginx配置WebSocket(支持wss与ws连接)

    详解Nginx配置WebSocket(支持wss与ws连接)

    本文主要介绍了详解Nginx配置WebSocket(支持wss与ws连接),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-09-09
  • Nginx反向代理后端服务的操作步骤

    Nginx反向代理后端服务的操作步骤

    反向代理是一种代理服务器,位于客户端与服务器之间,后端服务器处理请求后将响应发送回反向代理服务器,反向代理服务器再将响应返回给客户端,本文将详细介绍Nginx如何反向代理后端服务,涵盖其基本概念、配置方法、负载均衡、SSL/TLS支持等多个方面,需要的朋友可以参考下
    2024-06-06
  • Nginx配置支持IPV6地址的方法示例

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

    本文主要介绍了如何搭建并测试Nginx以支持IPV6地址的过程,包括下载安装包、编译安装、配置和启动Nginx等步骤,同时,文章还解决了在测试IPV6地址时遇到的两个问题:curl解析错误和阿里云、腾讯云IPV6地址配置问题
    2024-11-11
  • Nginx配置中使用Lua脚本的实现步骤

    Nginx配置中使用Lua脚本的实现步骤

    在阿里云API网关和字节跳动边缘计算平台中,Nginx+Lua的组合已成为处理复杂业务逻辑的标准解决方案,下面我们就来介绍一下Nginx配置中使用Lua脚本的实现步骤,感兴趣都可以了解一下
    2025-09-09
  • nginx下如何设置上传文件大小

    nginx下如何设置上传文件大小

    这篇文章主要介绍了nginx下如何设置上传文件大小问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Nginx配置Vue项目Hash/History模式路由跳转错误的解决方案

    Nginx配置Vue项目Hash/History模式路由跳转错误的解决方案

    这篇文章主要为大家详细介绍了Nginx配置Vue项目Hash/History模式路由跳转错误的相关解决方案,文中的示例代码讲解详细,需要的小伙伴可以了解下
    2025-09-09
  • Nginx禁止国外IP访问我的网站的实现

    Nginx禁止国外IP访问我的网站的实现

    本文主要介绍了Nginx禁止国外IP访问我的网站的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Nginx部署项目上传文件报错413的解决方法

    Nginx部署项目上传文件报错413的解决方法

    本文主要介绍了Nginx部署项目上传文件报错413的解决方法,报错413是因为Nginx对上传大小做了限制,所以我们需要配置文件,下面就来解决这个问题,感兴趣的可以了解一下
    2024-03-03
  • 详解Nginx几种常见实现301重定向方法上的区别

    详解Nginx几种常见实现301重定向方法上的区别

    本篇文章主要介绍了详解Nginx几种常见实现301重定向方法上的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论