python - Google App Engine 添加缓存控制和其他 header

标签 python google-app-engine http-caching

我在 Google App Engine 上有一个 Flask 应用程序,我想告诉浏览器缓存使用 Cache-Control header 的响应。它在 dev_appserver.py 上按预期工作,但部署到 App Engine 时, header 会被修改并破坏缓存 header 。

这里特别是 Flask View :

@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 的 header 为:

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

相比之下,开发应用程序服务器 header 符合预期:

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

当然,我已经检查以确保我没有添加额外的 header ,并且我找不到与缓存相关的 header 的引用(pragmaexpirescache-control)被添加到给定 View 之外。

看来 App Engine 在部署时添加了一堆 header ,这看起来很不寻常。我可能忽略了什么?

-- 编辑--

作为@dinoboff noted from the docs in a comment below :

Cache-Control, Expires and Vary

These headers specify caching policy to intermediate web proxies (such as Internet Service Providers) and browsers. If your script sets these headers, they will usually be unmodified, unless the response has a Set-Cookie header, or is generated for a user who is signed in using an administrator account.

最佳答案

这些 header 是由于您以登录的管理员用户身份查看该网站而添加的附加 header 。对于“普通”用户来说,它们不会出现。

这篇博文具体讨论了 X-AppEngine-Resource-Usage header :http://googleappengine.blogspot.co.uk/2009/08/new-features-in-124.html

正如他们所指出的:

You can view these headers using plugins such as Firefox's Live HTTP Headers or Firebug. Note that only logged in administrators see these figures - ordinary users, and users who aren't logged in, won't see them at all.

关于python - Google App Engine 添加缓存控制和其他 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292923/

相关文章:

javascript - 根据 Google PageSpeed Insights 建议,利用 maps.googleapis 浏览器缓存

php - 停止向 PHP 文件发送不必要的帖子

python - Pandas 在两个数据帧之间执行 SQL 减法

java - 使用 Restful Service(端点)或带有应用程序引擎的 servlet 哪个更可取?

http - 跨站点缓存 Access-Control-Allow-Origin 值

asp.net-mvc-3 - 为什么我的图像没有缓存?

python - 获取字典中任何未知内容的所有第一项

python - Python 中的 AI 工具入门

python - 安装 Google Cloud SDK 时出错

json - AppEngine Go 应用程序中的奇怪行为