利用浏览器缓存 - 带 CDN 的 nginx
Leverage browser caching - nginx with CDN
我正在使用 pingdom
测试我的网站:
https://tools.pingdom.com/#!/dCLQGc/https://stagingreport.daytwo.com
并在利用浏览器缓存方面获得低分:
从具有以下配置的 nginx 服务器提供的所有静态文件
server {
listen 80;
gzip on;
gzip_vary on;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
try_files $uri $uri/ /index.html;
}
}
因为我使用google云CDN我没有在服务器上设置任何缓存配置。
为了获得更高的性能分数
我应该更改 nginx 配置中的某些内容吗?
根据下面提到的 caching details,您可能需要相应地配置 nginx:
A response can be stored in Cloud CDN caches only if all of the
following are true:
It was served by a backend service or backend bucket with Cloud CDN
enabled.
It is a response to a GET request.
Its status code is 200, 203, 206, 300, 301, 302, 307, or 410.
It has either a Content-Length header or a Transfer-Encoding header.
It has a Cache-Control: public header.
It has a Cache-Control: s-maxage, Cache-Control: max-age, or Expires
header.
It has a Content-Length, Content-Range, or Transfer-Encoding header.
Its size is less than or equal to the maximum size.
For backend buckets, you can satisfy these requirements by marking the
object as shared publicly.
There are also checks that block caching of responses. A response is
not cached if any of the following are true:
It has a Set-Cookie header.
It has a Vary header with a value other than Accept, Accept-Encoding,
or Origin.
It has a Cache-Control: no-store, no-cache, or private directive.
The corresponding request had a Cache-Control: no-store directive.
我正在使用 pingdom
测试我的网站:
https://tools.pingdom.com/#!/dCLQGc/https://stagingreport.daytwo.com
并在利用浏览器缓存方面获得低分:
从具有以下配置的 nginx 服务器提供的所有静态文件
server {
listen 80;
gzip on;
gzip_vary on;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
try_files $uri $uri/ /index.html;
}
}
因为我使用google云CDN我没有在服务器上设置任何缓存配置。
为了获得更高的性能分数 我应该更改 nginx 配置中的某些内容吗?
根据下面提到的 caching details,您可能需要相应地配置 nginx:
A response can be stored in Cloud CDN caches only if all of the following are true:
It was served by a backend service or backend bucket with Cloud CDN enabled.
It is a response to a GET request.
Its status code is 200, 203, 206, 300, 301, 302, 307, or 410.
It has either a Content-Length header or a Transfer-Encoding header.
It has a Cache-Control: public header.
It has a Cache-Control: s-maxage, Cache-Control: max-age, or Expires header.
It has a Content-Length, Content-Range, or Transfer-Encoding header.
Its size is less than or equal to the maximum size.
For backend buckets, you can satisfy these requirements by marking the object as shared publicly.
There are also checks that block caching of responses. A response is not cached if any of the following are true:
It has a Set-Cookie header.
It has a Vary header with a value other than Accept, Accept-Encoding, or Origin.
It has a Cache-Control: no-store, no-cache, or private directive.
The corresponding request had a Cache-Control: no-store directive.