Nginx配置中location、root和alias的关系一直很让人困惑,查询好多资料也没能搞明白,于是自己进行了实际操作,整理一篇小白看的懂得文章!欲知详情,请往下看! (如果你也看懂了,请帮忙点赞!)
[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指定目录上一级目录中的内容。
[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所匹配内容也只能向下匹配。
root/alias 是指定文件路径的两种方式,主要区别就是怎么解析location后面的uri。 eg: 访问:http://localhost/appImg/abc.jpg
location ^~ /appImg/{
root /home/nginx;
}
这个location相当于访问服务器上的文件路径: /home/nginx/appImg/abc.jpg 。
location ^~ /appImg/{
alias /home/nginx/;
}
这个location相当于访问服务器上的文件目录:/home/nginx/abc.jpg(即alias不会使用location后面配置的路径)。而且如果alias 指定的是目录,后面一定要加上 "/"。。。
(adsbygoogle = window.adsbygoogle || []).push({});