重识Nginx - 06 搭建静态资源Web服务器(alias VS root)
文章目录
官网说明
点击 Module ngx_http_core_module
root vs alias
root与alias主要区别在于nginx如何解释location后面的uri, 分别以不同的方式将请求映射到服务器文件上。
- alias是一个目录别名的定义(仅能用于location上下文)
- root则是最上层目录的定义。
- root的处理结果是:root路径+location路径 ; alias的处理结果是:使用alias路径替换location路径
- alias后面必须要用“/”结束,否则会找不到文件, root则可有可无~~
- 使用alias时,目录名后面一定要加"/"
- alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
- alias只能位于location块中, root可以不放在location中
location ^~ /abc/ {
alias /www/artisan/html/new_abc/;
}
如果一个请求的URI是/abc/a.html
时, 将会返回服务器的/www/artisan/html/new_abc/a.html
注意这里是new_abc, alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
location ^~ /abc/ {
root /www/artisan/html/;
}
如果一个请求的URI是/abc/a.html
时,web服务器将会返回服务器上的/www/artisan/html/abc/a.html
的文件。 root会把location配置的路径进行追加----> root路径+location路径
alias (用alias场景居多)
语法
Syntax: alias path;
Default: —
Context: location
Demo
Defines a replacement for the specified location.
For example, with the following configuration
location /i/ {
alias /data/w3/images/;
}
on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.
The path value can contain variables, except document_root and realpath_root.
If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
alias /data/w3/images/$1;
}
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
root
语法
Syntax: root path;
Default:
root html;
Context: http, server, location, if in location
注意Contex的范围
Demo
Sets the root directory for requests.
For example, with the following configuration
location /i/ {
root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.
The path value can contain variables, except document_root and realpath_root.
A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.
相关文章
- Spring MVC 框架学习(六)---- 返回页面+加载静态资源
- 使用Gulp压缩静态资源
- 加载混淆的shellcode实现静态免杀
- 静态文件
- 静态路由命令配置_配置静态路由的命令格式为
- Spring MVC框架:第五章:静态资源访问与mvc:view-controller
- SpringBoot框架:第二章:SpringBoot中static和templates二个目录下的页面和静态资源访问的三个常见问题
- Netlify静态资源托管之部署自动化
- 花费3元尝试CDN分发,给静态资源提提速的同时也减少服务器压力
- 关于静态资源压缩
- vue.js客服系统实时聊天项目开发(二十一)vue项目中引入静态资源
- 静态霍夫曼编码
- 华为实验 | 配置 IPv4 静态路由基本功能
- 【前端探索】云存储用得好,静态资源没烦恼
- SpringBoot之静态资源(F)详解编程语言
- 如何在自定义Filter中优雅的实现静态资源放行详解编程语言
- 如何在Linux中删除静态路由?(linux删除静态路由)
- Redis缓存实现静态资源管理(redis 静态资源缓存)
- 快速搭建Redis静态工具类(redis 静态工具类)
- 用PHP制作静态网站的模板框架
- 从一个网站扒下的asp生成静态页面的代码脚本之家特供版
- Tomcat无法加载css和js等静态资源文件的解决思路
- C#静态static的用法实例分析
- nginx中使用nginx-http-concat模块合并静态资源文件