0
点赞
收藏
分享

微信扫一扫

nginx的负载均衡和故障转移

云卷云舒xj 2022-01-17 阅读 65

#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data0/proxy_temp_dir;

#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#轮询服务器,weight为服务器权重,与访问频率成正比,max_fails最大超时次数,fail_timeout服务器代理监听超时时间
upstream backend_server {
server 192.168.203.43:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.203.44:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.203.45:80 weight=1 max_fails=2 fail_timeout=10s;
}

server  
{  
     listen       80;  
     server_name   www.yourdomain.com 192.168.203.42;  
     index index.html index.htm;  
     root   /data0/htdocs/www;    
 
    location /  
    {  
     #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。  
     proxy_next_upstream http_502 http_504 error timeout invalid_header;  
     proxy_cache cache_one;  
       
     #对不同的HTTP状态码设置不同的缓存时间  
     proxy_cache_valid   200 304 12h;  
       
     #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内  
     proxy_cache_key $host$uri$is_args$args;  
     proxy_set_header Host   $host;  
     proxy_set_header X-Forwarded-For   $remote_addr;  
     proxy_pass http://backend_server;  
     expires       1d;  
    }  

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反向代理实际生成环境的应用,请参考文章开篇部分的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/;
每个设备的状态设置为:

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

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

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

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

转载自:https://blog.csdn.net/shudaqi2010/article/details/54341015

举报

相关推荐

0 条评论