假设nginx.conf里有如下配置:
server{listen 80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;}location /project1{root /home;index index.html index.htm;}location /project2{alias /home/page2;index index.html index.htm;}}
location 中root和alias的区别:
root:不会丢弃location匹配模式的路径,一般用于资源文件。
实际路径:root路径 + location匹配路径 + 请求末尾路径
如: 访问/project1/a.txt,实际会去找/home/project1/a.txt。
alias:会丢弃掉locationn匹配模式的路径,可以根据不同的路由访问不同的项目。
实际路径:alias路径 + 请求末尾路径
如: 访问/project2/a.txt,实际访问/home/page2/a.txt,他舍弃了/peoject2这一段。
