upstream的作用:
ngx_http_upstream_module模块用于定义可以由proxy_pass,fastcgi_pass,uwsgi_pass,scgi_pass,memcached_pass和grpc_pass指令引用的服务器组。
Syntax
Syntax: upstream name { ... }
Default: —
Context: http
Descriptions: Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.
Example:
upstream dynamic {
zone upstream_dynamic 64k;
server backend1.example.com weight=5;
server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
server 192.0.2.1 max_fails=3;
server backend3.example.com resolve;
server backend4.example.com service=http resolve;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://dynamic;
health_check;
}
}
upstream backend2 {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
在实践中,我们可以通过upstram来反向代理,那么这个时候nginx就变成了一个请求接收服务器,然后加以配置,我们在业务模块需要做的就是通过upstream配合location来扩展业务。
upstream的内部指令
- server
Syntax:
Syntax: server address [parameters];
Default: —
Context: upstream
Description:反向代理服务器的地址和端口和其参数
Parameters:
- weight=number:sets the weight of the server, by default, 1. 用于请求的分派调度参数
- max_conns=number:限制代理的最大连接数,默认为0,代表不限制。
- max_fails=number:失败多少次 认为主机已挂掉则,踢出
- fail_timeout=time:代理被踢出后,的不可用时间,默认为10s
- backup:将代理标记为备份服务器,当主服务器不可用时启用,注意:该参数不能与哈希,ip_hash和随机负载平衡方法一起使用。
- down:将代理标记为永远不可用
- resolve:监视与服务器域名相对应的IP地址的更改,并自动修改上游配置
- slow_start=time:代理恢复时,其权重从0开始慢增长到weight。
- zone
Syntax:
Syntax: zone name [size];
Default: —
Context: upstream
This directive appeared in version 1.9.0.
Description:Defines the name and size of the shared memory zone that keeps the group’s configuration and run-time state that are shared between worker processes.
- hash
Syntax:
Syntax: hash key [consistent];
Default: —
Context: upstream
This directive appeared in version 1.7.2.
Description:Specifies a load balancing method for a server group where the client-server mapping is based on the hashed key value.
- ip_hash
Syntax:
Syntax: ip_hash;
Default: —
Context: upstream
Description: 制定的负载均衡的方法,可以使得来自同一台机器的请求尽可能到同意服务器。
Example:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
server backend4.example.com;
}
- least_time
Syntax:
Syntax: least_time header | last_byte [inflight];
Default: —
Context: upstream
This directive appeared in version 1.7.10.
Description: 指定组应使用负载平衡方法,其中将请求传递到服务器的平均响应时间最短且活动连接数最少,同时考虑服务器的权重。 如果有多个此类服务器,则依次使用加权循环平衡方法尝试它们。
- least_conn
Syntax:
Syntax: least_conn;
Default: —
Context: upstream
This directive appeared in versions 1.3.1 and 1.2.2.
Description: 一种负载均衡的调度方式,就是按照那个设备连接最少就分配给那个设备
Nginx的负载均衡的方法还有url_hash(第三方)
Decription: 主要应用于缓存服务器上, 按照访问的 url 来分配请求,让相同的 url 定向到同一个服务器,后端服务器为缓存。服务器的时候效果更显著,在 upstream 中加入 hash 语句,server 语句中不能写入 weight
等其他的参数,hash_method 是使用的 hash 算法。 缺点:如果有一台机器宕机了,那就苦了,consistent_hash 可以解决这个问题。 可以提高后端缓存服务器的效率,nginx 本身不支持 url_hash 的,需要下载 hash软件。
Nginx的反向代理示意图:
Reference:
[1] http://nginx.org/en/docs/http/ngx_http_upstream_module.html