利用Nginx反向代理原理,实现集群服务器瞬间故障转移,看用于生产环境中综合设置的例子:

下载:  proxy.conf
  1. #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

  2. proxy_temp_path /data0/proxy_temp_dir;

  3. #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。

  4. proxy_cache_path /data0/proxy_cache_dirlevels=1:2keys_zone=cache_one:200minactive=1dmax_size=30g;

  5. #轮询服务器,weight为服务器权重,与访问频率成正比,max_fails最大超时次数,fail_timeout服务器代理监听超时时间

  6. upstreambackend_server{

  7. server192.168.203.43:80weight=1max_fails=2fail_timeout=30s;

  8. server192.168.203.44:80weight=1max_fails=2fail_timeout=30s;

  9. server192.168.203.45:80weight=1max_fails=2fail_timeout=30s;

  10. }

  11. server

  12. {

  13. listen80;

  14. server_namewww.yourdomain.com192.168.203.42;

  15. indexindex.htmlindex.htm;

  16. root /data0/htdocs/www

  17. location /

  18. {

  19. #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。

  20. proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;

  21. proxy_cachecache_one;

  22. #对不同的HTTP状态码设置不同的缓存时间

  23. proxy_cache_valid20030412h;

  24. #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内

  25. proxy_cache_key$host$uri$is_args$args;

  26. proxy_set_headerHost$host;

  27. proxy_set_headerX-Forwarded-For$remote_addr;

  28. proxy_passhttp://backend_server;

  29. expires1d;

  30. }

  31. }

Nginx反向代理配置参数释义:

  1. proxy_set_header(设定header)

  2. proxy_hide_header(隐藏header)

  3. proxy_pass_header(通过header)

  4. proxy_connect_timeout(代理连接超时)

  5. proxy_send_timeout(代理发送超时)

  6. proxy_read_timeout(代理接收超时)

  7. proxy_temp_file_write_size(设定缓存文件夹大小)

  8. proxy_buffer_size(代理缓冲大小)

  9. proxy_buffers(代理缓冲)

  10. proxy_busy_buffers_size(高负荷下缓冲大小)

  11. proxy_ignore_client_abort(不允许代理端主动关闭连接)

下面就分步介绍基于Nginx反向代理的upstream对服务请求转发与分配5种方式,实际生成环境综合设置,为了便于说明问题分不同方式来说明,nginx反向代理实际生成环境的应用,请参考《如何设置nginx反向代理实现服务器瞬间故障转移》文章开篇部分的proxy.conf配置。

nginx的upstream目前支持5种方式的分配

1、轮询(默认)

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

2、weight

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

3、ip_hash

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

upstream bakend {
ip_hash ;
server 192.168.203.14 : 88 ;
server 192.168.203.15 : 80 ;
}

4、fair(第三方)

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

upstream backend {
server 192.168.203.14 : 88 ;
server 192.168.203.15 : 80 ;
fair ;
}

5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
server squid1 : 3128 ;
server squid2 : 3128 ;
hash $request_uri ;
hash_method crc32 ;
}
upstream bakend {
#定义负载均衡设备的Ip及设备状态
ip_hash ;
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 : //bakend/;

每个设备的状态设置为:

  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支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug

client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

转自http://www.sky54.net/?p=3002 | 天空青年