Java 使用Nginx反向代理的SpringBoot:css文件总是使用缓存版本获取
我使用SpringBoot作为后端,thymeleaf作为模板,Nginx作为反向代理服务器。我的一些配置类似于:Java 使用Nginx反向代理的SpringBoot:css文件总是使用缓存版本获取,java,spring,nginx,caching,Java,Spring,Nginx,Caching,我使用SpringBoot作为后端,thymeleaf作为模板,Nginx作为反向代理服务器。我的一些配置类似于: SpingBoot应用程序的一部分。属性: server.port=9090 server.error.whitelabel.enabled=false server.compression.enabled=true spring.resources.cache.cachecontrol.cache-public=true spring.resources.cache.cache
SpingBoot应用程序的一部分。属性:
server.port=9090
server.error.whitelabel.enabled=false
server.compression.enabled=true
spring.resources.cache.cachecontrol.cache-public=true
spring.resources.cache.cachecontrol.no-cache=false
spring.resources.cache.cachecontrol.no-store=false
spring.resources.cache.cachecontrol.must-revalidate=false
spring.resources.cache.cachecontrol.max-age=604800
spring.resources.cache.period=604800
spring.resources.chain.html-application-cache=true
spring.resources.chain.gzipped=true
spring.resources.chain.cache=true
nginx.conf的一部分:
server {
location /
{
proxy_pass http://127.0.0.1:9090/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
location ~ .*\.(css|js|gif|jpg|jpeg|png|bmp|swf|mp4)$
{
proxy_pass http://127.0.0.1:9090;
}
location ~ .*\.(js|css)?$
{
expires 7d;
error_log off;
access_log /dev/null;
}
}
当应用程序联机时,我发现当请求一个静态css文件时,它总是使用旧版本获取,即使我对其进行了多次更新。然后我试着:
- 清除浏览器的缓存并更改新浏览器
- 更改
&spring.resources.cache.period=1
spring.resources.cache.cachecontrol.max age=1
- 将nginx的css过期时间更改为-1
- 重新启动/重新加载nginx,重新启动服务器
我知道散列文件并添加一个
v?=..
参数可以解决这个问题,我想要另一个解决方案,就像我可以手动控制和清除缓存一样如果这是一个面向internet的web应用程序,就无法手动控制缓存,因为您无法控制世界上所有的中间缓存和浏览器。如您所知,最好的方法是为每个更新的资源使用唯一的文件名/URL。对于静态资产,我们通常为部署的每个更新添加唯一的版本/内部版本号。