技术开发 频道

高负载WEB动态页面与静态页面的分离

【IT168 技术文档】

    试了一段时间的Lighttpd,表现的确不错,原先用APACHE只能跑到6K/S,改用Lighttpd后性能提升了将近一倍,只是跑PHP似乎很不稳定,数据库处理时间长一点就会到导致Lighttpd出现500的错误,也就是说要想使Lighttpd发挥更好的性能,除了优化程序还是优化程序,尽可能的加快PHP的执行速度,另外加上eAccelerator性能会有更大的提高。

    说了这么多优点,再来说说公司最近所尝试的一些经验,PHP程序已被优化的不能再优化了,但是有时候操作一下数据库(比如执行mysqldump)就会出现500错误,并发量非常大的时候很容易出现,在这点上Lighttpd的表现就没有Apache稳定了,所以想了一个折中的办法,让Apache来处理PHP程序,其他的静态页面(包括图片、软件等等)全都交给Lighttpd来处理。而实现方法就是通过mod_proxy来实现,Apache做前端处理或Lighttpd做前端处理都行,反正这2种都有个mod_proxy模块,基本上一样。


下面就以CentOS 4.4系统下使用Apache(80端口)和Lighttpd(81端口)实现动态页面和静态页面的分离。

Apache采用系统默认安装(yum install apache)的即可,网上的安装文档也很多,偶就不再介绍了。

安装Lighttpd v1.4.13


# yum install pcre-devel
# cd /usr/local/src
# wget http://www.lighttpd.net/download/lighttpd-1.4.13.tar.gz
# tar -zxvf lighttpd-1.4.13.tar.gz
# cd lighttpd-1.4.13
# ./configure --with-pcre
# make
# make install
# cd doc
# cp sysconfig.lighttpd /etc/sysconfig/lighttpd
# cp rc.lighttpd.redhat /etc/init.d/lighttpd
# chkconfig lighttpd on
# mkdir -p /etc/lighttpd
# cp lighttpd.conf /etc/lighttpd/
# mkdir /var/log/lighttpd
# touch /var/log/lighttpd/access.log
# touch /var/log/lighttpd/error.log
# chown -R apache:apache /var/log/lighttpd# vi /etc/init.d/lighttpd将lighttpd=”/usr/sbin/lighttpd”改为lighttpd=”/usr/local/sbin/lighttpd”

编辑Lighttpd的配置文件lighttpd.conf,大致修改如下:

server.modules = ("mod_access", "mod_simple_vhost", "mod_accesslog" )
server.document-root = "/var/www/html"
server.errorlog = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"
server.port = 81
server.bind = “localhost”
simple-vhost.server-root = “/var/www/html/”
simple-vhost.default-host = “”
simple-vhost.document-root = “/”
server.username = “apache”
server.groupname = “apache“LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
 
<VirtualHost *:80>
ServerAdmin <a href="mailto:webmaster@sofee.cn">webmaster@sofee.cn</a>
DocumentRoot /var/www/html/test.sofee.cn
ServerName test.sofee.cn
<IfModule mod_rewrite.c>
RewriteEngine On
#        RewriteLog logs/rewrite_log
#        RewriteLogLevel 1
RewriteRule "^/((.*).(js|css|htm|html|swf|gif|jpg|png|ico|exe|zip|rar))$" "http://0.0.0.0:81/$1" [P,QSA,L]
RewriteRule "^/(.*)" "$0" [L]
</IfModule>
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On
ProxyReceiveBufferSize 16384
ProxyTimeout 300
ProxyPassReverse / http://0.0.0.0:81/
</IfModule>
</VirtualHost>

0
相关文章