root: 指定文件根文件夹对应的/URL路径,将URL路径附加到根位置来形成要提供的最终文件路径
location ^~ /img {
root /var/www/static;
try_files $uri $uri/ =404;
}
如上配置,当用户访问https://www.wljslmz.cn/img/wljslmz.png
时,会找到/var/www/static/img/wljslmz.png
图片。
alias: 将 URL 重新映射到根位置以外的其他目录
location ^~ /img {
alias /var/www/static/images/;
try_files $uri $uri/ =404;
}
如上配置,当用户访问https://www.wljslmz.cn/img/wljslmz.png时,会找到/var/www/static/images/wljslmz.png图片。
对于alias指定的文件夹,需要加“/”
不同点:
- root读取的时根目录。可以在server或location指令中使用。
- alias只能在location指令中使用。
static模块两个指令root|alias
Content阶段有一个非常常用的模块static模块,这个模块提供的root alias指令是我们非常常用的,默认是nginx框架当中的,是没有办法移除出框架当中的,root alias两个指令都是用于把url映射为文件路径,以返回静态文件内容。
在配置nginx时,没有搞明白整个访问资源时所走的路径,总是会出现由于测试所写的url与配置文件中的不统一,导致返回404的状态码。
nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应。root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。nginx指定文件路径有两种方式root
和alias
root|alias语法
root
语法: root path;
默认值:root html;
配置段:http、server、location、if
alias
语法: alias path;
配置段:location
Location当中我们可以配置url和用户请求的url做匹配,root会将location后的url也添加到root指定的path之后。而alias对location后的url不会添加到path之后。
root是又默认配置的,它的值是html,上下文很多。而alias只能配置在location当中。所以root的使用范围更加的广(如放在http,server块当中,那么location就可以使用父配置块的指令)。
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:
root路径+location路径
alias的处理结果是:使用alias路径替换
location路径
alias是一个目录别名的定义,root则是最上层目录的定义。还有一个重要的区别是alias后面必须要用"/"
结束,否则会找不到文件的,而root则可有可无
alias标签和root标签到底有哪些区别
1、alias后跟的指定目录是准确的,并且末尾必须加“/”,否则找不到文件
示例:
1 2 3 | location ^~ /t/ { alias /www/root/html/new_t/; } |
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
2、root后跟的指定目录是上级目录,并且该上级目录下要含有和location后指定名称的同名目录才行,末尾“/”加不加无所谓。
示例:
1 2 3 | location ^~ /t/ { root /www/root/html/; } |
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
举几个例子如下
root路径映射规则:
[root@www ~]# mkdir -p /usr/local/nginx/html/first
[root@www ~]# echo "1.txt" > /usr/local/nginx/html/first/1.txt
location /root {
root html;
}
location ~ /root/(\w+\.txt) {
root html/first/$1;
}
(1)root后面不加上反斜杠/,即访问/root
[root@www ~]# curl 192.168.179.99/root --很好,访问出错了,来看看日志吧
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@www ~]# tail -f /usr/local/nginx/logs/error.log --可以看到访问路径是/html/root,只是访问路径并没有去访问资源
2020/05/01 18:56:43 [error] 96566#0: *23 open() "/usr/local/nginx/html/root" failed (2: No such file or directory), client: 192.168.179.99, server: www.test.com, request: "GET /root HTTP/1.1", host: "192.168.179.99"
(2)root后面加上反斜杠/,即访问/root/
[root@www ~]# curl 192.168.179.99/root/ --这里root加上反斜杠/访问的是/root/index.html。但是这个资源并不存在,和上面不加上反斜杠访问的路径是不一样的
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@www ~]# tail -f /usr/local/nginx/logs/error.log
2020/05/01 19:21:40 [error] 97380#0: *32 "/usr/local/nginx/html/root/index.html" is not found (2: No such file or directory), client: 192.168.179.99, server: www.test.com, request: "GET /root/ HTTP/1.1", host: "192.168.179.99"
----------------------------------------------------------------------------------------[root@www ~]# curl 192.168.179.99/root/1.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@www ~]# tail -f /usr/local/nginx/logs/error.log --其实访问的是html/first/1.txt
2020/05/01 19:05:00 [error] 97035#0: *27 open() "/usr/local/nginx/html/first/1.txt/root/1.txt" failed (20: Not a directory), client: 192.168.179.99, server: www.test.com, request: "GET /root/1.txt HTTP/1.1", host: "192.168.179.99"
举例2:
location块匹配到的url为"/demo",root指令的路径为"/opt/root_test",那么,根据上述配置,当我们访问"/demo"这个url的时候,实际上访问的到底是服务器中的那个路径呢?
答案为"/opt/root_test/demo"路径,怎么得到的答案呢?方法其实很简单,我们只需要将location中的rul添加到root指令对应的路径后面即可,得到最终服务器访问的路径,具体方法如下图所示:
也就是说,上述配置表示,当我们访问"/demo"这个url时,实际访问的时服务器的"/opt/root_test/demo"目录,那么,让我们来验证一下,看看实际情况如何,首先,我们先创建"/opt/root_test/demo"目录,并在此目录中放置一张示例图片以便演示,示例图片为ms.jpg,我已经在默认的的server块中配置了上图的location,然后重载配置。
-
浏览器访问http://172.20.26.104/demo/ms.jpg最终效果如下:
alias映射规则:
[root@www ~]# mkdir -p /usr/local/nginx/html/first
[root@www ~]# echo "1.txt" > /usr/local/nginx/html/first/1.txt
location /alias{
alias html;
}
location ~ /alias/(\w+\.txt) {
alias html/first/$1;
}
# Alias有个特征/alias/(\w+\.txt),不会将location匹配的前缀字符串填到后面去,需要使用$1取出
来,访问/alias/1.txt会映射到html/first/1.txt。而这个文件是存在的
(1)alias后面加上反斜杠,即/alias/
[root@www ~]# curl 192.168.179.99/alias/ --可以看到访问alias映射到html下面的首页资源。这里是alias后面加上反斜杠/,如果不加上反斜杠呢??
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
(2)alias后面不加上反斜杠,即/ailas,这样会301重定向
[root@www ~]# curl 192.168.179.99/alias --可以看到如果不加上反斜杠会发生重定向
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@www ~]# curl 192.168.179.99/alias/1.txt --可以看到正确返回资源了
1.txt
1. 使用alias时,目录名后面一定要加"/"(上面不加上反斜杠会发生重定向)
2. alias可以指定任何名称
3. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用
4. alias只能位于location块中
总结
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无。