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中root和alias指令的解释相关推荐

  1. nginx中root和alias;proxy_pass

    location中的root和alias的区别 root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上. root的处理结果 ...

  2. Nginx中root与alias区别

    Nginx中配置文件路径有两种方式,一种是root一种是alias,那么两种有什么区别呢,下面请跟我一起正确的使用root和alias吧 首先还是先说下他俩的区别,主要是对URI部分处理的不同,如下: ...

  3. nginx的root和alias指令的区别

    nginx配置下有两个指定目录的执行,root和alias location /img/ { alias /var/www/image/; } #若按照上述配置的话,则访问/img/目录里面的文件时, ...

  4. Nginx的root和alias指令

    设置请求资源的目录root / alias root:设置请求的根目录 语法 root path; 默认值 root html; 位置 http.server.location path为Nginx服 ...

  5. nginx中root和alias的区别

    今天使用nginx搭建了一个网站,访问后出现404错误Not found. 上网查了一下原因,是由于nginx的配置不对.因为我是有两个web目录,这两个目录在不同的位置上.而且我不想把两个目录合并在 ...

  6. Nginx的location、root、alias指令用法和区别

    nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域: [root] 语法:root path 默认值:root html 配置段:http.server.location.i ...

  7. nginx 中location中root和alias的区别

    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...

  8. nginx配置中root与alias的区别

    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...

  9. Nginx之location、root、alias指令用法

    在Nginx的server配置经常会遇到root与alias的配置 server {listen 80;server_name com.sxkj;access_log logs/access.log ...

最新文章

  1. 移动互联网改变商业环境:商品的颠覆
  2. linux学习之路(1)
  3. opencv3.1.0 交叉编译 H3516a
  4. 怎么爬before after之间的内容_关于伪元素::before和::after的用法
  5. 调研内容(算法相关--MDP)
  6. 初生牛犊不怕虎 造车新势力的硬核移动互联科技盘点
  7. 在桌面计算机找不到光盘驱动,如何弹出DVD驱动器,没有按钮,我在计算机中找不到DVD驱动器...
  8. win10修改hosts小工具
  9. UML类图和用例图练习
  10. 将搜狗输入法默认的中文输入更改成英文
  11. Oracle 判断正负数函数 sign
  12. 怎么退出用户登录linux,怎样登录和退出Linux系统
  13. 数据库-不允许保存更改,阻止保存要求重新创建表的更改
  14. 十、什么是临界资源及如何访问临界资源
  15. Android进程保活(黑白手段让APP活下去)
  16. rm -rf命令的作用 以及windows 代替命令
  17. word2vec源码分析
  18. 攻防世界逆向高手题之re2-cpp-is-awesome
  19. SVN客户端安装及使用说明
  20. UI设计准则在360云盘的运用

热门文章

  1. linux下Nginx部署前后端项目
  2. linux服务篇-DHCP服务
  3. MarkDown转译字符
  4. 简单Excel微型数据库
  5. echarts 饼状图
  6. 用Java输出兔子图形,如何画出可爱的兔子?那你看看这个!
  7. BAT 机器学习 1000 题 201-300(转)
  8. python编程超市购物系统_python 自动购物系统 超简单源码(入门级)
  9. 【软件测试真题合集】BATJ都在问哪些自动化测试面试题?看了你就知道了
  10. Java开发框架!java枚举定义lists参数