# Nginx 负载均衡

![](https://kingofzihua.oss-cn-shanghai.aliyuncs.com/blog/nginx/load-balancing-nginx.png)

## 配置语法

| upstream |                     |
| -------- | ------------------- |
| 语法       | upstream name {...} |
| 默认       | ---                 |
| 作用域      | http                |

## 后段服务器在负载调度中的状态

| 状态            | 介绍                      |
| ------------- | ----------------------- |
| down          | 当前的server暂时不参与负载均衡      |
| backup        | 预留的备份服务器                |
| max\_fails    | 允许请求失败的次数               |
| fail\_timeout | 经过max\_fails失败后,服务暂停的时间 |
| max\_conns    | 限制最大的接受连接数              |

### 例子:

```
        ...
    # 在http里面
    upstream test{
        server 192.168.10.10:8001;

        # weight : 负载权重
        server 192.168.10.10:8002 weight=5;

        # backup : 表示这个是一个备份节点
        server 192.168.10.10:8003 backup; 

        # down : 表示这个节点不参与服务
        server 192.168.10.10:8004 down;

        # max_fails : 允许请求失败的次数 fail_timeout : 经过max_fails失败后,服务暂停的时间
        server 192.168.10.10:8005 max_fails=1 fail_timeout=10s;

        # unix : socket方式
        #server unix:/tmp/backend3; 
    }

    server {
        ....

        location / {
            # test是上面定义的upstream 的名字
            proxy_pass http://test
            proxy_redirect default; #302跳转的时候，默认

            ...

        }

        ...
    }
```


---

# 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://kingofzihua.gitbook.io/middleware-architecture-based-on-nginx/chang-jing-shi-jian/fu-zai-jun-heng/nginx-fu-zai-jun-heng.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.
