nginx 配置路由 负载均衡以及服务器故障转移 简单案例

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

  1. #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区  
  2. proxy_temp_path   /data0/proxy_temp_dir;  
  3.  
  4. #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。  
  5. proxy_cache_path   /data0/proxy_cache_dir   levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;  
  6.  
  7. #轮询服务器,weight为服务器权重,与访问频率成正比,max_fails最大超时次数,fail_timeout服务器代理监听超时时间  
  8. upstream backend_server {  
  9.  server   192.168.203.43:80 weight=1 max_fails=2 fail_timeout=30s;  
  10.  server   192.168.203.44:80 weight=1 max_fails=2 fail_timeout=30s;  
  11.  server   192.168.203.45:80 weight=1 max_fails=2 fail_timeout=30s;  
  12. }  
  13.  
  14. server  
  15. {  
  16.      listen       80;  
  17.      server_name   www.yourdomain.com 192.168.203.42;  
  18.      index index.html index.htm;  
  19.      root   /data0/htdocs/www;    
  20.  
  21.     location /  
  22.     {  
  23.      #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。  
  24.      proxy_next_upstream http_502 http_504 error timeout invalid_header;  
  25.      proxy_cache cache_one;  
  26.        
  27.      #对不同的HTTP状态码设置不同的缓存时间  
  28.      proxy_cache_valid   200 304 12h;  
  29.        
  30.      #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内  
  31.      proxy_cache_key $host$uri$is_args$args;  
  32.      proxy_set_header Host   $host;  
  33.      proxy_set_header X-Forwarded-For   $remote_addr;  
  34.      proxy_pass http://backend_server;  
  35.      expires       1d;  
  36.     }  

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

 
 
  1. 1.proxy_set_header(设定header)  
  2. 2.proxy_hide_header(隐藏header)  
  3. 3.proxy_pass_header(通过header)  
  4. 4.proxy_connect_timeout(代理连接超时)  
  5. 5.proxy_send_timeout(代理发送超时)  
  6. 6.proxy_read_timeout(代理接收超时)  
  7. 7.proxy_temp_file_write_size(设定缓存文件夹大小)  
  8. 8.proxy_buffer_size(代理缓冲大小)  
  9. 9.proxy_buffers (代理缓冲)  
  10. 10.proxy_busy_buffers_size(高负荷下缓冲大小)  
  11. 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的问题。

 
 
  1. upstream bakend {  
  2.     ip_hash;  
  3.     server 192.168.203.14:88;  
  4.     server 192.168.203.15:80;  

4、fair(第三方)

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

 
 
  1. upstream backend {  
  2.     server 192.168.203.14:88;  
  3.     server 192.168.203.15:80;  
  4.     fair;  

5、url_hash(第三方)

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

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

 
 
  1. upstream backend {  
  2.     server squid1:3128;  
  3.     server squid2:3128;  
  4.     hash $request_uri;  
  5.     hash_method crc32;  
 
 
  1. upstream bakend{  
  2.     #定义负载均衡设备的Ip及设备状态  
  3.     ip_hash;  
  4.     server 127.0.0.1:9090 down;  
  5.     server 127.0.0.1:8080 weight=2;  
  6.     server 127.0.0.1:6060;  
  7.     server 127.0.0.1:7070 backup;  

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

 
 
  1. proxy_pass http://bakend/; 

每个设备的状态设置为:

 
 
  1. 1.down 表示单前的server暂时不参与负载  
  2. 2.weight 默认为1.weight越大,负载的权重就越大。  
  3. 3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误  
  4. 4.fail_timeout:max_fails次失败后,暂停的时间。  
  5. 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

猜你喜欢

转载自blog.csdn.net/t8116189520/article/details/80668608
今日推荐