Nginx配置中location、root和alias的关系一直很让人困惑,查询好多资料也没能搞明白,因而本身进行了实际操做,整理一篇小白看的懂得文章!欲知详情,请往下看! (若是你也看懂了,请帮忙点赞!)html
[root@adailinux vhost]# cat rio.conf server { listen 80; server_name rio.com; location /r/ { root /data/wwwroot/rio.com/; } }
[root@adailinux vhost]# tree /data/wwwroot/rio.com/ /data/wwwroot/rio.com/ ├── file1.html └── r ├── file2.html └── t └── file3.html
[root@adailinux vhost]# cat rio.conf server { listen 80; server_name rio.com; location /r/ { root /data/wwwroot/rio.com/t/; } } [root@adailinux vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@adailinux vhost]# /usr/local/nginx/sbin/nginx -s reload
location和root组合至关于在root指定目录下进行location匹配,location所匹配内容必须保证在root指定目录的子目录,不然配置无效,并且location只能向下匹配,不能匹配location指定目录上一级目录中的内容。linux
[root@adailinux vhost]# cat rio.conf server { listen 80; server_name rio.com; location /r/ { alias /data/wwwroot/rio.com/r/; } }
[root@adailinux vhost]# cat rio.conf server { listen 80; server_name rio.com; location /r/ { alias /data/wwwroot/rio.com/; } } [root@adailinux vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@adailinux vhost]# /usr/local/nginx/sbin/nginx -s reload
location与alias组合,须要保证location匹配目录与alias指定目录级别相同,不然配置无效,与location和root组合相同的是,location所匹配内容也只能向下匹配。nginx
root/alias 是指定文件路径的两种方式,主要区别就是怎么解析location后面的uri。
eg: 访问:http://localhost/appImg/abc.jpg服务器
location ^~ /appImg/{ root /home/nginx; }
这个location至关于访问服务器上的文件路径: /home/nginx/appImg/abc.jpg 。app
location ^~ /appImg/{ alias /home/nginx/; }
这个location至关于访问服务器上的文件目录:/home/nginx/abc.jpg(即alias不会使用location后面配置的路径)。并且若是alias 指定的是目录,后面必定要加上 "/"。。。测试