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