# 负载均衡策略

<table><thead><tr><th width="163">负载均衡算法</th><th>作用</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>轮询</strong></mark></td><td><p>逐一轮询，<mark style="color:blue;"><strong>默认方式</strong></mark> 。<strong>如果服务器down掉了，会自动剔除该服务器。</strong></p><ul><li>此策略适合服务器配置相当，无状态且短平快的服务使用</li><li>业务无特殊要求时使用</li><li>也适用于图片服务器集群和纯静态页面服务器集群</li></ul></td></tr><tr><td><mark style="color:blue;"><strong>weight</strong></mark></td><td><strong>加权轮询</strong>，<mark style="color:blue;"><strong>weight越大，分配的几率越高</strong></mark>。<br>适用业务场景：用于后端服务器硬件性处理能力不平均的情形</td></tr><tr><td><mark style="color:blue;"><strong>ip_hash</strong></mark></td><td><p><mark style="color:blue;"><strong>按照访问IP的hash值进行分配，会导致来自同一IP的请求访问固定的一个后台服务器。</strong></mark></p><p>适合需要<strong>状态保持的服务</strong>，例如需要账号登录的系统，会话连接保持的业务。</p></td></tr><tr><td><mark style="color:blue;"><strong>url_hash</strong></mark></td><td><p><mark style="color:blue;"><strong>按照访问URL的hash结果分配，</strong></mark><strong>适用于后端服务器为缓存服务器时比较有效</strong></p><p>（需编译安装第三方模块 ngx_http_upstream_hash_module）</p></td></tr><tr><td><mark style="color:blue;"><strong>least_conn</strong></mark></td><td><p><mark style="color:blue;"><strong>按HTTP连接数来分配请求，连接数少的优先分配。</strong></mark></p><p>适合请求处理时间长短不一造成服务器过载的业务场景。</p></td></tr><tr><td><mark style="color:blue;"><strong>fair</strong></mark></td><td><p><mark style="color:blue;"><strong>按后端服务器的响应时间来分配请求，响应时间短的优先分配</strong></mark>。 适合对访问响应速度有一定要求的业务。</p><p>(需编译安装第三方模块 ngx_http_upstream_fair_module)</p></td></tr></tbody></table>

### ip\_hash 的配置示例

```nginx
# 动态服务器组
upstream dynamic_server {
  ip_hash;  # 保证每个访客固定访问一个后端服务器
  server localhost:8080  weight=2; # jdk 8
  server localhost:8081; # jdk 17
  server localhost:8082; # jdk 16
  server localhost:8083  max_fails=3 fail_timeout=20s; # jdk 15
}
```

{% hint style="warning" %}

* ip\_hash**不能与backup同时使用**
* 由于ip\_hash技术主要根据客户端IP地址分配服务器，因此**在整个系统中，Nginx服务器应该是处于最前端的服务器**，这样才能获取到客户端的IP地址，否则它得到的IP地址将是位于它前面的服务器地址，从而就会产生问题
  {% endhint %}

### **url\_hash配置示例**

按目标url的hash结果来分配请求，使每个url定向到同一个后端服务器，要配合缓存命中来使用。

```nginx
# 动态服务器组
upstream dynamic_server {
  hash $request_uri;  # 实现每个url定向到同一个后端服务器
  server localhost:8080;
  server localhost:8081; 
  server localhost:8082 max_fails=3 fail_timeout=20s; # jdk 15
}

```

### **least\_conn配置示例**

有些请求占用的时间很长，会导致其所在的后端负载较高。这种情况下，least\_conn这种方式就可以达到更好的负载均衡效果。

```nginx
# 动态服务器组
upstream dynamic_server {
  least_conn;  # 把请求优先转发给连接数较少的后端服务器
  server localhost:8080  weight=2; #jdk 8
  server localhost:8081; #jdk 17
  server localhost:8082  backup; #jdk 16
  server localhost:8083  max_fails=3 fail_timeout=20s; #jdk 15
}
```

### **fair配置示例**

特点：按后端服务器的响应时间来分配请求，响应时间短的优先分配。&#x20;

适用业务场景：对访问响应速度有一定要求的业务

```nginx
# 动态服务器组
upstream dynamic_server {
  fair;  # 把请求转发给连接数较少的后端服务器
  server localhost:8080;
  server localhost:8081;
  server localhost:8082;
  server localhost:8083 max_fails=3 fail_timeout=20s;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/devops/ngnix/ji-chu-zhi-shi/4.-nginx-fu-wu-qi-de-dai-li-fu-wu/4.4-upstream/fu-zai-jun-heng-ce-le.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
