一、try_files
Syntax: | try_files try_files |
---|---|
Default: | — |
Context: | server , location |
按照请求的资源,顺序检查资源是否存在,返回第一个找到的资源,如果没有找到请求的资源,则会使用最后一个参数进行内部重定向。
例1:www.example.com/a/b (假设资源不存在)
location /a/ {
try_files $uri /a/default.gif;
}
会先找/a/b,没有找到,然后nginx就返回default.gif,此时浏览器地址栏上还是显示的/a/b
例2:
效果是访问不存在路径时返回主页,但是浏览器地址栏不会变化,比如访问:www.a.b/yyy,不存在时,就会自动返回主页。
location / {
root /usr/share/nginx/html/dev/web;
index index.html index.htm;
add_header Cache-Control no-cache;
try_files $uri $uri/ /index.html;
}
二、缓存压缩相关
这里出现了好几个JavaScript的类型,是为了兼容才写了这么多
text/javascript 内容类型已过时。在HTML的早期就使用了它。
application / x-javascript是一种实验性的内容类型(因此为x-)。不应该在应用程序中使用它。
application / javascript是正确的内容类型。
$request_filename是nginx内置变量,
~* 不区分大小写的正则
server {
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript application/javascript text/css application/xml;
gzip_vary on;
listen 80;
server_name a.b.com;
location / {
if ($request_filename ~* .*.(?:index.html)$) {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}
root /usr/a/b/c;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}