nginx学习(七)——nginx的配置系统3之upstream_module(下)

时间:2021-07-10 12:19:19

health_check

Enables periodic health checks of the servers in a group referenced in the surrounding location.
The following optional parameters are supported:

interval=time

sets the interval between two consecutive health checks, by default, 5 seconds;
设置两次health check的间隔时间,默认5秒。
fails=number
sets the number of consecutive failed health checks of a particular server after which this server will be considered unhealthy, by default, 1;
设置连续多少次健康检查失败就认为该服务器是不健康的。默认是1.
passes=number
sets the number of consecutive passed health checks of a particular server after which the server will be considered healthy, by default, 1;
设置连续多少次健康检查通过就认为该服务器是健康的。默认是1.
uri=uri
defines the URI used in health check requests, by default, “/”;
设置健康检查的uri。默认是/.
match=name
specifies the match block configuring the tests that a response should pass in order for a health check to pass; by default, the response should have status code 2xx or 3xx;
定义match块儿,用来配置当请求通过健康检查时的响应。默认情况下,响应应该有状态码2xx或者3xx。
port=number
defines the port used when connecting to a server to perform a health check (1.9.7); by default, equals the server port.
定义连接服务器进行健康检查的端口号。默认使用服务器的端口号。
For example,
http {
server {
...
location / {
proxy_pass http://backend;
health_check match=welcome;
}
}

match welcome {
status 200;
header Content-Type = text/html;
body ~ "Welcome to nginx!";
}
}
This configuration shows that in order for a health check to pass, the response to a health check request should succeed, have status 200, content type “text/html”, and contain “Welcome to nginx!” in the body.
If several health checks are defined for the same group of servers, a single failure of any check will make the corresponding server be considered unhealthy.
This directive is available as part of our commercial subscription.
该配置表示,为了通过健康检查,响应应该正确返回,状态是200,content type是text/html,内容有welcome to nginx在body体里面。
如果一组服务器配置了几个健康检查的话,只要有一个失败,就会认为该服务器是不健康的。
该指令在商业收费版本中才有(坑爹啊)

match 

status 200;
status is 200
status ! 500;
status is not 500
status 200 204;
status is 200 or 204
status ! 301 302;
status is neither 301 nor 302
status 200-399;
status is in the range from 200 to 399
status ! 400-599;
status is not in the range from 400 to 599
status 301-303 307;
status is either 301, 302, 303, or 307
header Content-Type = text/html;
header contains “Content-Type” with value text/html
header Content-Type != text/html;
header contains “Content-Type” with value other than text/html
header Connection ~ close;
header contains “Connection” with value matching regular expression close
header Connection !~ close;
header contains “Connection” with value not matching regular expression close
header Host;
header contains “Host”
header ! X-Accel-Redirect;
header lacks “X-Accel-Redirect”
body ~ "Welcome to nginx!";
body matches regular expression “Welcome to nginx!”
body !~ "Welcome to nginx!";
body does not match regular expression “Welcome to nginx!”
Examples:
# status is 200, content type is "text/html",
# and body contains "Welcome to nginx!"
match welcome {
status 200;
header Content-Type = text/html;
body ~ "Welcome to nginx!";
}
# status is not one of 301, 302, 303, or 307, and header does not have "Refresh:"
match not_redirect {
status ! 301-303 307;
header ! Refresh;
}
# status ok and not in maintenance mode
match server_ok {
status 200-399;
body !~ "maintenance mode";
}

queue 

If an upstream server cannot be selected immediately while processing a request, and there are the servers in the group that have reached the max_conns limit, the request will be placed into the queue. The directive specifies the maximum number of requests that can be in the queue at the same time. If the queue is filled up, or the server to pass the request to cannot be selected within the time period specified in the timeout parameter, the 502 (Bad Gateway) error will be returned to the client.

The default value of the timeout parameter is 60 seconds.
如果upstream服务器在处理请求的时候不是立即可用的,同时组内的其他服务器都到达了最大连接数限制,这时请求会被放到一个队列中。该指令指定队列中能够同时容纳的最大请求梳理阿玲。如果队列满了,或者当前服务器在超时时间之内无法响应请求,会返回502给用户。

hash

Specifies a load balancing method for a server group where the client-server mapping is based on the hashed key value. The key can contain text, variables, and their combinations. Note that adding or removing a server from the group may result in remapping most of the keys to different servers. The method is compatible with the Cache::Memcached Perl library.
If the consistent parameter is specified the ketama consistent hashing method will be used instead. The method ensures that only a few keys will be remapped to different servers when a server is added to or removed from the group. This helps to achieve a higher cache hit ratio for caching servers. The method is compatible with the Cache::Memcached::Fast Perl library with the ketama_points parameter set to 160.
给一组服务器指定一个负载均衡方法,根据hash的key值来进行客户端请求的映射。
值得一提的是,向一个服务器组添加新的服务器,或者从一个服务器组移除某个服务器都会导致大部分不同服务器的key重新映射(remapping)。
该方法确保了只会有少数几个key重新映射到不同的服务器上,当一个服务器被添加或者被移除。该方法可以提高缓存服务器的命中率。
upstream xxcupid1 {
#least_conn;
#ip_hash;
#Consumer
server 192.168.237.186:10087 weight=3;
#Provider
server 192.168.237.186:10086;
hash $remote_addr;
hash_again 10;
#server srv3.example.com;
}
会根据$remote_addr来做hash,这样来自同一个remote_addr的请求就会打到同样的机器上了。当然还可以指定按照不同的参数来做hash。