用Nginx给Joomla!提速
Joomla!是个不错的基于PHP的CMS,在LAMP环境下安装运行都非常方便,不过性能并非最优。一个提速的方法是用Nginx服务静态内容。
Nginx的配置片段:
upstream apache1{
server 127.0.0.1:8001;
}server{
listen 80;
server_name mysite.com.cn www.mysite.com.cn;location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /home/raymond/public_html/www.mysite.com.cn;
expires 14d;gzip on;
gzip_comp_level 3;
gzip_types text/plain text/javascript text/html text/css image/png application/json application/x-javascript text/xml application/xml+rss;
gzip_vary on;
gzip_buffers 16 8k;
}location / {
proxy_pass http://apache1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
apache2的配置片段
<VirtualHost 127.0.0.1:8001>
ServerName www.mysite.com.cn
DocumentRoot /home/raymond/public_html/www.mysite.com.cn
</VirtualHost>
记得把apache2主配置文件中的Listen 80 改为Listen 8001。如果有多个虚拟主机,还要加上Listen 8002……
别把Webserver累坏了
天气又热起来了,想着服务器在机房里累的呼呼冒着热气,我多少有些于心不忍,于是想办法帮它减负吧。
首先是网页模板方面:
- 尽量减少修饰目的的<img/>,改用css来修饰模板,减小传输流量和request数目
- 合并CSS和JS,就是把所有用到的CSS写到一个文件,JS写到一个文件,减少request数目
- HTML CSS JS文件的minify,就是去除文本中多余的空格、换行、注释等等
然后是HTTP Server方面:
- 静动分离,例如用nginx服务静态文件,而apache2服务动态内容(php,……)
- 利用浏览器端的cache(Apache2: mod_expires; Nginx: expires),降低重复访问时的流量。
- 对文本或非压缩格式文件开启gzip压缩
- 关闭access log或者将log写到另外的服务器
最后是webapp方面:
- 很多CMS都有内建的cache机制,尽量开启,减少DB操作
- 尽量减少安装组件或插件,特别是访问统计这类,DB开销很大
好了,简单总结一下,抛砖引玉吧。
附:Apache 2.0 开启gzip压缩和expire的例子
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|pdf)$ no-gzip dont-varyExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresActive On


