root 与alias指令

Syntax: alias path;
Default: —
Context: location Syntax: root path;
Default: root html;
Context: http, server, location, if in location

 功能:将URI映射为文件路径,以静态文件返回

差别:root 会将完整的URI映射进文件路径中;alias只会将location后面的文件映射进location中

示例

server {
server_name root.com;
error_log logs/root_error.log info;
access_log logs/root.log main;
location /root {
root html; #表示映射本地html/root
}
location /alias {
alias html; #访问root.com/alias 命中本地/data/web/html
}
location ~ /root/(\w+\.txt) {
root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt
}
location ~ /alias/(\w+\.txt) {
alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt
}
}

  日志

[root@python vhast]# curl root.com/root
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/alias
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl root.com/alias/1.txt
first 日志
192.168.183.4 - - [10/Jul/2019:16:25:01 +0800] "GET /root HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root" "-" "/data/web/html" "/data/web/html/root"
192.168.183.4 - - [10/Jul/2019:16:30:16 +0800] "GET /root/1.txt HTTP/1.1" 404 153 "-" "curl/7.29.0" "-" "/root/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt/root/1.txt"
192.168.183.4 - - [10/Jul/2019:16:31:51 +0800] "GET /alias HTTP/1.1" 301 169 "-" "curl/7.29.0" "-" "/alias" "-" "/data/web/html" "/data/web/html"
192.168.183.4 - - [10/Jul/2019:16:32:09 +0800] "GET /alias/1.txt HTTP/1.1" 200 6 "-" "curl/7.29.0" "-" "/alias/1.txt" "-" "/data/web/html/first/1.txt" "/data/web/html/first/1.txt

  三个变量

request_filename :待访问文件的完整路径
document_root : 由URI和root/alis规则生成的文件夹路径
realpath_root:将document_root中的软连接等换成真实路径
[root@python vhast]# cat root.conf
server {
server_name root.com;
error_log logs/root_error.log info;
access_log logs/root.log main;
location /root {
root html; #表示映射本地html/root
}
location /alias {
alias html; #访问root.com/alias 命中本地/data/web/html
}
location ~ /root/(\w+\.txt) {
root html/first/$1;#访问这个root.com/root/1.txt,命中本地的/data/web/html/first/1.txt/root/1.txt
}
location ~ /alias/(\w+\.txt) {
alias html/first/$1; # 访问这个curl root.com/alias/1.txt,命中本地的/data/web/html/first/1.txt
}
location /RealPath/ {
alias html/realpath/;
return 200 "$request_filename:$document_root:$realpath_root!\n";
}
}

  

测试
root@python vhast]# curl root.com/RealPath/
/data/web/html/realpath/:/data/web/html/realpath/:!

  

 
 

nginx 的 content阶段的root指令与alias指令的更多相关文章

  1. nginx location中root指令和alias指令的区别

    nginx location中root指令和alias指令 功能:将url映射为文件路径,以返回静态文件内容 差别:root会将完整的url映射进文件路径中 alias只会将location后的url ...

  2. Nginx CONTENT阶段 static模块

    L63-65 alias指令  syntax: alias path;# 静态文件路径  alias不会将请求路径后的路径添加到 path中 context : location; root指令 sy ...

  3. (转)Nginx静态服务配置---详解root和alias指令

    Nginx静态服务配置---详解root和alias指令 原文:https://www.jianshu.com/p/4be0d5882ec5 静态文件 Nginx以其高性能著称,常用与做前端反向代理服 ...

  4. Nginx 核心配置-根目录root指令与别名alias指令实战案例

    Nginx 核心配置-根目录root指令与别名alias指令实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.试验环境说明 1>.虚拟机环境说明 [root@nod ...

  5. Nginx静态服务配置---详解root和alias指令

    Nginx静态服务配置---详解root和alias指令 静态文件 Nginx以其高性能著称,常用与做前端反向代理服务器.同时nginx也是一个高性能的静态文件服务器.通常都会把应用的静态文件使用ng ...

  6. 【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...

  7. Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...

  8. openresty开发系列35--openresty执行流程之5内容content阶段

    openresty开发系列35--openresty执行流程之5内容content阶段 content 阶段 ---init阶段---重写赋值---重写rewrite---access content ...

  9. 你真的了解nginx重定向URI?-rewrite和alias指令

    未经允许不得转载!最近发现有博主转载我的文章,并没有跟我打招呼,也没有注明出处!!!! 熟悉Nginx的同学都知道Nginx可以用来做负载均衡和反向代理,非常好用.做前后端分离也是非常的方便. 今天我 ...

随机推荐

  1. Go非缓冲/缓冲/双向/单向通道

    1. 非缓冲和缓冲 package main import ( "fmt" "strconv" ) func main() { /* 非缓冲通道:make(ch ...

  2. MySQL命令行脚本

    1. 命令行连接 打开终端,运行命令 mysql -uroot -p 回车后输入密码,当前设置的密码为mysql 退出登录 quit 和 exit 或 ctrl+d 查看版本:select versi ...

  3. winform 多个datagridview 之间同步滚动

    1.添加Scroll事件 2.注意需判断数据长度,避免溢出 private void dgYY_Scroll(object sender, ScrollEventArgs e) { ) { dgFee ...

  4. 统计字符在字符串中第n次出现的位置

    输入一个字符串s,一个数字n和一个字符c,统计这个字符c在字符串s中第n次出现的位置 输入格式: 输入3行.第1行是字符串s,第2行是数字n,第3行是被查找的字符c. 输出格式: 第n个字符在字符串中 ...

  5. DIV 设置垂直居中

    要说面试官经常问的问题中“如何将一个块元素水平垂直居中”就算一个. 之前的面试中也有中招,现在总结一下. 1.CSS垂直水平居中 要让DIV水平和垂直居中,必需知道该DIV得宽度和高度,然后设置位置为 ...

  6. RS422接口与RS485接口

    RS422具体接线参考网站 RS485接口 RS485设备为半双工设备,RS485收发器信号相关引脚包括控制引脚.485A.485B,其中控制引脚的高低电平决定当前处于接收模式还是发送模式. RS48 ...

  7. ztree-可拖拽可编辑的树

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - addNodes / editName / rem ...

  8. 什么是DO / DTO / BO / VO /AO ?

    转载:https://blog.csdn.net/ouzhuangzhuang/article/details/86644476 POJO 是 DO / DTO / BO / VO 的统称. DO(D ...

  9. 使用Log4net记录日志(非常重要)

    使用Log4net记录日志   首先说说为什么要进行日志记录.在一个完整的程序系统里面,日志系统是一个非常重要的功能组成部分.它可以记录下系统所产生的所有行为,并按照某种规范表达出来.我们可以使用日志 ...

  10. Linux修改本机/etc/hosts的hostName后经常不生效

    1.Linux修改本机别名/etc/hosts的hostName后经常不生效解决 Linux修改本机别名/etc/hosts的hostName后经常不生效, 比如我们/etc/hosts的内容如下: ...