Nginx的upstream指令

本文详细介绍了Nginx的upstream指令,包括其作用、语法和内部指令,如server、zone、hash、ip_hash、least_time、least_conn等。upstream用于定义服务器组,实现反向代理和负载均衡。文中提到了各种负载均衡策略,并讨论了url_hash(第三方)在缓存服务器中的应用。最后,文章提供了Nginx反向代理的示意图和官方文档链接作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的内部指令
  1. 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。
  1. 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.

  1. 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.

  1. 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;
}
  1. least_time

Syntax:

Syntax:	least_time header | last_byte [inflight];
Default:	—
Context:	upstream
This directive appeared in version 1.7.10.

Description: 指定组应使用负载平衡方法,其中将请求传递到服务器的平均响应时间最短且活动连接数最少,同时考虑服务器的权重。 如果有多个此类服务器,则依次使用加权循环平衡方法尝试它们。

  1. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值