如何仅在NGINX中的index.html页面上不设置缓存

pik*_*ike 5 caching nginx cache-control

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

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;
}
Run Code Online (Sandbox Code Playgroud)

这样,我就防止了整个应用程序的缓存,这是不希望的,因为我只想防止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';
}
Run Code Online (Sandbox Code Playgroud)

NGINX可以运行,但是index.html似乎仍在存储高速缓存,就好像add_header它不存在一样。

我还有其他命令吗?

小智 5

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

  • 无缓存:防止未经验证就重用资源
  • 无存储:防止在客户端存储高速缓存

在您的情况下,我会尝试这样做:

location = /index.html {
    internal;
    add_header Cache-Control 'no-store';
}
Run Code Online (Sandbox Code Playgroud)

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