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

2023-09-02 00:41:53 作者:瘋子、也有瘋子的格調

我有一个问题,即企业的代理服务器不同的登录用户提供了网页。我想我可以通过禁用代理缓存解决这个问题。 此页面建议包括htaccess的以下片段:

I have a problem where corporate proxy servers serves up the page for different logged in users. I reckon I can solve this issue by disabling proxy caching. This page suggests including the following snippet in htaccess:

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

正如我理解它(谷歌搜索),Expires头唯一由代理读取,所以我也可以只使用页眉设置过期0?

As I've understood it (by Googling), the Expires header is only read by proxies, so I might also just use "Header set Expires 0"?

我想样式表,图像和其他资产,这也将prevent缓存(虽然只有代理人,而不是浏览器)?

I suppose this would also prevent caching of stylesheets, images and other assets (although only by proxies, not browsers)?

什么是对付它的最好方法?我运行PHP的,并且很容易通过PHP修改标题,同样,如果是这样的建议。

What is the best way to deal with this? I'm running PHP, and can easily modify headers through PHP, too, if that's recommended.

我没有访问代理服务器用于测试

I don't have access to a proxy server for testing.

推荐答案

从HTTP 1.1规范( RFC 2616 )第14.9.1

From http 1.1 spec (RFC 2616) chapter 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

头设置缓存控制的私人,......的伎俩。

Header set Cache-Control "private, ..." does the trick.

没有必要为Expires头。缓存控制:最大年龄覆盖       过期时间字段。参见RFC节:14.21

There is no need for the Expires header. Cache-Control: max-age overrides the Expires field. See RFC Section: 14.21

您应该送这取决于你提供的内容不同的缓存头。

You should send different caching headers depending on the content you deliver.

下面的例子是一个网站提供静态内容/静态的,各不相同登录的用户的内容。登录的用户标识的会话cookie的presence:MYSESSID

The following example is for a website delivering static contents in /static and vary content for logged in users. Logged in users are identified by presence of the session cookie: MYSESSID.

在默认情况下,允许5分钟公共缓存 在允许对静态文件365天公共缓存 允许5分钟的私人缓存登录的用户 拒绝在高速缓存/动态/ *
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
 
精彩推荐
图片推荐