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

本文详细介绍了如何利用Nginx和Squid在KVM环境下构建高效的网站负载均衡与缓存系统。通过配置Nginx作为负载均衡器,将请求分发到多台Squid缓存节点,实现图片服务器的高速缓存,显著提升网站响应速度。文中提供了从部署到配置的完整步骤,包括Nginx的负载均衡策略、Squid缓存加速服务器的搭建与配置,以及关键参数的解释,旨在帮助开发者优化网站性能。

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

图片

 

nginx的负载均衡的详细介绍可以参考这篇文章:http://www.linuxidc.com/Linux/2010-05/25987p3.htm

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.linuxidc.com  {
server   192.168.122.10:80 backup;    //这里采用backup方式分配负载
server   192.168.122.20:80;
}


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

location / {
proxy_pass        http://pic.linuxidc.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.linuxidc.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,我的理解是设置用线程来运行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.linuxidc.com  parent 80 0 no-query originserver name=pic
cache_peer_domain pic   pic.bleachyang.co
……………………………………其他信息省略……………………………………………………

[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.linuxidc.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.linuxidc.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.linuxidc.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.linuxidc.com/1.jpg - NONE/- image/jpeg
1273136459.871      7 192.168.122.1 TCP_MISS/404 598 GET http://pic.linuxidc.com/favicon.ico - FIRST_UP_PARENT/pic

text/html

 

Nginx负载均衡基础知识
Nginx的upstream目前支持5种方式的分配

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2)、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

2)、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

3)、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4)、url_hash(第三方)

Nginx 负载均衡实例1
upstream bbs.linuxidc.net {#定义负载均衡设备的Ip及设备状态
   server 127.0.0.1:9090 down;
   server 127.0.0.1:8080 weight=2;
   server 127.0.0.1:6060;
   server 127.0.0.1:7070 backup;
}

在需要使用负载均衡的server中增加

proxy_pass http://www.linuxidc.net/;
每个设备的状态设置为:

a)down 表示单前的server暂时不参与负载
b)weight 默认为1.weight越大,负载的权重就越大。
c)max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
d)fail_timeout:max_fails 次失败后,暂停的时间。
e)backup:其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug client_body_temp_path 设置记录文件的目录 可以设置最多3层目录 location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

 


Nginx 负载均衡实例2
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,也可以用作提高Squid缓存命中率.

简单的负载均等实例:

#vi nginx.conf  //nginx主配置文件核心配置
……….
#loadblance my.linuxidc.net
      upstream  my.linuxidc.net  {
      ip_hash;
      server   127.0.0.1:8080;
      server   192.168.169.136:8080;
      server   219.101.75.138:8080;
      server   192.168.169.117;
      server   192.168.169.118;
      server   192.168.169.119;
    }
…………..
include          vhosts/linuxtone_lb.conf;
………
# vi proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_store on;
proxy_store_access   user:rw  group:rw  all:r;
#nginx cache              
#client_body_temp_path  /data/nginx_cache/client_body 1 2;
proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
#vi  linuxtone_lb.conf
server
   {
       listen  80;
       server_name my.linuxidc.net;
       index index.php;
       root /data/www/wwwroot/mylinuxtone;
       if (-f $request_filename) {
           break;
          }
       if (-f $request_filename/index.php) {
         rewrite (.*) $1/index.php break;
       }
       error_page 403 http://my.linuxidc.net/member.php?m=user&a=login;
       location / {
          if ( !-e $request_filename) {
              proxy_pass http://my.linuxidc.net;
              break;
          }
          include /usr/local/nginx/conf/proxy.conf;
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值