【发布时间】:2014-02-13 02:25:15
【问题描述】:
我在 Google App Engine 上有一个 Flask 应用程序,我想告诉浏览器缓存使用 Cache-Control 标头的响应。它在 dev_appserver.py 上按预期工作,但在部署到 App Engine 时,标头会被修改并破坏缓存标头。
这里特别是 Flask 视图:
@app.route("/resource")
def resource():
response = make_response(render_template("resource.html"))
response.headers['Cache-Control'] = "max-age=31536000"
logging.error("HEADERS: {}".format(response.headers))
return response
开发服务器和 App Engine 的日志显示:
Content-Type: text/html; charset=utf-8
Content-Length: 112628
Cache-Control: max-age=31536000
当我使用开发应用服务器运行它时,它按预期工作,您可以从下面的标题中看到。
当我打开 Chrome 的开发工具时,App Engine 的标题是:
alternate-protocol:443:quic
cache-control:no-cache, must-revalidate
content-encoding:gzip
content-length:19520
content-type:text/html; charset=utf-8
date:Wed, 22 Jan 2014 19:53:47 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:Google Frontend
set-cookie:session=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
status:200 OK
strict-transport-security:max-age=31536000
vary:Accept-Encoding
version:HTTP/1.1
x-appengine-estimated-cpm-us-dollars:$0.002267
x-appengine-resource-usage:ms=7388 cpu_ms=5069
x-frame-options:DENY
x-ua-compatible:chrome=1
相比之下,开发应用服务器标头符合预期:
Cache-Control:max-age=31536000, private
Content-Length:112628
content-type:text/html; charset=utf-8
Date:Wed, 22 Jan 2014 19:57:05 GMT
Expires:Wed, 22 Jan 2014 19:57:05 GMT
Server:Development/2.0
set-cookie:session=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
x-frame-options:DENY
x-ua-compatible:chrome=1
当然,我已经检查以确保我没有添加额外的标头,并且我找不到在给定之外添加的与缓存相关的标头(pragma、expires 和 cache-control)的引用查看。
所以看起来 App Engine 在部署时添加了一堆标头,这似乎很不寻常。我可能忽略了什么?
-- 编辑--
作为@dinoboff noted from the docs in a comment below:
缓存控制、过期和变化
这些标头为中间 Web 代理(例如 Internet 服务提供商)和浏览器指定缓存策略。如果您的脚本设置了这些标头,它们通常不会被修改,除非响应具有 Set-Cookie 标头,或者是为使用管理员帐户登录的用户生成的。
【问题讨论】:
-
IIRC 我认为其中一些(x-app)是因为您以登录管理员用户的身份查看它?
-
@PaulCollingwood:我认为您已经找到了答案——当我以非管理员身份尝试该页面时,我得到了预期的结果。随意张贴作为答案,我会标记它是正确的。
-
听起来不错,很高兴为您提供帮助 :)
-
还有人面临类似的问题吗?我一直在尝试为特定文件 (
cache-control: max-age=604800) 设置缓存控制标头,但在响应标头 (cache-control:max-age=604800,public, max-age=600) 中仍然有一个后缀。我确保我已从管理员帐户注销并以隐身模式访问文件,没有任何帮助。在开发服务器中,我得到了正确的标题。有人对此有解释吗?
标签: python google-app-engine http-caching