Nginx中ip_hash负载均衡中location的proxy_pass配置与说明

来源:互联网 发布:银行家算法例题 编辑:程序博客网 时间:2024/05/21 19:49

Nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕机了才会换一个。

?
1
2
3
4
5
6
7
upstream tomcatserver {#定义负载均衡 设备的Ip及设备状态
ip_hash;
    server :8080down;
    server :8888weight=2;
    server :9999;
    server :7777backup;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    server {
        listen      80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
             proxy_pass        http://tomcatserver;
        }
        # 转发jsp servlet 或springmvc请求
        #
         location  ~ (\.jsp)|(\.do)|(\.action)$     {
            proxy_pass   http://tomcatserver;
         }
}

每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1,weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

0 0