如何使用.htaccess禁用代理缓存

时间:2010-08-11 08:44:37

标签: apache .htaccess http caching proxy

我遇到一个问题,即公司代理服务器为不同的登录用户提供页面。我估计我可以通过禁用代理缓存来解决这个问题。 This page建议在htaccess中包含以下代码段:

ExpiresDefault A0
Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"

正如我所了解的那样(通过Google搜索),Expires标头只能由代理读取,所以我也可能只使用“Header set Expires 0”?

我认为这也会阻止样式表,图片和其他资产的缓存(尽管只能通过代理,而不是浏览器)?

处理此问题的最佳方法是什么?我正在运行PHP,如果推荐的话,也可以通过PHP轻松修改标题。

我无法访问代理服务器进行测试。

2 个答案:

答案 0 :(得分:3)

来自http 1.1 spec(RFC 2616)第14.9.1章

private
    Indicates that all or part of the response message is intended for
    a single user and MUST NOT be cached by a shared cache. This
    allows an origin server to state that the specified parts of the

标题集Cache-Control“private,...”可以解决问题。

不需要Expires标头。缓存控制:max-age覆盖       到期字段。参见RFC Section:14.21

您应该发送不同的缓存标头,具体取决于您提供的内容。

以下示例适用于在/ static中提供静态内容并为登录用户更改内容的网站。登录用户通过会话cookie的存在来识别:MYSESSID。

  • 默认允许5分钟公共缓存
  • 允许对静态文件进行365天的公共缓存
  • 允许登录用户使用5分钟私有缓存
  • 拒绝缓存/ dynamic / *

RewriteEngine On
# Flag files in /static as STATIC
RewriteRule ^static - [E=STATIC:1]

# Flag requests by logged in users as PRIVATE
# Users are identified by presence of MYSESSID cookie
# Ignores files in: /static 
RewriteCond %{HTTP_COOKIE} MYSESSID
RewriteCond %{REQUEST_URI} !^/static
RewriteRule ^ - [E=PRIVATE:1]

# Tell proxy servers that contents not in /static vary based on the given cookies
RewriteCond %{REQUEST_URI} !^/static
RewriteRule ^ - [E=VARY:1]

# Flag requests to /dynamic as NO_CACHE
RewriteRule ^dynamic - [E=NO_CACHE:1]


## Default Cache-Control
# Per default, any content is public and 5min cacheable
Header set Cache-Control "public, max-age=300"

## Static Files
# Static files are public and 365d cacheable.
Header set Cache-Control "public, max-age=31536000" env=STATIC
# Reset age, indicates objects as fresh
Header set Age 0 env=STATIC

## Private responses
# private. Allow 5min caching
Header set Cache-Control "private, max-age=300" env=PRIVATE

## Deny caching
Header set Cache-Control "private, max-age=0, no-cache, no-store, must-revalidate" env=NO_CACHE

## Vary rules
Header append Vary: Cookie env=VARY

答案 1 :(得分:0)

使用:

  

ExpiresActive On

     

现在ExpiresDefault

     

标头设置Cache-Control“no-cache,no-store,must-revalidate,max-age = 0,proxy-revalidate,no-transform”

     

标题集Pragma“no-cache”