使用 Apache httpd,如何为给定的 UserAgent 配置不缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/596520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With Apache httpd, how do I configure no caching for a given UserAgent?
提问by Eddie
I have Apache HTTPD configured to add a cache header to requests for most static content:
我将 Apache HTTPD 配置为向大多数静态内容的请求添加缓存标头:
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 UserAgentcontains 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 anyCache-Controlor other cache-related headers on the HTTP response.
如何为UserAgent包含字符串 JNLP 的任何请求禁用此缓存?当请求来自用户代理 JNLP(例如“ User-Agent: JNLP/6.0 javaws/1.6.0_12 (b04) Java/1.6.0_12”)时,我不希望HTTP 响应中包含任何Cache-Control或其他与缓存相关的标头。
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.
我可以找到一些基于用户代理的配置示例,但我无法弄清楚如何根据用户代理配置缓存。
回答by David Z
Your ExpiresByTypedirective looks like a good idea... if that's not enough, then try using BrowserMatch:
您的ExpiresByType指令看起来是个好主意……如果这还不够,请尝试使用BrowserMatch:
BrowserMatch JNLP ua_is_jnlp
This sets the environment variable ua_is_jnlpto 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
ua_is_jnlp对于用户代理标头包含字符串的任何请求,这会将环境变量设置为某个值JNLP(还有一个不区分大小写的变体,BrowserMatchNoCase)。然后你可以清除任何缓存头
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

