Nginx的负载均衡配置范例

    server {
        listen       80;
        server_name tc.test.nd ;

        location / {
           proxy_pass http://gwy.test.nd; 
        }
    }
 

 

 

 

upstream load_balance {
        server localhost:8088 ;
        server 192.168.1.4:8080 down;
        server home.ucenter weight=2;
        server backserver:9100 backup;
        
}
server {
        listen 80;
        #location / {
        #       proxy_pass http://home.ucenter/;
        #}
        location / {
                proxy_pass http://load_balance;
        }
        server_name proxy;
}
配置解释:
1)定义一个server集群load_balance,可以用域名,也可以直接用IP,(要注意不要加上http://).
2)用前一节讲的proxy_pass实现代理。把域名代理到集群名上面.

3)结束了,哈哈。简单吧。
单台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机器。所以这台机器压力正常最小。
附上英文:
# weight = NUMBER - set weight of the server, if not set weight is equal to one.

#max_fails = NUMBER - number of unsuccessful attempts at communicating with the server within the time period (assigned by parameter fail_timeout) after which it is considered inoperative. If not set, the number of attempts is one. A value of 0 turns off this check. What is considered a failure is defined by proxy_next_upstream or fastcgi_next_upstream (except http_404 errors which do not count towards max_fails).

#fail_timeout = TIME - the time during which must occur *max_fails* number of unsuccessful attempts at communication with the server that would cause the server to be considered inoperative, and also the time for which the server will be considered inoperative (before another attempt is made). If not set the time is 10 seconds. fail_timeout has nothing to do with upstream response time, use proxy_connect_timeout and proxy_read_timeout for controlling this.

# down - marks server as permanently offline, to be used with the directive ip_hash.

# backup - (0.6.7 or later) only uses this server if the non-backup servers are all down or busy

你可能感兴趣的:(nginx)