【问题标题】:How to disable proxy caching with .htaccess如何使用 .htaccess 禁用代理缓存
【发布时间】:2011-03-28 06:28:30
【问题描述】:

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

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

据我所知(通过谷歌搜索),Expires 标头只能由代理读取,所以我也可以只使用“Header set Expires 0”?

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

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

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

【问题讨论】:

  • @Sumurai8 这不是骗子,因为这专门针对代理缓存而不是浏览器缓存。
  • 我想我当时没好好读书。我认为不可能可靠地做到这一点,但我们会看到。

标签: apache .htaccess http caching proxy


【解决方案1】:

来自 http 1.1 规范 (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 标头。 Cache-Control: max-age 覆盖 过期字段。请参阅 RFC 部分: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

【讨论】:

  • 你能缩短规则吗?
  • 简短的回答是:Header set Cache-Control "private, max-age=N"。它提供所有可在浏览器中缓存的内容,时间仅为N 秒。
【解决方案2】:

用途:

ExpiresActive On

ExpiresDefault 现在

标头集 Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"

标头集 Pragma "no-cache"

【讨论】:

  • 这将允许浏览器缓存和代理不?
猜你喜欢
  • 2016-10-17
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
相关资源
最近更新 更多