Google Cloud Platform系列

配置http响应头部信息以适应CDN要求

首先是配置Cache-Control字段,在apache配置文件中写入如下设置,要求启用相应的模块才能生效

<IfModule mod_headers.c>
# YEAR
<FilesMatch "\.(flv|gif|ico|jpeg|jpg|png|woff)$">
Header set Cache-Control "max-age=12000, public"
</FilesMatch>

# WEEK
<FilesMatch "\.(pdf|swf|css|js)$">
Header set Cache-Control "max-age=7000, public"
</FilesMatch>

# DAY
<FilesMatch "\.(htm|html|shtml)$">
Header set Cache-Control "max-age=5400, public"
</FilesMatch>

后来又出现了一个文件color.php没有被cdn cache,所以我要对这个单独的文件设置Cache-Control,同时因为该文件头部自带very: Cookie字段,导致不会被缓存,所以我还要删除或者修改very,这个问题在StackOverFlow上是这样解释的:
Simple answer below, but you need some shenanigans to avoid sending an empty Vary: header (try to avoid it out of paranoia)

Header edit Vary (.*)User-Agent(.*) $1$2
# Remove leading or trailing ',' without making things complex above
Header edit Vary ^,?(.*),?$ $1
# 2.4-only: remove empty Vary header
Header unset Vary "expr=resp('Vary') =~ /^$/"

x-never-match can be any unlikely-to-ever-be-used header name.
最后我通过一个简单的方法,就是设置Very来覆盖原有的头

<FilesMatch "color\.php">
Header set Cache-Control "max-age=3600, public"
Header set Vary "Accept-Encoding"
</FilesMatch>
(0)

本文由 永烁星光 作者:linus 发表,转载请注明来源!

关键词:,

热评文章

发表评论