Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置HTTP头(用于缓存控制)?_Http_Browser Cache - Fatal编程技术网

如何设置HTTP头(用于缓存控制)?

如何设置HTTP头(用于缓存控制)?,http,browser-cache,Http,Browser Cache,如何为我的站点启用浏览器缓存?我是否只是像这样将cache-control:public放在我的头中的某个位置 我正在使用最新版本的PHP在最新版本的XAMPP上开发。要在HTML中使用缓存控制,请使用,例如 内容字段中的值定义为以下四个值之一 有关缓存控制标题的一些信息如下 HTTP 1.1。允许值=公共|私有|无缓存|无存储 公共-可以缓存在公共共享缓存中。 专用-只能缓存在专用缓存中。 无缓存-可能无法缓存。 无存储-可以缓存但不能存档 指令CACHE-CONTROL:NO-CAC

如何为我的站点启用浏览器缓存?我是否只是像这样将cache-control:public放在我的头中的某个位置



我正在使用最新版本的PHP在最新版本的XAMPP上开发。

要在HTML中使用缓存控制,请使用,例如


内容字段中的值定义为以下四个值之一

有关
缓存控制
标题的一些信息如下

HTTP 1.1。允许值=公共|私有|无缓存|无存储

公共-可以缓存在公共共享缓存中。
专用-只能缓存在专用缓存中。
无缓存-可能无法缓存。
无存储-可以缓存但不能存档

指令CACHE-CONTROL:NO-CACHE指示不应使用缓存的信息 相反,请求应该转发到源服务器。该指令与PRAGMA:NO-CACHE具有相同的语义

当向未知符合HTTP/1.1的服务器发送无缓存请求时,客户端应同时包含PRAGMA:NO-CACHE和CACHE-CONTROL:NO-CACHE。另请参见过期

注意:在HTTP中指定缓存命令可能比在META语句中更好,在META语句中,缓存命令的影响不仅限于浏览器,还包括可能缓存信息的代理和其他中介


对于Apache服务器,您应该检查设置Expires和缓存控制头

或者,您可以使用指令自行添加缓存控制:

Header set Cache-Control "max-age=290304000, public"
您可以使用:


请注意,使用的确切标题将取决于您的需要(如果您需要支持和/或)

页面建议使用以下内容:

添加缓存控制头 这将放在root.htaccess文件中,但如果您有权访问 httpd.conf更好。

这段代码使用FilesMatch指令和Header指令向某些文件添加缓存控制头

# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
#480周
标题集缓存控制“最大年龄=290304000,公共”
最好使用文件
.htaccess
。但是,请注意将内容留在缓存中的时间

使用:


标题集缓存控制“最大年龄=604800,公共”
其中:604800=7天


PS:这可用于重置任何标题

元缓存控制标记允许Web发布者定义缓存应如何处理页面。它们包括用于声明哪些内容应可缓存、哪些内容可由缓存存储、过期机制的修改以及重新验证和更新的指令 重新加载控件

允许的值为:

公共-可以缓存在公共共享缓存中
专用-只能缓存在专用缓存中
无缓存-可能无法缓存
无存储-可以缓存但不能存档

请注意区分大小写。在网页源中添加以下元标记。标记末尾的拼写差异是使用“/>=xml或“>=html

    <meta http-equiv="Cache-control" content="public">
    <meta http-equiv="Cache-control" content="private">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache-control" content="no-store">


来源->OWASP建议如下:

尽可能确保缓存控制HTTP头设置为无缓存、无存储,必须重新验证、私有;并且pragma HTTP头被设置为没有缓存

<IfModule mod_headers.c>
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</IfModule>

标题集缓存控制“专用、无缓存、无存储、代理重新验证、无转换”
标题集Pragma“无缓存”

这是我在实际网站中使用过的最好的
.htaccess

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

##Tweaks##
Header set X-Frame-Options SAMEORIGIN

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_headers.c>
    Header set Connection keep-alive
    <filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
        Header set Cache-Control "max-age=2592000, public"
    </filesmatch>
    <filesmatch "\.(jpg|jpeg|png)$">
        Header set Cache-Control "max-age=1209600, public"
    </filesmatch>
    # css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
    <filesmatch "\.(css)$">
        Header set Cache-Control "max-age=31536000, private"
    </filesmatch>
    <filesmatch "\.(js)$">
        Header set Cache-Control "max-age=1209600, private"
    </filesmatch>
    <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
      </filesMatch>
</IfModule>

mod_gzip_on Yes
是的
mod|gzip|item|u include file.(html?| txt | css | js | php | pl)$
mod_gzip_item_包含处理程序^cgi脚本$
mod_gzip_项目包括mime^text/*
mod_gzip_item_包括mime^application/x-javascript*
mod_gzip_项_排除mime^image/*
mod_gzip_item_排除rspheader^内容编码:.*gzip*
##调整##
标题集X-Frame-Options SAMEORIGIN
##过期缓存##
过期于
过期按类型映像/jpg“访问1年”
过期按类型图像/jpeg“访问1年”
ExpiresByType image/gif“访问1年”
过期按类型图像/png“访问1年”
ExpiresByType文本/css“访问1个月”
ExpiresByType text/html“访问1个月”
过期按类型应用程序/pdf“访问1个月”
ExpiresByType文本/x-javascript“访问1个月”
过期按类型应用程序/x-shockwave-flash“访问1个月”
过期按类型图像/x图标“访问1年”
ExpiresDefault“访问1个月”
##过期缓存##
标题集连接保持活动状态
标题集缓存控制“最大年龄=2592000,公共”
标题集缓存控制“最大年龄=1209600,公共”
#css和js应该使用私有代理缓存https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
标题集缓存控制“最大年龄=31536000,专用”
标头集缓存控制“最大年龄=1209600,专用”
标题集缓存控制“最大年龄=600,专用,必须重新验证”

您使用的服务器端语言是什么?PHP?,ASP?,JSP?设置标题的方式类似,但不完全相同。或者,如果您正在缓存图像。。。这通常是在Apache(或web服务器)中完成的,因为“解决方案”只会生成无效的DOCTYPE;-)如果您希望从HTML执行此操作(我不建议这样做),请使用更正:不应缓存任何存储,不允许缓存任何缓存,但必须在保留之前与服务器进行检查-请参阅缓存控制不存储-没有存储类似于没有缓存,因为响应无法缓存和重复使用,然而,有一个重要的区别。没有存储要求每次都从源服务器请求和下载资源。在处理私人信息时,这是一个重要的特性。这样的顺序重要吗?“max age=290304000,public”或“public,max age=290304000”或两者都是正确的?如果值不冲突(如
缓存
和<
<IfModule mod_headers.c>
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</IfModule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

##Tweaks##
Header set X-Frame-Options SAMEORIGIN

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_headers.c>
    Header set Connection keep-alive
    <filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
        Header set Cache-Control "max-age=2592000, public"
    </filesmatch>
    <filesmatch "\.(jpg|jpeg|png)$">
        Header set Cache-Control "max-age=1209600, public"
    </filesmatch>
    # css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
    <filesmatch "\.(css)$">
        Header set Cache-Control "max-age=31536000, private"
    </filesmatch>
    <filesmatch "\.(js)$">
        Header set Cache-Control "max-age=1209600, private"
    </filesmatch>
    <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
      </filesMatch>
</IfModule>