|
| 1 | +# ngx_http_upstream_conf_module |
| 2 | + |
| 3 | +- [示例配置](#example_configuration) |
| 4 | +- [指令](#directives) |
| 5 | + - [upstream_conf](#upstream_conf) |
| 6 | + |
| 7 | +**直到 1.13.10 版本,它被 1.13.3 中的 [ngx_http_api_module](ngx_http_api_module.md) 模块所取代。** |
| 8 | + |
| 9 | +`ngx_http_upstream_conf_module` 模块允许通过简单的 HTTP 接口即时配置上游(upstream)服务器组,而无需重新启动 nginx。[http](ngx_http_upstream_module.md#zone) 或[流](ngx_stream_upstream_module.md#zone)服务器组必须驻留在共享内存中。 |
| 10 | + |
| 11 | +> 该模块作为[商业订阅](http://nginx.com/products/?_ga=2.70698388.504417624.1562407216-1811470965.1562407216)部分提供, |
| 12 | +
|
| 13 | +<a id="example_configuration"></a> |
| 14 | + |
| 15 | +## 示例配置 |
| 16 | + |
| 17 | +```nginx |
| 18 | +upstream backend { |
| 19 | + zone upstream_backend 64k; |
| 20 | +
|
| 21 | + ... |
| 22 | +} |
| 23 | +
|
| 24 | +server { |
| 25 | + location /upstream_conf { |
| 26 | + upstream_conf; |
| 27 | + allow 127.0.0.1; |
| 28 | + deny all; |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +<a id="directives"></a> |
| 34 | + |
| 35 | +## 指令 |
| 36 | + |
| 37 | +### upstream_conf |
| 38 | + |
| 39 | +|\-|说明| |
| 40 | +|:------|:------| |
| 41 | +|**语法**|**upstream_conf**;| |
| 42 | +|**默认**|——| |
| 43 | +|**上下文**|location| |
| 44 | + |
| 45 | +在 location 中引入该指令,开启上游配置 HTTP 接口。 应[限制](ngx_http_core_module.md#satisfy)访问此 location。 |
| 46 | + |
| 47 | +配置命令可用于: |
| 48 | + |
| 49 | +- 查看组配置 |
| 50 | +- 查看、修改或删除服务器 |
| 51 | +- 添加新服务器 |
| 52 | + |
| 53 | +> 由于组中的地址不要求唯一,因此组中的服务器使用 ID 引用。添加新服务器或查看组配置时,会自动分配 ID 并显示 ID。 |
| 54 | +
|
| 55 | +配置命令作为请求参数传入,例如: |
| 56 | + |
| 57 | +``` |
| 58 | +http://127.0.0.1/upstream_conf?upstream=backend |
| 59 | +``` |
| 60 | + |
| 61 | +支持以下参数: |
| 62 | + |
| 63 | +- `stream=` |
| 64 | + |
| 65 | + 选择一个 [stream](../stream/ngx_stream_upstream_module.md) 上游服务器组。如果没有此参数,将选择一个 [http](ngx_http_upstream_module.md) 上游服务器组。 |
| 66 | + |
| 67 | +- `upstream=name` |
| 68 | + |
| 69 | + 选择要使用的组。此参数是必需的。 |
| 70 | + |
| 71 | +- `id=number` |
| 72 | + |
| 73 | + 选择一个要查看、修改或删除的服务器。 |
| 74 | + |
| 75 | +- `remove=` |
| 76 | + |
| 77 | + 从组中删除一个服务器。 |
| 78 | + |
| 79 | +- `add=` |
| 80 | + |
| 81 | + 向组中添加一个新服务器。 |
| 82 | + |
| 83 | +- `backup=` |
| 84 | + |
| 85 | + 需要添加一个备用服务器。 |
| 86 | + |
| 87 | + > 在 1.7.2 版之前,查看、修改或删除现有备用服务器还需要 `backup=` 参数。 |
| 88 | +
|
| 89 | +- `server=address` |
| 90 | + |
| 91 | + 与 [http](ngx_http_upstream_module.md#server) 或 [stream](../stream/ngx_stream_upstream_module.md#server) 上游服务器的 `address` 参数相同。 |
| 92 | + |
| 93 | + 添加服务器时,可以将其指定为一个域名。这种情况下,将监视与域名对应的 IP 地址的更改并自动应用于上游配置,无需重新启动 nginx(1.7.2)。这需要 [http](ngx_http_upstream_module.md#server) 或 [stream](../stream/ngx_stream_upstream_module.md#server) 块中的 `resolver` 指令。另请参阅 [http](ngx_http_upstream_module.md#server) 或 [stream](../stream/ngx_stream_upstream_module.md#server) 上游服务器的 `resolve` 参数。 |
| 94 | + |
| 95 | +- `service=name` |
| 96 | + |
| 97 | + 与 [http](ngx_http_upstream_module.md#service) 或 [stream](../stream/ngx_stream_upstream_module.md#service) 上游服务器的 `service` 参数相同(1.9.13)。 |
| 98 | + |
| 99 | +- `weight=number` |
| 100 | + |
| 101 | + 与 [http](ngx_http_upstream_module.md#weight) 或 [stream](../stream/ngx_stream_upstream_module.md#weight) 上游服务器的 `weight` 参数相同。 |
| 102 | + |
| 103 | +- `max_conns=number` |
| 104 | + |
| 105 | + 与 [http](ngx_http_upstream_module.md#max_conns) 或 [stream](../stream/ngx_stream_upstream_module.md#max_conns) 上游服务器的 `max_conns` 参数相同。 |
| 106 | + |
| 107 | +- `max_fails=number` |
| 108 | + |
| 109 | + 与 [http](ngx_http_upstream_module.md#max_fails) 或 [stream](../stream/ngx_stream_upstream_module.md#max_fails) 上游服务器的 `max_fails` 参数相同。 |
| 110 | + |
| 111 | +- `fail_timeout=time` |
| 112 | + |
| 113 | + 与 [http](ngx_http_upstream_module.md#fail_timeout) 或 [stream](../stream/ngx_stream_upstream_module.md#fail_timeout) 上游服务器的 `fail_timeout` 参数相同。 |
| 114 | + |
| 115 | +- `slow_start=time` |
| 116 | + |
| 117 | + 与 [http](ngx_http_upstream_module.md#slow_start) 或 [stream](../stream/ngx_stream_upstream_module.md#slow_start) 上游服务器的 `slow_start` 参数相同。 |
| 118 | + |
| 119 | +- `down=` |
| 120 | + |
| 121 | + 与 [http](ngx_http_upstream_module.md#down) 或 [stream](../stream/ngx_stream_upstream_module.md#down) 上游服务器的 `down` 参数相同。 |
| 122 | + |
| 123 | +- `drain=` |
| 124 | + 将 [http](ngx_http_upstream_module.md#draining) 上游服务器置为 `draining` 模式(1.7.5)。在此模式下,只有绑定到服务器的请求才会被代理。 |
| 125 | + |
| 126 | +- `up=` |
| 127 | + |
| 128 | + 与 [http](ngx_http_upstream_module.md#down) 或 [stream](../stream/ngx_stream_upstream_module.md#down) 上游服务器的 `down` 参数相反。 |
| 129 | + |
| 130 | +- `route=string` |
| 131 | + |
| 132 | + 与 [http](ngx_http_upstream_module.md#route) 上游服务器的 `route` 参数相同。 |
| 133 | + |
| 134 | +前三个参数会选择一个对象。这可以是整个 http 或 stream 上游服务器组,也可以是特定服务器。如果没有其他参数,则会显示所选组或服务器的配置。 |
| 135 | + |
| 136 | +例如,要查看整个组的配置,请发送: |
| 137 | + |
| 138 | +``` |
| 139 | +http://127.0.0.1/upstream_conf?upstream=backend |
| 140 | +``` |
| 141 | + |
| 142 | +要查看特定服务器的配置,还要指定其 ID: |
| 143 | + |
| 144 | +``` |
| 145 | +http://127.0.0.1/upstream_conf?upstream=backend&id=42 |
| 146 | +``` |
| 147 | + |
| 148 | +要添加新服务器,请在 `server=` 参数中指定其地址。如果未指定其他参数,添加服务器时将其他参数设置为其默认值(请参阅 [http](ngx_http_upstream_module.md#server) 或 [stream](../stream/ngx_stream_upstream_module.md#server) 的 `server` 指令)。 |
| 149 | + |
| 150 | +例如,要添加一个新的主服务器,请发送: |
| 151 | + |
| 152 | +``` |
| 153 | +http://127.0.0.1/upstream_conf?add=&upstream=backend&server=127.0.0.1:8080 |
| 154 | +``` |
| 155 | + |
| 156 | +要添加一个备用服务器,发送: |
| 157 | + |
| 158 | +``` |
| 159 | +http://127.0.0.1/upstream_conf?add=&upstream=backend&backup=&server=127.0.0.1:8080 |
| 160 | +``` |
| 161 | + |
| 162 | +要添加一个新的主服务器,将其参数设置为非默认值并将其标记为 `down`,发送: |
| 163 | + |
| 164 | +``` |
| 165 | +http://127.0.0.1/upstream_conf?add=&upstream=backend&server=127.0.0.1:8080&weight=2&down= |
| 166 | +``` |
| 167 | + |
| 168 | +删除一个服务器,指定其 ID: |
| 169 | + |
| 170 | +``` |
| 171 | +http://127.0.0.1/upstream_conf?remove=&upstream=backend&id=42 |
| 172 | +``` |
| 173 | + |
| 174 | +讲一个现有的服务器标记为 `down`,发送: |
| 175 | + |
| 176 | +``` |
| 177 | +http://127.0.0.1/upstream_conf?upstream=backend&id=42&down= |
| 178 | +``` |
| 179 | + |
| 180 | +修改一个现有服务器的地址,发送: |
| 181 | + |
| 182 | +``` |
| 183 | +http://127.0.0.1/upstream_conf?upstream=backend&id=42&server=192.0.2.3:8123 |
| 184 | +``` |
| 185 | + |
| 186 | +修改一个现有服务器的其他参数,发送: |
| 187 | + |
| 188 | +``` |
| 189 | +http://127.0.0.1/upstream_conf?upstream=backend&id=42&max_fails=3&weight=4 |
| 190 | +``` |
| 191 | + |
| 192 | +以上示例适用于 [http](ngx_http_upstream_module.md) 上游服务器组。[stream](../stream/ngx_stream_upstream_module.md) 上游服务器组需要加上 `stream=` 参数。 |
| 193 | + |
| 194 | +## 原文档 |
| 195 | + |
| 196 | +- [http://nginx.org/en/docs/http/ngx_http_upstream_conf_module.html](http://nginx.org/en/docs/http/ngx_http_upstream_conf_module.html) |
0 commit comments