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上部署react项目的实例方法

    nginx上部署react项目的实例方法

    今天小编就为大家分享一篇关于nginx上部署react项目的实例方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Nginx+uwsgi+ssl配置https的详细步骤

    Nginx+uwsgi+ssl配置https的详细步骤

    nginx是一个轻量级的web服务器,在处理静态资源和高并发有优势,uwsgi是一个基于python的高效率的协议,处理后端和动态网页有优势,我这里使用的是Ubuntu18.04版本,服务器在阿里云,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • 使用Nginx实现端口转发TCP代理的实现示例

    使用Nginx实现端口转发TCP代理的实现示例

    本文主要介绍了使用Nginx实现端口转发TCP代理的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 使用nginx设置代理服务器

    使用nginx设置代理服务器

    今天小编就为大家分享一篇关于使用nginx设置代理服务器,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Nginx部署React项目时重定向循环问题的解决方案

    Nginx部署React项目时重定向循环问题的解决方案

    Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介绍了相关的解决方法,需要的朋友可以参考下
    2025-05-05
  • 一台nginx服务器多域名配置的方法

    一台nginx服务器多域名配置的方法

    本篇文章主要介绍了一台nginx服务器多域名配置的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • 常见的Nginx配置误区

    常见的Nginx配置误区

    对很多人而言,配置Nginx+PHP无外乎就是搜索一篇教程,然后拷贝粘贴。听上去似乎也没什么问题,可惜实际上网络上很多资料本身年久失修,漏洞百出,如果大家不求甚解,一味的拷贝粘贴,早晚有一天会为此付出代价
    2014-03-03
  • Nginx服务器中处理AJAX跨域请求的配置方法讲解

    Nginx服务器中处理AJAX跨域请求的配置方法讲解

    这篇文章主要介绍了Nginx服务器中处理AJAX跨域请求的配置方法讲解,包括Nginx作Apache的反向代理时的配置方法,需要的朋友可以参考下
    2016-01-01
  • linux下Nginx+Tomcat负载均衡配置方法

    linux下Nginx+Tomcat负载均衡配置方法

    这篇文章主要介绍了linux下Nginx+Tomcat负载均衡配置方法,需要的朋友可以参考下
    2016-09-09
  • nginx启动命令和默认配置文件的使用

    nginx启动命令和默认配置文件的使用

    这篇文章主要介绍了nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06

最新评论