因同一个server下location root只能配置一个,此时需要使用虚拟目录alias。
location /mobile/ {
alias /home/mobile/
try_files $uri $uri/ /mobile/index.html;
}
这样配置当访问的/mobile/下资源不存在时会跳转到/mobile/index.html。
try_file简单介绍:
一.try_files的语法规则:
格式1:try_files file … uri; 格式2:try_files file … =code;
可应用的上下文:server,location段
二.举例说明:
location /images/ {
root /opt/html/;
try_files $uri $uri/ /images/default.gif;
}
比如 请求 127.0.0.1/images/test.gif 会依次查找
1.文件/opt/html/images/test.gif
2.文件夹 /opt/html/images/test.gif/下的index文件
3. 请求127.0.0.1/images/default.gif
三.其他注意事项
1.try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页 即 访问127.0.0.1/images/ 不会去访问 127.0.0.1/images/index.html
四.其他用法
location / {
try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@mongrel;
}
location @mongrel {
proxy_pass http://mongrel;
}
以上中若未找到给定顺序的文件,则将会交给location @mongrel处理(相当于匹配到了@mongrel来匹配)