[英]Enable Gzip compression and set Cache expire times in CakePHP
我正在使用CakePHP和Apache2。 使用Google的PageSpeed工具分析我的网页,我发现了两个主要问题。 第一个是我应该启用gzipping内容,第二个我应该利用浏览器缓存图像,javascript和css文件。
我或多或少找到了如何解决这个问题,但它涉及更改我的主.htaccess文件。 这看起来并不像CakePHP,如果没有,因为.htaccess是CakePHP发行版的一部分,我必须记住在更改版本时保留它。 有一个更好的方法吗?
首先,我可以通过推杆补救
php_value output_buffering On
php_value output_handler ob_gzhandler
我也试过了
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/x-javascript
</IfModule>
但我没有看到PageSpeed的任何结果。
对于我现在使用的第二个
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/x-javascript A8640000
ExpiresByType text/javascript A8640000
ExpiresByType text/css A8640000
ExpiresByType image/png A8640000
</IfModule>
它有点工作,虽然我仍然收到消息“以下可缓存资源的生命周期很短。为以下资源指定未来至少一个月的到期时间:blah blah”
在GZIP压缩和浏览器缓存方面没有“CakePHP方式”。 这肯定更像是Apache配置问题。 您应该可以修改/app
文件夹中的.htaccess
文件而不受惩罚。
更新/升级CakePHP时,您只需要更改/cake
文件夹中的文件。 您不必在/app
文件夹的.htaccess
文件中重新启用任何服务器配置,因为那里的所有内容都将保持不变。
永远记住: /app
文件夹中的任何内容都是合理的游戏。
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 30 days"
</IfModule>
要在ubuntu中启动mod_expires模块,请运行以下命令
sudo a2enmod headers
sudo a2enmod expires
sudo service apache2 restart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.