nginx学习(六)——nginx的配置系统3之upstream_module(上)

来源:互联网 发布:java编程心得体会 编辑:程序博客网 时间:2024/06/08 10:20

upstream

Module ngx_http_upstream_module
upstream可以用来定义一组服务器,这些服务器可以通过proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass这些指令指定的域名关联起来,比如下面这组配置,backend就可以和server location中proxy_pass定义的backend关联起来。
当用户访问http://backend的时候,就会映射到upstream定义的虚拟服务器上了。
upstream backend {    server backend1.example.com       weight=5;    server backend2.example.com:8080;    server unix:/tmp/backend3;    server backup1.example.com:8080   backup;    server backup2.example.com:8080   backup;}server {    location / {        proxy_pass http://backend;    }}

weight=number

设置某个服务器的权重,默认为1.

max_fails=number

sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream directives.
当服务器不可用的时候重试的次数。

fail_timeout=time

the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable;
and the period of time the server will be considered unavailable.
By default, the parameter is set to 10 seconds.

backup

marks the server as a backup server. It will be passed requests when the primary servers are unavailable.
当配置的主要的服务器都不可用的时候,就使用这个备份服务器来处理用户请求。

down

marks the server as permanently unavailable.
标记当前服务器处于暂时不可用的状态。

ip_hash

Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.
If one of the servers needs to be temporarily removed, it should be marked with the down parameter in order to preserve the current hashing of client IP addresses.
用户的请求会根据用户的ip地址分配到不同的服务器上。IPV4地址的前三位用来做hash。该指令会保证来自相同ip的用户请求会分发到同一台服务器上(除非该服务器不可用)。如果一台服务器需要暂时被移除,应该加上down指令,以保证当前按照用户IP地址的hash值不发生变化(不用去rehash)。

keepalive

Activates the cache for connections to upstream servers.

The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.

It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open. The connections parameter should be set to a number small enough to let upstream servers process new incoming connections as well.

Example configuration of memcached upstream with keepalive connections:

upstream memcached_backend {    server 127.0.0.1:11211;    server 10.0.0.2:11211;    keepalive 32;}server {    ...    location /memcached/ {        set $memcached_key $uri;        memcached_pass memcached_backend;    }    server {    ...    location /http/ {        proxy_pass http://http_backend;        proxy_http_version 1.1;        proxy_set_header Connection "";        ...    }}

For HTTP, the proxy_http_version directive should be set to “1.1” and the “Connection” header field should be cleared:

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version

Sets the HTTP protocol version for proxying. By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.

启动upstream服务器的连接缓存。该参数设置了每个worker进程缓存的最大的空闲“长连接”个数。当超过该值之后,根据LRU算法关闭对应的长连接。

需要特别注意的是,该参数不会限制一个nginx worker进程可以打开的总连接数。这个参数应该设置的足够小,以便于让upstream服务器也能够处理建立新的连接来处理新的请求(长连接是在同一个连接上执行多个请求)。

如果是keepAlive connections(长连接),proxy_http_version推荐使用1.1。

least_conn

Specifies that a group should use a load balancing method where a request is passed to the server with the least number of active connections, taking into account weights of servers. If there are several such servers, they are tried in turn using a weighted round-robin balancing method.

请求会被分发到拥有最少活跃连接数的服务器上(服务器当前正在处理请求的连接数或者长连接数最少)在考虑权证的情况下。如果有多个这样的服务器,那么就按照权重的轮训机制来分发请求。

least_time 

Specifies that a group should use a load balancing method where a request is passed to the server with the least average response time and least number of active connections, taking into account weights of servers. If there are several such servers, they are tried in turn using a weighted round-robin balancing method.

请求会被分发给平均响应时间最小的服务器。








nginx还有很多的配置,需要用到的时候可以查看nginx网站上的文档。

Alphabetical index of directives

0 0