首页 > 网站 > Apache > 正文

Linux Apache设置压缩及缓存

2024-08-27 18:27:10
字体:
来源:转载
供稿:网友

压缩文件:

编辑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

末尾添加

第一种缓存方式

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表