Tomcat ExpiresFilter 无法正常工作 [英] Tomcat ExpiresFilter not working correctly

查看:35
本文介绍了Tomcat ExpiresFilter 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 Tomcat 发送具有正确过期定义的图像.浏览器不断发送已下载图像的获取请求,Tomcat 以 304 响应.我想要的是 Tomcat 将使用适当的 expires 标头响应初始请求,并且没有任何 Last-modified 标头,因此浏览器将使用本地缓存,直到文件每次加载页面时,无需前往服务器查看图像是否已更改.

I can't get Tomcat to send images with the correct expires definition. The browser keep sending get requests for images already downloaded and Tomcat responds with 304. What I would want is that Tomcat will respond to the initial request with proper expires header and without any Last-modified header so the browser will use local cache until the file expires without going to the server each page load to see if the image had changed.

我的 web.xml 文件中有以下定义:

I have the following definition in my web.xml file:

<filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 1 weeks</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>modification plus 0 minutes</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>modification plus 0 minutes</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping> 

有什么想法吗?

推荐答案

您能否按照 容器提供的过滤器 > ExpiresFilter > 故障排除 :

$CATALINA_BASE/conf/logging.properties中添加以下声明:

org.apache.catalina.filters.ExpiresFilter.level = FINE

您将看到如下日志消息:

You will see log message such as the following:

添加了过期标头:

Mar 26, 2010 2:09:47 PM org.apache.catalina.filters.ExpiresFilter onBeforeWriteResponseBody
  FINE: Request "/tomcat.gif" with response status "200"
  content-type "image/gif", set expiration date 3/26/10 2:19 PM

未添加过期标头:

Mar 26, 2010 2:10:27 PM org.apache.catalina.filters.ExpiresFilter onBeforeWriteResponseBody
  FINE: Request "/docs/config/manager.html" with response status "200"
  content-type "text/html", no expiration configured

这篇关于Tomcat ExpiresFilter 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆