Google App Engine添加缓存控制和其他标头 [英] Google App Engine adding cache-control and other headers

查看:48
本文介绍了Google App Engine添加缓存控制和其他标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google App Engine上有一个Flask应用程序,我想告诉浏览器缓存使用 Cache-Control 标头的响应.它可以在 dev_appserver.py 上正常运行,但是在部署到App Engine时,标头会被修改并破坏缓存标头.

I have a Flask application on Google App Engine and I want to tell browsers to cache a response that uses the Cache-Control header. It works as expected on dev_appserver.py but when deployed to App Engine the headers are modified and break the cache header.

以下是Flask视图:

Here is the Flask view in particular:

@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的日志均显示:

The logs for both development server and App Engine show:

Content-Type: text/html; charset=utf-8
Content-Length: 112628
Cache-Control: max-age=31536000

当我在开发应用服务器上运行它时,它可以按预期工作,如下面的标题所示.

When I run it with the development app server it works as expected, as you can see from the headers below.

当我打开Chrome的开发工具时,App Engine的标题为:

When I open Chrome's development tools the headers for App Engine are:

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

相反,开发应用程序服务器标头与预期的一样:

In contrast the development app server headers are as expected:

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 )添加到给定视图之外.

Of course I have checked to make sure that I am not adding the extra headers, and I could find no reference of the cache-related headers (pragma, expires and cache-control) being added outside the given view.

因此,似乎App Engine在部署时会添加一堆标题,这似乎很不正常.我可能忽略了什么?

So it seems App Engine is adding a bunch of headers when deployed, which seems unusual. What might I have overlooked?

-编辑-

作为@dinoboff 在以下评论中从文档中注意到:

As @dinoboff noted from the docs in a comment below:

缓存控制,过期和变化

Cache-Control, Expires and Vary

这些标头指定对中间Web代理(例如Internet服务提供商)和浏览器的缓存策略.如果您的脚本设置了这些标头,则它们通常不会被修改,除非响应具有Set-Cookie标头,或者是为使用管理员帐户登录的用户生成的标头.

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.

推荐答案

这些标头是添加的其他标头,因为您将站点视为已登录的管理员用户.对于普通"用户,它们将不存在.

These headers are additional headers that are added because you are looking at the site as a logged in admin user. They will not be present for "normal" users.

此博客文章专门讨论了 X-AppEngine-Resource-Usage 标头:

This blog post talks about the X-AppEngine-Resource-Usage header specifically: http://googleappengine.blogspot.co.uk/2009/08/new-features-in-124.html

正如他们所指出的那样:

And as they note:

您可以使用Firefox的Live HTTP等插件查看这些标头标头或萤火虫.请注意,只有登录的管理员才能看到这些信息数字-普通用户和未登录的用户将看不到他们.

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.

这篇关于Google App Engine添加缓存控制和其他标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆