说说upstream里的server指令:
server 后面可以是域名格式,也可以是socket格式[ip:port],后面还可以带参数。参数有下面几个:
weight = NUMBER - 设置服务器的权重值,默认为1. 值越大,分配的请求越多。只适用于轮询这种LB策略。
max_fails = NUMBER - 在fail_timeout设置的时间内,尝试连接服务器失败的次数.默认为1,0表示关闭检查.错误类型在proxy_next_upstream or fastcgi_next_upstream中定义,(除了404错误不计入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 (cannot be used with the directive ip_hash)
nginx负载均衡的策略:
1.轮询(默认方式)
对于一级后端服务器群,形成一个环队列的形式,对于每个到达的请求按时间顺序顺次分配给这些后端服务器。在前端调度器与后端服务器之间采用“心跳”方式进行状态检查,如果发现后端服务器宕机,则将其删除。
这种方式为默认配置,优点是简洁,但缺点是无法进行最优化调度,有可能有的请求需要耗时较久,这样会带来一定的不平衡。
它的例子:在http区域里添加:
upstream lb {
server 10.10.57.122:80;
server 10.10.57.123:80;
}
在你的某个server里增加:
location / {
proxy_pass http://lb;
}
2. 加权轮询
这是一种对上述方式的改进,引入权值的概念,能够解决后端服务器性能不均的情况。
例如这样一个配置:
upstream lb {
server 10.10.57.122:80 weight=5;
server 10.10.57.123:80 weight=10;
}
This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.
It is not possible to combine ip_hash and weight methods for connection distribution. If one of the servers must be removed for some time, you must mark that server as *down*.