【问题标题】:How to set no cache on only the index.html page in NGINX如何仅在 NGINX 中的 index.html 页面上设置不缓存
【发布时间】:2018-08-07 06:38:19
【问题描述】:

我当前的项目几乎使用以下配置:

root %%COMP_WEB_ROOT;

# COMP Static File serving
location /comp {
    alias %%COMP_WEB_ROOT;
    try_files $uri$args $uri$args/ /index.html;
    add_header 'Cache-Control' 'no-cache, no-store, must-revalidate';
}

# Hide the index file, not exposing that path specifically
location = /index.html {
    internal;
}

这样,我将阻止整个应用程序的缓存,这是不可取的,因为我只想阻止 index.html 页面存储缓存。

所以我尝试将add_header 行放在第二个块中,如下所示:

root %%COMP_WEB_ROOT;

# COMP Static File serving
location /comp {
    alias %%COMP_WEB_ROOT;
    try_files $uri$args $uri$args/ /index.html;
    error_page 401 = @error401web;
}

# Hide the index file so that we're not exposing that path specifically
location = /index.html {
    internal;
    add_header 'Cache-Control' 'no-cache, no-store, must-revalidate';
}

NGINX 能够运行,但 index.html 似乎仍在存储缓存,就好像 add_header 不存在一样。

我还缺少其他命令吗?

【问题讨论】:

    标签: caching nginx cache-control


    【解决方案1】:

    我只想阻止 index.html 页面存储缓存。

    • no-cache:防止在没有验证的情况下重用资源
    • no-store:防止在客户端存储缓存

    在你的情况下,我会试试这个:

    location = /index.html {
        internal;
        add_header Cache-Control 'no-store';
    }
    

    ...然后 CTRL+F5 强制刷新浏览器中的所有资源。
    从这一点开始,您的 index.html 应该按照您想要的方式工作。

    【讨论】:

    • 您好,感谢您的回答。但是有一个问题,如果不希望我的用户强制刷新,我如何确保不保存缓存?目前,似乎仍然为 index.html 存储缓存。
    • 你能看到服务器发送的缓存控制头吗?用户不需要强制刷新。我只是想确保您正在使用最新的文件和配置测试更改。
    • 我明白了。就我而言,当我在 /index.html 的位置块中使用 add_header 时,我没有看到缓存控制标头。当我在 /comp 的位置块中使用 add_header 时,我看到了缓存控制。你认为我需要修改我的 try_files 行吗?
    • 这个 index.html 文件是否在 comp 目录中?
    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 2017-03-28
    • 2018-09-01
    • 2020-09-09
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多