Linux Apache设置压缩及缓存
压缩文件:
编辑apache模块
vim /etc/httpd/conf.modules.d/00-base.conf
确保以下三个模块没有被注释
loadmodule headers_module modules/mod_headers.so
loadmodule deflate_module modules/mod_deflate.so
loadmodule filter_module modules/mod_filter.so
注意:也可以把三个模块放到/etc/httpd/conf/httpd.conf文件里,模块只能在一个文件中加载
设置压缩规则
vim /etc/httpd/conf/httpd.conf
末尾添加
<ifmodule mod_deflate.c>
addoutputfilterbytype deflate application/javascript
addoutputfilterbytype deflate application/rss+xml
addoutputfilterbytype deflate application/vnd.ms-fontobject
addoutputfilterbytype deflate application/x-font
addoutputfilterbytype deflate application/x-font-opentype
addoutputfilterbytype deflate application/x-font-otf
addoutputfilterbytype deflate application/x-font-truetype
addoutputfilterbytype deflate application/x-font-ttf
addoutputfilterbytype deflate application/x-javascript
addoutputfilterbytype deflate application/xhtml+xml
addoutputfilterbytype deflate application/xml
addoutputfilterbytype deflate application/x-httpd-php
addoutputfilterbytype deflate application/x-httpd-fastphp
addoutputfilterbytype deflate font/opentype
addoutputfilterbytype deflate font/otf
addoutputfilterbytype deflate font/ttf
addoutputfilterbytype deflate image/svg+xml
addoutputfilterbytype deflate image/x-icon
addoutputfilterbytype deflate text/css
addoutputfilterbytype deflate text/html
addoutputfilterbytype deflate text/javascript
addoutputfilterbytype deflate text/plain
addoutputfilterbytype deflate text/xml
# remove browser bugs (only needed for really old browsers)
browsermatch ^mozilla/4 gzip-only-text/html
browsermatch ^mozilla/4\.0[678] no-gzip
browsermatch \bmsie !no-gzip !gzip-only-text/html
#setenvifnocase request_uri .(?:gif|jpe?g|png)$ no-gzip dont-vary #设置不对后缀gif,jpg,jpeg,png的图片文件进行压缩
header append vary user-agent
</ifmodule>
压缩率一般都大于70%
压缩率检测(http://tool.chinaz.com/gzips/)只能在pc端检测
设置缓存:
vim /etc/httpd/conf.modules.d/00-base.conf
确保以下模块没有被注释
loadmodule headers_module modules/mod_headers.so #第一种缓存方式
loadmodule expires_module modules/mod_expires.so #第二种缓存方式
注意:也可以把这个模块放到/etc/httpd/conf/httpd.conf文件里,模块只能在一个文件中加载
设置缓存规则
vim /var/www/html/.htaccess
末尾添加
第一种缓存方式
fileetag inode mtime size
第二种缓存方式
# 缓存有效时间 1 月
<filesmatch "\.(ico|jpg|jpeg|png|gif|js|css)$">
header set cache-control "max-age=2592000, public"
</filesmatch>
<filesmatch "\.(html|txt|htm|php)$">
header set cache-control "max-age=2592000, public, must-revalidate"
</filesmatch>
注意:max-age为缓存时间,单位秒,按需修改
第三种缓存方式
vim /etc/httpd/conf/httpd.conf
添加以下内容
<ifmodule expires_module>
expiresactive on
#css文件缓存2592000/3600/24=1月
expiresbytype text/css a2592000
#js文件缓存2592000/3600/24=1月
expiresbytype application/x-javascript a2592000
expiresbytype application/javascript a2592000
#html文件缓存2592000/3600/24=1月
expiresbytype text/html a2592000
#图片文件缓存2592000/3600/24=1月
expiresbytype image/jpg a2592000
expiresbytype image/jpeg a2592000
expiresbytype image/gif a2592000
expiresbytype image/png a2592000
expiresbytype image/ico a2592000
expiresbytype image/x-icon a2592000
#文件默认缓存1月
expiresdefault "access plus 30 days"
</ifmodule>
注意:以上方式,三选一
重启apache服务
service httpd restart
注意:apache设置压缩和缓存都会增加服务器的内存压力,但设置压缩和缓存之后,网站的访问速度会有所提高