前段时间帮一个中型企业做了一个集群方案配置,目前已经安全运行了一个月,而且速度非常快,稳定性也很强,作为一般中型企业负载均衡的方案,非常的合适。但是由于资本有限,没有做到高可用性方案。
一,拓扑图如下:
- #wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
- #wget http://www.monkey.org/~provos/libevent-1.3.tar.gz
- # tar zxvf libevent-1.3.tar.gz
- # cd libevent-1.3
- # ./configure --prefix=/usr
- # make
- # make install
- # cd /tmp
- # tar zxvf memcached-1.4.13.tar.gz
- # cd memcached-1.4.13
- # ./configure --with-libevent=/usr
- # make
- # make install
- #tar vxzf memcache-2.2.1.tgz
- #cd memcache-2.2.1
- #/usr/local/webserver/php/bin/phpize
- #./configure--enable-memcache --with-php-config=/usr/local/webserver/php/bin/php-config --with-zlib-dir
- #make
- #make install
- Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
- #extension = "memcache.so"
5,重新加载php 配置文件
- # /usr/local/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 10240 -P /tmp/memcached.pid
- memcache启动参数备注:
- -d选项是启动一个守护进程,
- -m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
- -u是运行Memcache的用户,我这里是root,
- -l是监听的服务器IP地址,为了安全我这里指定了 127.0.0.1,
- -p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
- -c选项是最大运行的并发连接数,默认是1024,我这里设置10240,按照你服务器的负载量来设定
- -P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
- #wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
- #tar -xvf varnish-3.0.0.tar.gz
- #cd varnish-3.0.0
- #./configure --prefix=/usr/local/varnish
- #make &&make install
- #vim /usr/local/varnish/etc/varnish/vcl.conf
- #Cache for linuxtone sites
- #backend vhost
- backend wwwyaozhibingceshicom {
- .host = "www.yaozhibingceshi.com";
- .port = "80";
- }
- #acl
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.0.0"/24;
- }
- sub vcl_recv {
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {
- remove req.http.Accept-Encoding;
- remove req.http.Cookie;
- } else if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- remove req.http.Accept-Encoding;
- }
- }
- if (req.http.host ~ "(.*)yaozhibingceshi.com") {
- set req.backend = wwwyaozhibingceshicom;
- }
- else {
- error 404 "This website is maintaining or not exist!";
- }
- if (req.request == "PURGE") {
- if (!client.ip ~purge) {
- error 405 "Not Allowed";
- }
- #.dd.....
- return(lookup);
- }
- #...GET...url...jpg,png,gif. ..cookie
- 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)$") {
- unset req.http.cookie;
- }
- #..GET...url.php....cache....
- if (req.request =="GET"&&req.url ~ "\.php($|\?)"){
- return (pass);
- }
- # if (req.restarts == 0) {
- if (req.http.x-forwarded-for) {
- set req.http.X-Forwarded-For =
- req.http.X-Forwarded-For + ", " + client.ip;
- } else {
- set req.http.X-Forwarded-For = client.ip;
- }
- # }
- #........pipe..
- if (req.request != "GET" &&
- req.request != "HEAD" &&
- req.request != "PUT" &&
- req.request != "POST" &&
- req.request != "TRACE" &&
- req.request != "OPTIONS" &&
- req.request != "DELETE") {
- return (pipe);
- }
- #..GET .HEAD.....
- if (req.request != "GET" && req.request != "HEAD") {
- return (pass);
- }
- if (req.http.Authorization) {
- return (pass);
- }
- return (lookup);
- }
- #..url+host hash......
- sub vcl_hash {
- hash_data(req.url);
- if (req.http.host) {
- hash_data(req.http.host);
- } else {
- hash_data(server.ip);
- }
- return (hash);
- }
- # .....purge .....
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 200 "Purged";
- }
- return (deliver);
- }
- sub vcl_fetch {
- if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {
- set beresp.ttl = 2d;
- set beresp.http.expires = beresp.ttl;
- set beresp.http.Cache-Control = "max-age=172800";
- unset beresp.http.set-cookie;
- }
- if (req.url ~ "\.(dmg|js|css|html|htm)$") {
- set beresp.do_gzip = true;
- }
- if (beresp.status == 503) {
- set beresp.saintmode = 15s;
- }
- }
- sub vcl_deliver {
- set resp.http.x-hits = obj.hits ;
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT You!";
- } else {
- set resp.http.X-Cache = "MISS Me!";
- }
- }
- wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
- #tar -xvf haproxy-1.4.19.tar.gz
- #cd haproxy-1.4.19
- #make TARGET=linux26 PREFIX=/usr/local/haproxy
- #make install PREFIX=/usr/local/haproxy
- # cd /usr/local/haproxy
- # vim haproxy.conf
- global
- maxconn 5120
- chroot /usr/local/haproxy
- uid 99
- gid 99
- daemon
- quiet
- nbproc 2
- pidfile /usr/local/haproxy/haproxy.pid
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- log 127.0.0.1 local3
- retries 3
- option redispatch
- maxconn 20000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen www.yaozhibingceshi.com 0.0.0.0:80
- mode http
- stats uri /status (后端服务器状态查看地址)
- stats realm Haproxy\ statistics
- stats auth admin:admin (状态查看页面登陆帐号密码)
- balance source (调度算法,调度算法有很多,我这里用source是和nginx的ip_hash同理,解决session问题)
- option httpclose
- option forwardfor
- server app1_1 192.168.1.20:80 cookie app1inst1 check inter 2000 rise 2 fall 5
- server app1_2 192.168.1.21:80 cookie app1inst2 check inter 2000 rise 2 fall 5