5

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

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

<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> 

任何想法?

4

1 回答 1

6

您能否按照容器提供的过滤器 > ExpiresFilter > 故障排除中的描述启用 ExpiresFilter 日志:

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

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

您将看到如下日志消息:

添加了过期标头:

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
于 2013-07-26T20:40:10.980 回答