pscheit
pscheit

Reputation: 2981

Tomcat 8 caches static files allthough configured otherwise

My Problem:

When I access a static file served from comcat 8 over http the content of the file is cached server sidely. I can rule the following errors out:

  1. it's not my browser cache (disabled, tried different browsers, used wget on cli). Tomcat is returning with http 200 OK
  2. It's not an configuration issue with a conflicting antiResourceLocking
  3. I don't thinks it's an configuration issue? (see below)

Here's my server.xml (I know it's not so nice to put the contexts in here..)

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8888" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

        <!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
        <Connector
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           port="8883" maxThreads="200"
           socketBuffer="-1"
           scheme="https" secure="true" SSLEnabled="true"
           keystoreFile="/vagrant/provision/configFiles/keystore" keystorePass="changeit"
           clientAuth="false" sslProtocol="TLS"/>

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

           <Host name="www.xxx.de"  appBase="webapps">
                        <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
                        <Context path="" docBase="/vagrant/webroot/xxx">
                          <Resources cachingAllowed="false" />
                        </Context>
                        <Context path="/r2d2" docBase="/vagrant/webroot/r2d2"/>
                        <Context path="/data" docBase="/vagrant/webroot/shares/data"/>
          </Host>

    </Engine>
  </Service>
</Server>

And this is my context.xml

<Context>
    <Resources cachingAllowed="false" />

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

</Context>

The context xml resources tag is only for paranoia. The server.xml resources tag should be enough without the resources tag in context.xml right? However, here is what I have observed:

When I change the file in the webroot, tomcat changes the content-length accordingly (!). So it is actually detecting that the file is changed. But the content is still wrong (adding NUL chars at the end of the file, when I put more content in it on filesystem). I always get 200 ok responses. It does work perfectly for small files, but not for "larger" files like ~> 45172 bytes

My webapps and work folder is empty(!).

Even if I restart tomcat, the file is still cached server side.. And this is where I lost my mind: Where is tomcat getting the old file contents from? I even read the sourcecode on github from tomcat and I've seen that the contents of cached files are stored in memory.. I grepped for parts of the cached file and found nothing.

For more Infos: the tomcat is running in vagrant, but it does not matter if I modify the file on the guest or on the host. From file system (guest and host) the file is changed, tomcat response is 200 but the wrong content is delivered.

tomcat-8.0.28 running.

No more ideas left :( Thank you in advance. Philipp

Upvotes: 1

Views: 5047

Answers (1)

barrenec
barrenec

Reputation: 36

Check the site with a tool and look for the headers. I guess your problem is that tomcat is sending the wrong httpheaders for you.

Per default tomcat sends every https request with httpheaders pragma: no cache. Just try the same with a https request. If you see your changes on file then you can add following snippet on your 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 0 minutes</param-value>
     </init-param>
     <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>access plus 0 minutes</param-value>
     </init-param>
     <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>access plus 0 minutes</param-value>
     </init-param>
</filter>

i hope it works

Upvotes: 2

Related Questions