With Apache httpd, how do I configure no caching for a given UserAgent?
-
09-09-2019 - |
Question
I have Apache HTTPD configured to add a cache header to requests for most static content:
ExpiresActive On
ExpiresDefault "access plus 1 year"
# Force JNLP and BSH files to expire immediately so updates are checked for
# and seen (We need this so we see changes in the dynamic content in both)
ExpiresByType application/x-java-jnlp-file "now"
ExpiresByType application/x-bsh "now"
How can I disable this caching for any request where the UserAgent
contains the string JNLP? When the request comes from the User Agent JNLP (for example "User-Agent: JNLP/6.0 javaws/1.6.0_12 (b04) Java/1.6.0_12
") I don't want any Cache-Control
or other cache-related headers on the HTTP response.
I can find configuration examples for several things based on user agent, but I cannot figure out how to configure caching depending on the user agent.
Solution
Your ExpiresByType
directive looks like a good idea... if that's not enough, then try using BrowserMatch
:
BrowserMatch JNLP ua_is_jnlp
This sets the environment variable ua_is_jnlp
to some value for any request whose user agent header contains the string JNLP
(there is also a case-insensitive variant, BrowserMatchNoCase
). Then you can clear any caching headers with
Header unset Cache-Control env=ua_in_jnlp
Header unset Expires env=ua_in_jnlp
although if you want to disable caching entirely, you'd be better off setting
Header set Cache-Control no-cache env=ua_in_jnlp
Header set Expires 0 env=ua_in_jnlp