您好,欢迎访问三七文档
中小企业web集群方案haproxy+varnish+LNMP+memcached配置2012-03-0118:02:35标签:memcachedvarnishhaproxy原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。前段时间帮一个中型企业做了一个集群方案配置,目前已经安全运行了一个月,而且速度非常快,稳定性也很强,作为一般中型企业负载均衡的方案,非常的合适。但是由于资本有限,没有做到高可用性方案。一,拓扑图如下:二,架构说明及配置1,haproxy服务器:192.168.1.52,varnish服务器集群:192.168.1.20;192.168.1.213,nginx+fastcgi服务器:192.168.1.504,memcached服务器:memcached我是装在nginx+fastcgi服务器上的,192.168.1.505,mysql服务器:192.168.1.60三,具体配置以下我们来一个个讲解,我们先从后端来个个讲解,1,在192.168.1.60服务器上安装mysql2,在192.168.1.50服务器上安装nginx+fastcgi.mysql+nginx+fastcgi配置,这里不讲解了,网上包括我的博客里面都有讲解如何时安装。3,memcached安装配置。这里的memcached我安装在192.168.1.50服务器上安装步骤如下:(1)下载所需要的软件(memcached目前最新版本为1.4.13)安装memcached需要libevent库支持,我们下载libevent1.3版本即可1.#wget://~provos/libevent-1.3.tar.gz(2)先安装libevent:1.#tarzxvflibevent-1.3.tar.gz2.#cdlibevent-1.33.#./configure--prefix=/usr4.#make5.#makeinstall(3)安装memcached,同时需要安装中指定libevent的安装位置:1.#cd/tmp2.#tarzxvfmemcached-1.4.13.tar.gz3.#cdmemcached-1.4.134.#./configure--with-libevent=/usr5.#make6.#makeinstall安装完成后会把memcached放到/usr/local/bin/memcached(4)fastcgi安装memcache扩展模块,让php支持memcached1.在选择相应想要下载的memcache版本。2.安装PHP的memcache扩展1.#tarvxzfmemcache-2.2.1.tgz2.#cdmemcache-2.2.13.#/usr/local/webserver/php/bin/phpize4.#./configure--enable-memcache--with-php-config=/usr/local/webserver/php/bin/php-config--with-zlib-dir5.#make6.#makeinstall3.上述安装完后会有类似这样的提示:1.Installingsharedextensions:/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/4,在php.ini添加一行来载入memcache扩展:1.#extension=memcache.so如下图5,重新加载php配置文件启动memcached1.#/usr/local/bin/memcached-d-m1024-uroot-l127.0.0.1-p11211-c10240-P/tmp/memcached.pid我们来看一下是否正常启动端口已正常启动。1.memcache启动参数备注:2.-d选项是启动一个守护进程,3.-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,4.-u是运行Memcache的用户,我这里是root,5.-l是监听的服务器IP地址,为了安全我这里指定了127.0.0.1,6.-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,7.-c选项是最大运行的并发连接数,默认是1024,我这里设置10240,按照你服务器的负载量来设定8.-P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,9.Memcached安装完毕。我们来测试一下是否能缓存mysql数据我们在此服务器上架设一个php网站,此处我先用的是占用资源比较大的magento程序网站,调试好配置文件,绑定一下host文件。看网站是否能正常打开。好,网站打开正常,接下来我们看一下,memcached是否发挥了他的作用这里我们用一个软件memadmin(网上可以下载到)来监控一下memcached的缓存命中率,如下图:命中率还是挺高的。4,varnish服务器安装以下在192.168.1.20和192.168.1.21上操作下载varnish软件(此处下载3.0版本)1.#wget=/usr/local/varnish5.#make&&makeinstall至此安装完成。我们来配置一下1.#vim/usr/local/varnish/etc/varnish/vcl.conf以下为配置代码:(除backend及vcl_recv外,其他地方无需更改,这里是更改其访问的主机名,及定义后端服务器地址,字数有限,其他代码这里就不解释了)1.#Cacheforlinuxtonesites2.#backendvhost3.backend{4..host==80;6.}7.#acl8.aclpurge{9.localhost;10.127.0.0.1;11.192.168.0.0/24;12.}13.subvcl_recv{14.if(req.http.Accept-Encoding){15.if(req.url~\.(jpg|png|gif|jpeg|flv)$){16.removereq.http.Accept-Encoding;17.removereq.http.Cookie;18.}elseif(req.http.Accept-Encoding~gzip){19.setreq.http.Accept-Encoding=gzip;20.}elseif(req.http.Accept-Encoding~deflate){21.setreq.http.Accept-Encoding=deflate;22.}else{23.removereq.http.Accept-Encoding;24.}25.}26.if(req.http.host~(.*)yaozhibingceshi.com){27.setreq.backend=}29.else{30.error404Thiswebsiteismaintainingornotexist!;31.}32.if(req.request==PURGE){33.if(!client.ip~purge){34.error405NotAllowed;35.}36.#.dd.....37.return(lookup);38.}39.#...GET...url...jpg,png,gif...cookie40.if(req.request==GET&&req.url~\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm|gz|tgz|bz2|tbz|mp3|ogg|mp4|flv|f4v|pdf)$){41.unsetreq.http.cookie;42.}43.#..GET...url.php....cache....44.if(req.request==GET&&req.url~\.php($|\?)){45.return(pass);46.}47.#if(req.restarts==0){48.if(req.http.x-forwarded-for){49.setreq.http.X-Forwarded-For=50.req.http.X-Forwarded-For+,+client.ip;51.}else{52.setreq.http.X-Forwarded-For=client.ip;53.}54.#}55.#........pipe..56.if(req.request!=GET&&57.req.request!=HEAD&&58.req.request!=PUT&&59.req.request!=POST&&60.req.request!=TRACE&&61.req.request!=OPTIONS&&62.req.request!=DELETE){63.return(pipe);64.}65.#..GET.HEAD.....66.if(req.request!=GET&&req.request!=HEAD){67.return(pass);68.}69.if(req.http.Authorization){70.return(pass);71.}72.return(lookup);73.}74.#..url+hosthash......75.subvcl_hash{76.hash_data(req.url);77.if(req.http.host){78.hash_data(req.http.host);79.}else{80.hash_data(server.ip);81.}82.return(hash);83.}84.#.....purge.....85.subvcl_hit{86.if(req.request==PURGE){87.setobj.ttl=0s;88.error200Purged;89.}90.return(deliver);91.}92.subvcl_fetch{93.if(req.url~\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$){94.setberesp.ttl=2d;95.setberesp.http.expires=beresp.ttl;96.setberesp.http.Cache-Control=max-age=172800;97.unsetberesp.http.set-cookie;98.}99.if(req.url~\.(dmg|js|css|html|htm)$){100.setberesp.do_gzip=true;101.}102.if(beresp.status==503){103.setberesp.saintmode=15s;104.}105.}106.subvcl_deliver{107.setresp.http.x-hits=obj
本文标题:中小企业web集群方案 haproxy+varnish+LNMP+memcached配置
链接地址:https://www.777doc.com/doc-6346663 .html