Apache开启 gzip 压缩方法:
这里我使用的是Apache2.4.17
打开apache安装目录,找到conf目录,用记事本打开httpd.conf 文件。
ctrl+f 查找
去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释 # 去掉 #LoadModule deflate_module modules/mod_deflate.so 前面的注释 # 去掉 #LoadModule filter_module modules/mod_filter.so 前面的注释 #
文件末尾加上
<IfModule deflate_module> SetOutputFilter DEFLATE DeflateCompressionLevel 6 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript //需要压缩的格式 AddOutputFilter DEFLATE css js txt xml rss html htm //需要压缩的格式 Header append Vary User-Agent env=!dont-vary SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|bmp|tif)
然后看客户端的请求里是否有 :
Accept-Encoding: gzip, deflate //代表客户端支持gzip
服务端的响应里是否有 :
Content-Encoding: gzip //代表服务端已开启gzip
有些服务器对内容进行 gzip 编码只针对某些文件,所以 有没有返回 这个,并不能代表,是否支持gzip的依据。
Apache配置 Expire/Cache-Control 头
打开apache安装目录,找到conf目录,用记事本打开httpd.conf 文件。
ctrl+f 查找 LoadModule expires_module modules/mod_expires.so 去掉前面 #号!
在文本最后面添加: <IfModule expires_module> #打开缓存 ExpiresActive on #css文件缓存7200000/3600/24=83天 ExpiresByType text/css A7200000
#js文件缓存83天 ExpiresByType application/x-javascript A7200000 ExpiresByType application/javascript A7200000
#html文件缓存83天 ExpiresByType text/html A7200000
#图片文件缓存83天 ExpiresByType image/jpeg A7200000 ExpiresByType image/gif A7200000 ExpiresByType image/png A7200000 ExpiresByType image/x-icon A7200000 </IfModule>
上面开启的是expire
下面是cache-control 在文本后面继续添加 <FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch>
<FilesMatch "\.(css|js)$"> Header set Cache-Control "max-age=604800, public" </FilesMatch>
这里时间设置不一样是为了检验是否成功配置的,因为,没设cache-control的时候,它会自动根据expire的时间设置自己。
最后重启apache服务器,ok!
这里说一个自己爬的坑吧!(那就是如果不出现 200 form cache, 而是出现了304,那是因为,你刷新了浏览器。。。想要出现 200 form cache,需要在浏览器地址栏里按回车键。)
也就是说:刷新浏览器 触发 304, 地址栏回车触发 200 form cache。
查了好久才发现了,之前一直想不通,既然设置了cache-control和expire为啥还是304,这就是原因,就是这么简单。