|
| 1 | +# ngx_http_memcached_module |
| 2 | + |
| 3 | +- [指令](#directives) |
| 4 | + - [memcached_bind](#memcached_bind) |
| 5 | + - [memcached_buffer_size](#memcached_buffer_size) |
| 6 | + - [memcached_connect_timeout](#memcached_connect_timeout) |
| 7 | + - [memcached_force_ranges](#memcached_force_ranges) |
| 8 | + - [memcached_gzip_flag](#memcached_gzip_flag) |
| 9 | + - [memcached_next_upstream](#memcached_next_upstream) |
| 10 | + - [memcached_next_upstream_timeout](#memcached_next_upstream_timeout) |
| 11 | + - [memcached_next_upstream_tries](#memcached_next_upstream_tries) |
| 12 | + - [memcached_pass](#memcached_pass) |
| 13 | + - [memcached_read_timeout](#memcached_read_timeout) |
| 14 | + - [memcached_send_timeout](#memcached_send_timeout) |
| 15 | +- [内嵌变量](#embedded_variables) |
| 16 | + |
| 17 | +`ngx_http_memcached_module` 模块用于从 memcached 服务器上获取响应。key 设置在 `$memcached_key` 变量中。应通过 nginx 之外的方式提前将响应放入 memcached。 |
| 18 | + |
| 19 | +<a id="example_configuration"></a> |
| 20 | + |
| 21 | +## 示例配置 |
| 22 | + |
| 23 | +```nginx |
| 24 | +server { |
| 25 | + location / { |
| 26 | + set $memcached_key "$uri?$args"; |
| 27 | + memcached_pass host:11211; |
| 28 | + error_page 404 502 504 = @fallback; |
| 29 | + } |
| 30 | +
|
| 31 | + location @fallback { |
| 32 | + proxy_pass http://backend; |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +<a id="directives"></a> |
| 38 | + |
| 39 | +## 指令 |
| 40 | + |
| 41 | +### memcached_bind |
| 42 | + |
| 43 | +|\-|说明| |
| 44 | +|------:|------| |
| 45 | +|**语法**|**memcached_bind** `address [transparent ]` | `off`;| |
| 46 | +|**默认**|——| |
| 47 | +|**上下文**|http、server、location| |
| 48 | +|**提示**|该指令在 0.8.22 版本中出现| |
| 49 | + |
| 50 | +通过可选端口(1.11.2)从指定的本地 IP 地址发出到memcached 服务器的传出连接。参数值可以包含变量(1.3.12)。特殊值 `off`(1.3.12)取消了从先前配置级别继承的 `memcached_bind` 指令的效果,这允许系统自动分配本地 IP 地址和端口。 |
| 51 | + |
| 52 | +`transparent` 参数(1.11.0)允许从非本地 IP 地址(例如,从客户端的真实 IP 地址)发出到 memcached 服务器的传出连接: |
| 53 | + |
| 54 | +```nginx |
| 55 | +memcached_bind $remote_addr transparent; |
| 56 | +``` |
| 57 | + |
| 58 | +为了使这个参数起作用,通常需要以超级用户权限运行nginx工作进程。 在Linux上不需要(1.13.8),就好像指定了透明参数一样,工作进程继承了主进程中的CAP_NET_RAW功能。 还有必要配置内核路由表以拦截来自memcached服务器的网络流量。 |
| 59 | + |
| 60 | +### memcached_buffer_size |
| 61 | + |
| 62 | +|\-|说明| |
| 63 | +|------:|------| |
| 64 | +|**语法**|**memcached_buffer_size** `size`;| |
| 65 | +|**默认**|memcached_buffer_size 4k|8k| |
| 66 | +|**上下文**|http、server、location| |
| 67 | + |
| 68 | +设置用于读取从memcached服务器接收到的响应的缓冲区的大小。 一旦收到响应,响应便会同步传递给客户端。 |
| 69 | + |
| 70 | +### memcached_connect_timeout |
| 71 | + |
| 72 | +|\-|说明| |
| 73 | +|------:|------| |
| 74 | +|**语法**|**memcached_connect_timeout** `time`;| |
| 75 | +|**默认**|memcached_connect_timeout 60s| |
| 76 | +|**上下文**|http、server、location| |
| 77 | + |
| 78 | +定义与memcached服务器建立连接的超时时间。 应该指出的是,这个超时通常不能超过75秒。 |
| 79 | + |
| 80 | +### memcached_force_ranges |
| 81 | + |
| 82 | +|\-|说明| |
| 83 | +|------:|------| |
| 84 | +|**语法**|**memcached_force_ranges** `on` | `off`;| |
| 85 | +|**默认**|memcached_force_ranges off;| |
| 86 | +|**上下文**|http、server、location| |
| 87 | +|**提示**|该指令在 1.7.7 版本中出现| |
| 88 | + |
| 89 | +无论这些响应中的 **Accept-Ranges** 字段如何,都可以对来自 memcached 服务器的缓存和未缓存的响应启用 byte-range 支持。 |
| 90 | + |
| 91 | +### memcached_gzip_flag |
| 92 | + |
| 93 | +|\-|说明| |
| 94 | +|------:|------| |
| 95 | +|**语法**|**memcached_gzip_flag** `flag`;| |
| 96 | +|**默认**|——| |
| 97 | +|**上下文**|http、server、location| |
| 98 | +|**提示**|该指令在 1.3.6 版本中出现| |
| 99 | + |
| 100 | +启用对 memcached 服务器响应中的标志存在性的测试,并在标志设置时将 **Content-Encoding** 响应标头字段设置为 **gzip**。 |
| 101 | + |
| 102 | +### memcached_next_upstream |
| 103 | + |
| 104 | +|\-|说明| |
| 105 | +|------:|------| |
| 106 | +|**语法**|**memcached_next_upstream** `error` | `timeout` | `invalid_response` | `not_found` | `off ...`;| |
| 107 | +|**默认**|memcached_next_upstream error timeout;| |
| 108 | +|**上下文**|http、server、location| |
| 109 | + |
| 110 | +指定在哪些情况下请求应传递给下一台服务器: |
| 111 | + |
| 112 | +- `error` |
| 113 | + |
| 114 | + 在与服务器建立连接,传递请求或读取响应头时发生错误; |
| 115 | + |
| 116 | +- `timeout` |
| 117 | + |
| 118 | + 在与服务器建立连接,传递请求或读取响应头时发生超时; |
| 119 | + |
| 120 | +- `invalid_response` |
| 121 | + |
| 122 | + 服务器返回了空的或无效的响应; |
| 123 | + |
| 124 | +- `invalid_response` |
| 125 | + |
| 126 | + 在服务器上未找到响应; |
| 127 | + |
| 128 | +- `off` |
| 129 | + |
| 130 | + 禁用将请求传递给下一个服务器。 |
| 131 | + |
| 132 | +人们应该记住,只有在没有任何内容发送给客户端的情况下,才能将请求传递到下一台服务器。也就是说,如果在响应传输过程中发生错误或超时,修复这是不可能的。 |
| 133 | + |
| 134 | +该指令还定义了与服务器进行通信的[失败尝试](ngx_http_upstream_module.md#max_fails)。即使未在指令中指定,`error`、`timeout` 和 `invalid_response` 的情况始终被视为不成功尝试。`not_found` 的情况永远不会被视为不成功的尝试。 |
| 135 | + |
| 136 | +将请求传递给下一台服务器可能受到尝试次数和时间的限制。 |
| 137 | + |
| 138 | +### memcached_next_upstream_tries |
| 139 | + |
| 140 | +|\-|说明| |
| 141 | +|------:|------| |
| 142 | +|**语法**|**memcached_next_upstream_tries** `number`;| |
| 143 | +|**默认**|memcached_next_upstream_tries 0;| |
| 144 | +|**上下文**|http、server、location| |
| 145 | +|**提示**|该指令在 1.7.5 版本中出现| |
| 146 | + |
| 147 | +限制将请求传递到下一个服务器的可能尝试次数。`0` 值关闭此限制。 |
| 148 | + |
| 149 | +### memcached_pass |
| 150 | + |
| 151 | +|\-|说明| |
| 152 | +|------:|------| |
| 153 | +|**语法**|**memcached_pass** `address`;| |
| 154 | +|**默认**|——| |
| 155 | +|**上下文**|http、location 中 if| |
| 156 | + |
| 157 | +设置 memcached 服务器地址。该地址可以指定为域名或 IP 地址,以及端口: |
| 158 | + |
| 159 | +```nginx |
| 160 | +memcached_pass localhost:11211; |
| 161 | +``` |
| 162 | + |
| 163 | +或者作为 UNIX 域套接字路径: |
| 164 | + |
| 165 | +```nginx |
| 166 | +memcached_pass unix:/tmp/memcached.socket; |
| 167 | +``` |
| 168 | + |
| 169 | +如果域名解析为多个地址,则所有这些地址都将以循环方式使用。另外,地址可以被指定为服务器组。 |
| 170 | + |
| 171 | +### memcached_read_timeout |
| 172 | + |
| 173 | +|\-|说明| |
| 174 | +|------:|------| |
| 175 | +|**语法**|**memcached_read_timeout** `time`;| |
| 176 | +|**默认**|memcached_read_timeout 60s;| |
| 177 | +|**上下文**|http、server、location| |
| 178 | + |
| 179 | +定义从 memcached 服务器读取响应的超时。超时只在两次连续的读操作之间设置,而不是用于传输整个响应。 如果 memcached 服务器在此时间内没有传输任何内容,则连接将被关闭。 |
| 180 | + |
| 181 | +### memcached_send_timeout |
| 182 | + |
| 183 | +|\-|说明| |
| 184 | +|------:|------| |
| 185 | +|**语法**|**memcached_send_timeout** `time`;| |
| 186 | +|**默认**|memcached_send_timeout 60s;| |
| 187 | +|**上下文**|http、server、location| |
| 188 | + |
| 189 | +设置将请求传输到 memcached 服务器的超时时间。超时只在两次连续写入操作之间设置,而不是用于传输整个请求。如果 memcached 服务器在此时间内没有收到任何内容,则连接关闭。 |
| 190 | + |
| 191 | +<a id="embedded_variables"></a> |
| 192 | + |
| 193 | +## 内嵌变量 |
| 194 | + |
| 195 | +- `$memcached_key` |
| 196 | + |
| 197 | + 定义从 memcached 服务器获取响应的密钥 |
| 198 | + |
| 199 | +## 原文档 |
| 200 | +[http://nginx.org/en/docs/http/ngx_http_memcached_module.html](http://nginx.org/en/docs/http/ngx_http_memcached_module.html) |
0 commit comments