nginx文件路径配置(root|alias)

nginx指定文件路径主要有两种方式:root|alias。

那么他们究竟有什么区别呢?

指令的使用方法和作用域:
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:location
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径。
alias的处理结果是:alias路径替换location路径。
还有一个重要的点是alias后面必须要用"/"结束。

示例:

root:

location ^~ /html/ {
    root /opt/;
}

如果一个请求的URI是/opt/index.html时,web服务器将会返回服务器上的/opt/html/index.html的文件。

alias:

location ^~ /html/ {
    alias /opt/;
}

如果一个请求的URI是/html/index.html时,web服务器将会返回服务器上的/opt/index.html的文件。因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。

注意:

1. 使用alias时,目录名后面一定要加"/"。
2. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
3. alias只能位于location块中。
4.root|alias不能作用于/tmp目录。

赠人玫瑰,手有余香,如果我的文章有幸能够帮到你,麻烦帮忙点下右下角的推荐,谢谢!

作者: imcati

出处: https://www.cnblogs.com/imcati/>

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

原文地址:https://www.cnblogs.com/imcati/p/11294024.html