简体   繁体   English

利用浏览器缓存-具有CDN的Nginx

[英]Leverage browser caching - nginx with CDN

I'm testing my site with pingdom : https://tools.pingdom.com/#!/dCLQGc/https://stagingreport.daytwo.com 我正在使用pingdom测试我的网站: https : pingdom ! / pingdom / https : pingdom

And getting a low grade in leverage browser caching: 并在杠杆浏览器缓存中得分较低: 在此处输入图片说明

All static files being served from an nginx server with the following configuration 使用以下配置从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;
    }


}

Because I'm using google cloud CDN I didn't setup any caching configuration on server. 因为我使用的是Google Cloud CDN,所以未在服务器上设置任何缓存配置。

In order to get a higher performance score should I change something in the nginx configuration? 为了获得更高的性能分数,我应该在Nginx配置中进行一些更改吗?

As per the caching details mentioned below, you might need to configure the nginx accordingly: 根据下面提到的缓存详细信息 ,您可能需要相应地配置nginx:

A response can be stored in Cloud CDN caches only if all of the following are true: 仅当满足以下所有条件时,响应才能存储在Cloud CDN缓存中:

It was served by a backend service or backend bucket with Cloud CDN enabled. 它由启用了Cloud CDN的后端服务或后端存储桶提供。

It is a response to a GET request. 它是对GET请求的响应。

Its status code is 200, 203, 206, 300, 301, 302, 307, or 410. 其状态码是200、203、206、300、301、302、307或410。

It has either a Content-Length header or a Transfer-Encoding header. 它具有Content-Length标头或Transfer-Encoding标头。

It has a Cache-Control: public header. 它具有Cache-Control:公共头。

It has a Cache-Control: s-maxage, Cache-Control: max-age, or Expires header. 它具有Cache-Control:s-maxage,Cache-Control:max-age或Expires标头。

It has a Content-Length, Content-Range, or Transfer-Encoding header. 它具有Content-Length,Content-Range或Transfer-Encoding标头。

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. 它具有Set-Cookie标头。

It has a Vary header with a value other than Accept, Accept-Encoding, or Origin. 它具有Vary标头,其值不是Accept,Accept-Encoding或Origin。

It has a Cache-Control: no-store, no-cache, or private directive. 它具有一个Cache-Control:无存储,无缓存或专用指令。

The corresponding request had a Cache-Control: no-store directive. 相应的请求具有Cache-Control:no-store指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM