如何在JBoss EAP6.4中为静态文件index.html设置Cache Control头?
为JBoss EAP 6.4的静态资源配置Cache-Control响应头
不用纠结Java代码,在JBoss的standalone.xml里通过Web子系统的响应头过滤器就能全局配置静态资源的Cache-Control头,具体操作步骤如下:
找到Web子系统配置段
打开standalone.xml,定位到<subsystem xmlns="urn:jboss:domain:web:1.5" ...>节点(注意EAP6.4对应的web子系统版本是1.5,要确保xmlns属性和这个版本匹配)。添加响应头过滤器
在该子系统的<filters>节点下(如果没有<filters>节点就直接新建一个),添加自定义的响应头过滤器,用来设置Cache-Control的具体值:<filters> <!-- 这里可以自定义缓存策略,示例是设置一年的缓存有效期,public允许中间缓存节点存储资源 --> <response-header name="StaticAssetCacheControl" header-name="Cache-Control" header-value="public, max-age=31536000" /> </filters>你可以根据需求调整
header-value,比如需要禁止缓存的话,可设置为no-cache, no-store, must-revalidate。映射过滤器到目标静态资源
找到该子系统下的<virtual-server name="default-host" ...>节点,添加<filter-mapping>规则来指定哪些静态资源应用这个缓存策略:<virtual-server name="default-host" enable-welcome-root="true"> <alias name="localhost"/> <alias name="your-domain.com"/> <!-- 仅针对index.html单个文件配置缓存 --> <filter-mapping name="StaticAssetCacheControl" url-pattern="/index.html" /> <!-- 如果需要覆盖所有静态资源,可添加多个匹配规则,比如: --> <!-- <filter-mapping name="StaticAssetCacheControl" url-pattern="*.css" /> --> <!-- <filter-mapping name="StaticAssetCacheControl" url-pattern="*.js" /> --> <!-- <filter-mapping name="StaticAssetCacheControl" url-pattern="*.png" /> --> </virtual-server>重启JBoss使配置生效
保存修改后的standalone.xml,重启JBoss EAP服务,新的缓存头配置就会生效了。
这个方案完全不需要修改任何Java代码,通过JBoss原生配置就能实现全局或特定静态资源的缓存头设置,刚好匹配你的场景。
内容的提问来源于stack exchange,提问作者Mohit
相关产品推荐
相关产品推荐

