Nginx+Squid+Apche 负载均衡配置示例

最近研究nginx在负载均衡上的配置,下步网站的整体架构有可能要适当的调整下,使用nginx在最前端做负载均衡,中间放多个squid缓存节点,为最后端的图片服务器提供高速缓存,从而大大提高网站打开的响应速度;下面介绍下在kvm环境下的大致部署步骤和配置,负载均衡器方面也可以使用lvs来替代nginx,拓扑图大致如下:

 


nginx的负载均衡的详细介绍可以参考这篇文章:http://www.zzbaike.com/wiki/Nginx%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1
Nginx的 upstream目前支持5种方式的分配,nginx支持同时设置多组的负载均衡,用来给不用的server来使用;
1:down 表示单前的server暂时不参与负载
2:weight 默认为1.weight越大,负载的权重就越大;
3:max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4:fail_timeout:max_fails次失败后,暂停的时间;
5:backup:其它所有的非backup机器down或者忙的时候,请求backup机器,所以这台机器压力会最轻; 

一:配置负载均衡器nginx
root@server5 ~]# grep -v '^#' /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 1;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid        /usr/local/nginx/nginx.pid;

events
{
use epoll;
worker_connections 65535;
}
http
{     
include       mime.types;
default_type application/octet-stream;

keepalive_timeout 120;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;


upstream pic.bleachyang.com {
server   192.168.122.10:80 backup;    //这里采用backup方式分配负载
server   192.168.122.20:80;
}


server
{
listen 80;
server_name pic.bleachyang.com;

location / {
proxy_pass        http://pic.bleachyang.com;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}

log_format pic.bleachyang.com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/pic.bleachyang.log;
}
}


二,配置反向代理squid缓存加速服务器,两台的配置一致即可
[root@server1 ~]# groupadd squid
[root@server1 ~]# useradd -g squid -s /sbin/nologin squid
[root@server1 ~]# cd /usr/local/src/tarbag/
[root@server1 tarbag]# wget ftp://ftp.dti.ad.jp/pub/unix/net/proxy/squid/squid-3.1.2.tar.gz
[root@server1 tarbag]# tar -zxvf squid-3.1.2.tar.gz -C ../software/
[root@server1 tarbag]# cd ../software/squid-3.1.2/
[root@server1 squid-3.1.2]# ./configure --prefix=/usr/local/squid --enable-gnuregex --enable-arp-acl --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-async-io=80 --enable-storeio=ufs --enable-icmp --enable-kill-parent-hack --enable-snmp --disable-ident-lookups --enable-cache-digests --enable-ssl --enable-delay-pools --enable-poll --enable-linux-netfilter --enable-underscore --enable-err-language="Simplify_Chinese" --enable-default-err-languages="Simplify_Chinese"

编译选项注解:
--prefix=/usr/local/squid    //指定安装路径
--enable-arp-acl             //这样可以在规则设置中直接通过客户端的MAC地址进行管理,防止客户使用IP欺骗
--enable-async-io=80         //这个主要是设置async模式来运行squid,如果服务器配置很不错,有1G以上内存,cpu使用SMP的方式的话可以考虑设成160或者更高,如果服务器比较糟糕就根据实际情况设了,另外此项还使cache文件支持aufs
--enable-auth-modules        //此编译选项启用认证模块,可以对访问代理用户进行授权;
--enable-cache-digests       //使能缓存摘要,本来此项目的是为了在Squid集群服务之间迅速发现缓存对象,这里在本地使用,可以加快请求时,检索缓存内容的速度;
--enable-err-language="Simplify_Chinese" 和--enable-default-err-languages="Simplify_Chinese" //指定出错是显示的错误页面为简体中文
--enable-delay-pools           //此选项使能一个延时池,这样能对某些特定的请求限制额定带宽。
--enable-gnuregex              //由于Squid大量使用字符串处理做各种判断,加此项能更好处理。
--enable-icmp                  //加入icmp支持
--disable-ident-lookups        //防止系统使用RFC931规定的身份识别方法。
--enable-kill-parent-hack      //关掉suqid的时候,连同父进程一起关掉
--enable-linux-netfilter       //允许使用Linux的透明代理功能。
--enable-poll                  //应启用Poll()函数而不是select()函数,通常而言poll(轮询)比select要好,但configure(脚本程序)已知Poll在某些平台下失效, 若你认为你比configure编译配置脚本程序要聪明的话,可以用这个选项启用Poll。总之就是用这个可以提升性能
--enable-snmp                 //此选项可以让MRTG使用SNMP协议对服务器的流量状态进行监测;
--enable-storeio=ufs,null     //使用的文件系统通常是默认的ufs,不过如果想要做一个不缓存任何文件的代理服务器,就需要加上null文件系统;
--enable-underscore           //允许解析的URL中出现下划线,因为默认squid会认为带下划线的URL地址是非法的,并拒绝访问该地址;

[root@server1 squid-3.1.2]# make && make install
[root@server1 squid-3.1.2]# ls /usr/local/squid/
bin etc include lib libexec sbin share var
[root@server1 ~]# chown -R squid.squid /usr/local/squid/
[root@server1 ~]# /usr/local/squid/sbin/squid -z
2010/05/06 13:05:40| Creating Swap Directories

[root@server1 ~]# grep -v '^#' /usr/local/squid/etc/squid.conf   //squid反向代理的关键配置
cache_effective_user squid
cache_effective_group squid
http_access allow all
http_port 80 accel vhost vport 
coredump_dir /usr/local/squid/var/cache
cache_peer pic.bleachyang.com parent 80 0 no-query originserver name=pic
cache_peer_domain pic   pic.bleachyang.com
……………………………………其他信息省略……………………………………………………

[root@server1 ~]# /usr/local/squid/sbin/squid    //启动squid
[root@server1 ~]# netstat -ntpl |grep squid
tcp        0      0 :::80                       :::*                        LISTEN      384/(squid) 

[root@server1 ~]# grep pic /etc/hosts           //需要在反向代理的hosts文件上加上后台服务器的IP
192.168.122.1   pic.bleachyang.com

日志信息:
[root@server5 ~]# tail -f /usr/local/nginx/logs/pic.bleachyang.log
192.168.122.1 - - [06/May/2010:17:01:41 +0800] "GET /1.jpg HTTP/1.1" 304 0 "http://pic.bleachyang.com/" "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.12) Gecko/2009070811 Red Hat/3.0.12-1.el5_3 Firefox/3.0.12"

[root@server1 ~]# tail -f /usr/local/squid/var/logs/access.log 
1273135928.937      4 192.168.122.1 TCP_MISS/304 284 GET http://pic.bleachyang.com/1.jpg - FIRST_UP_PARENT/pic -

[root@server2 logs]# tail -f /usr/local/squid/var/logs/access.log 
1273136458.753    129 192.168.122.1 TCP_MEM_HIT/200 1299980 GET http://pic.bleachyang.com/1.jpg - NONE/- image/jpeg
1273136459.871      7 192.168.122.1 TCP_MISS/404 598 GET http://pic.bleachyang.com/favicon.ico - FIRST_UP_PARENT/pic text/html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值