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 指定的是目錄,後面必定要加上 "/"。。。測試