301重定向后如何防止chrome从磁盘缓存加载index.html

M-N*_*M-N 5 caching google-chrome http nginx

每当我们发布网站的新版本时,Chrome 缓存都会出现问题,我们搜索了几天并添加/删除了可能导致此问题的任何标头、元标记、nginx 配置等,或者据说可以修复它,但是什么都没有改变。index.html 仍在缓存中,我们必须按 F5 才能查看随机计算机上的新更改,

该站点使用 angularjs 框架实现为 SPA,并使用位于 traefik 代理后面的 ngnix 托管,使用 traefik 规则,我们通过 301 响应强制将 HTTP 请求重定向到 HTTPS。

今天我发现了失败的场景!如果用户在地址栏中输入 https 协议(例如https://example.com),一切正常,但如果用户返回并尝试访问http://example.com,chrome 会将用户重定向到https 通过 301 重定向缓存,然后加载 index.html 的缓存版本,这不是我们想要的,缓存 301 重定向是可以的,但我们希望 chrome 从服务器获取 index.html,而不是从磁盘缓存加载它!

注意:只要用户使用 HTTPS,索引就会从服务器加载或获得 304 响应,我们希望在用户仅输入域本身而不是使用 https 时发生这种情况。

这是添加到 index.html 的元标记:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="pragma" content="no-cache" />
Run Code Online (Sandbox Code Playgroud)

这是我们 nginx 配置的一部分:

...
http {
    ...
    proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=prerender_cache:100m max_size=10g inactive=1d;
    proxy_cache_key "$request_method$host$request_uri$is_args$args";
    ...
    server {
        access_log /dev/stdout combined1;
        listen      80 default_server;
        server_name example.com;
        root /app;
        index  index.html;
        ...
        location ~ ^/(assets|static|styles) {
            expires     31d;
            add_header  Cache-Control public;
        }
        location @asset_pass {
            root app/;
            try_files $uri =404;
        }
        location / {
            expires -1;
            add_header Pragma "no-cache";
            add_header Cache-Control "no-store, no-cache, must-revalicate, post-check=0 pre-check=0";
            try_files $uri @prerender;
            autoindex on;
        }
        location ~ \.html$ {
            expires -1;
            add_header Pragma "no-cache";
            add_header Cache-Control "no-store, no-cache, must-revalicate, post-check=0 pre-check=0";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是:有没有办法解决这个问题,或者这是谷歌浏览器的默认行为?

感谢大家花时间阅读这个问题,如果问题太长,请见谅。

Noa*_*oah 0

您可以尝试touch您的index.html文件,然后响应标头last-modified将更改为最新日期时间,因此浏览器会认为该资源“已过时”,将应用最新的301规则;