130
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 ngx_http_upstream_conf_t upstream;
|
278
|
14 ngx_int_t index;
|
130
|
15 } ngx_http_memcached_loc_conf_t;
|
|
16
|
|
17
|
|
18 typedef struct {
|
|
19 size_t rest;
|
|
20 ngx_http_request_t *request;
|
146
|
21 ngx_str_t key;
|
130
|
22 } ngx_http_memcached_ctx_t;
|
|
23
|
|
24
|
|
25 static ngx_int_t ngx_http_memcached_create_request(ngx_http_request_t *r);
|
|
26 static ngx_int_t ngx_http_memcached_reinit_request(ngx_http_request_t *r);
|
|
27 static ngx_int_t ngx_http_memcached_process_header(ngx_http_request_t *r);
|
|
28 static ngx_int_t ngx_http_memcached_filter_init(void *data);
|
|
29 static ngx_int_t ngx_http_memcached_filter(void *data, ssize_t bytes);
|
|
30 static void ngx_http_memcached_abort_request(ngx_http_request_t *r);
|
|
31 static void ngx_http_memcached_finalize_request(ngx_http_request_t *r,
|
|
32 ngx_int_t rc);
|
|
33
|
|
34 static void *ngx_http_memcached_create_loc_conf(ngx_conf_t *cf);
|
|
35 static char *ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf,
|
|
36 void *parent, void *child);
|
|
37
|
|
38 static char *ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
39 void *conf);
|
|
40
|
260
|
41 static char *ngx_http_memcached_upstream_max_fails_unsupported(ngx_conf_t *cf,
|
|
42 ngx_command_t *cmd, void *conf);
|
|
43 static char *ngx_http_memcached_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
|
|
44 ngx_command_t *cmd, void *conf);
|
|
45
|
130
|
46
|
|
47 static ngx_conf_bitmask_t ngx_http_memcached_next_upstream_masks[] = {
|
|
48 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
|
|
49 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
|
|
50 { ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
|
|
51 { ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
|
214
|
52 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
|
130
|
53 { ngx_null_string, 0 }
|
|
54 };
|
|
55
|
|
56
|
|
57 static ngx_command_t ngx_http_memcached_commands[] = {
|
|
58
|
|
59 { ngx_string("memcached_pass"),
|
358
|
60 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
|
130
|
61 ngx_http_memcached_pass,
|
|
62 NGX_HTTP_LOC_CONF_OFFSET,
|
|
63 0,
|
|
64 NULL },
|
|
65
|
|
66 { ngx_string("memcached_connect_timeout"),
|
|
67 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
68 ngx_conf_set_msec_slot,
|
|
69 NGX_HTTP_LOC_CONF_OFFSET,
|
|
70 offsetof(ngx_http_memcached_loc_conf_t, upstream.connect_timeout),
|
|
71 NULL },
|
|
72
|
|
73 { ngx_string("memcached_send_timeout"),
|
|
74 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
75 ngx_conf_set_msec_slot,
|
|
76 NGX_HTTP_LOC_CONF_OFFSET,
|
|
77 offsetof(ngx_http_memcached_loc_conf_t, upstream.send_timeout),
|
|
78 NULL },
|
|
79
|
|
80 { ngx_string("memcached_buffer_size"),
|
|
81 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
82 ngx_conf_set_size_slot,
|
|
83 NGX_HTTP_LOC_CONF_OFFSET,
|
|
84 offsetof(ngx_http_memcached_loc_conf_t, upstream.buffer_size),
|
|
85 NULL },
|
|
86
|
|
87 { ngx_string("memcached_read_timeout"),
|
|
88 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
89 ngx_conf_set_msec_slot,
|
|
90 NGX_HTTP_LOC_CONF_OFFSET,
|
|
91 offsetof(ngx_http_memcached_loc_conf_t, upstream.read_timeout),
|
|
92 NULL },
|
|
93
|
|
94 { ngx_string("memcached_next_upstream"),
|
|
95 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
96 ngx_conf_set_bitmask_slot,
|
|
97 NGX_HTTP_LOC_CONF_OFFSET,
|
|
98 offsetof(ngx_http_memcached_loc_conf_t, upstream.next_upstream),
|
|
99 &ngx_http_memcached_next_upstream_masks },
|
|
100
|
|
101 { ngx_string("memcached_upstream_max_fails"),
|
|
102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
260
|
103 ngx_http_memcached_upstream_max_fails_unsupported,
|
|
104 0,
|
|
105 0,
|
130
|
106 NULL },
|
|
107
|
|
108 { ngx_string("memcached_upstream_fail_timeout"),
|
|
109 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
260
|
110 ngx_http_memcached_upstream_fail_timeout_unsupported,
|
|
111 0,
|
|
112 0,
|
130
|
113 NULL },
|
|
114
|
|
115 ngx_null_command
|
|
116 };
|
|
117
|
|
118
|
216
|
119 static ngx_http_module_t ngx_http_memcached_module_ctx = {
|
130
|
120 NULL, /* preconfiguration */
|
|
121 NULL, /* postconfiguration */
|
|
122
|
|
123 NULL, /* create main configuration */
|
|
124 NULL, /* init main configuration */
|
|
125
|
|
126 NULL, /* create server configuration */
|
|
127 NULL, /* merge server configuration */
|
|
128
|
|
129 ngx_http_memcached_create_loc_conf, /* create location configration */
|
|
130 ngx_http_memcached_merge_loc_conf /* merge location configration */
|
|
131 };
|
|
132
|
|
133
|
|
134 ngx_module_t ngx_http_memcached_module = {
|
|
135 NGX_MODULE_V1,
|
|
136 &ngx_http_memcached_module_ctx, /* module context */
|
|
137 ngx_http_memcached_commands, /* module directives */
|
|
138 NGX_HTTP_MODULE, /* module type */
|
|
139 NULL, /* init master */
|
|
140 NULL, /* init module */
|
|
141 NULL, /* init process */
|
|
142 NULL, /* init thread */
|
|
143 NULL, /* exit thread */
|
|
144 NULL, /* exit process */
|
|
145 NULL, /* exit master */
|
|
146 NGX_MODULE_V1_PADDING
|
|
147 };
|
|
148
|
|
149
|
278
|
150 static ngx_str_t ngx_http_memcached_key = ngx_string("memcached_key");
|
|
151
|
|
152
|
130
|
153 #define NGX_HTTP_MEMCACHED_END (sizeof(ngx_http_memcached_end) - 1)
|
|
154 static u_char ngx_http_memcached_end[] = CRLF "END" CRLF;
|
|
155
|
|
156
|
|
157 static ngx_int_t
|
|
158 ngx_http_memcached_handler(ngx_http_request_t *r)
|
|
159 {
|
|
160 ngx_int_t rc;
|
|
161 ngx_http_upstream_t *u;
|
|
162 ngx_http_memcached_ctx_t *ctx;
|
|
163 ngx_http_memcached_loc_conf_t *mlcf;
|
|
164
|
194
|
165 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
|
130
|
166 return NGX_HTTP_NOT_ALLOWED;
|
|
167 }
|
|
168
|
326
|
169 rc = ngx_http_discard_request_body(r);
|
130
|
170
|
326
|
171 if (rc != NGX_OK) {
|
130
|
172 return rc;
|
|
173 }
|
|
174
|
|
175 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
176 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
177 }
|
|
178
|
510
|
179 if (ngx_http_upstream_create(r) != NGX_OK) {
|
130
|
180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
181 }
|
|
182
|
510
|
183 u = r->upstream;
|
|
184
|
430
|
185 u->schema.len = sizeof("memcached://") - 1;
|
|
186 u->schema.data = (u_char *) "memcached://";
|
348
|
187
|
510
|
188 u->output.tag = (ngx_buf_tag_t) &ngx_http_memcached_module;
|
130
|
189
|
510
|
190 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module);
|
130
|
191
|
|
192 u->conf = &mlcf->upstream;
|
|
193
|
|
194 u->create_request = ngx_http_memcached_create_request;
|
|
195 u->reinit_request = ngx_http_memcached_reinit_request;
|
|
196 u->process_header = ngx_http_memcached_process_header;
|
|
197 u->abort_request = ngx_http_memcached_abort_request;
|
|
198 u->finalize_request = ngx_http_memcached_finalize_request;
|
|
199
|
|
200 ctx = ngx_palloc(r->pool, sizeof(ngx_http_memcached_ctx_t));
|
|
201 if (ctx == NULL) {
|
|
202 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
203 }
|
|
204
|
|
205 ctx->rest = NGX_HTTP_MEMCACHED_END;
|
|
206 ctx->request = r;
|
|
207
|
146
|
208 ngx_http_set_ctx(r, ctx, ngx_http_memcached_module);
|
|
209
|
130
|
210 u->input_filter_init = ngx_http_memcached_filter_init;
|
|
211 u->input_filter = ngx_http_memcached_filter;
|
|
212 u->input_filter_ctx = ctx;
|
|
213
|
|
214 ngx_http_upstream_init(r);
|
|
215
|
|
216 return NGX_DONE;
|
|
217 }
|
|
218
|
|
219
|
|
220 static ngx_int_t
|
|
221 ngx_http_memcached_create_request(ngx_http_request_t *r)
|
|
222 {
|
278
|
223 size_t len;
|
322
|
224 uintptr_t escape;
|
278
|
225 ngx_buf_t *b;
|
|
226 ngx_chain_t *cl;
|
|
227 ngx_http_memcached_ctx_t *ctx;
|
|
228 ngx_http_variable_value_t *vv;
|
|
229 ngx_http_memcached_loc_conf_t *mlcf;
|
|
230
|
|
231 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module);
|
130
|
232
|
278
|
233 vv = ngx_http_get_indexed_variable(r, mlcf->index);
|
|
234
|
|
235 if (vv == NULL || vv->not_found || vv->len == 0) {
|
|
236 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
237 "the \"$memcached_key\" variable is not set");
|
|
238 return NGX_ERROR;
|
|
239 }
|
|
240
|
322
|
241 escape = 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_MEMCACHED);
|
|
242
|
|
243 len = sizeof("get ") - 1 + vv->len + escape + sizeof(CRLF) - 1;
|
130
|
244
|
|
245 b = ngx_create_temp_buf(r->pool, len);
|
|
246 if (b == NULL) {
|
|
247 return NGX_ERROR;
|
|
248 }
|
|
249
|
|
250 cl = ngx_alloc_chain_link(r->pool);
|
|
251 if (cl == NULL) {
|
|
252 return NGX_ERROR;
|
|
253 }
|
|
254
|
|
255 cl->buf = b;
|
|
256 cl->next = NULL;
|
|
257
|
|
258 r->upstream->request_bufs = cl;
|
|
259
|
|
260 *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' ';
|
|
261
|
146
|
262 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module);
|
|
263
|
|
264 ctx->key.data = b->last;
|
|
265
|
322
|
266 if (escape == 0) {
|
|
267 b->last = ngx_copy(b->last, vv->data, vv->len);
|
|
268
|
|
269 } else {
|
|
270 b->last = (u_char *) ngx_escape_uri(b->last, vv->data, vv->len,
|
|
271 NGX_ESCAPE_MEMCACHED);
|
|
272 }
|
130
|
273
|
146
|
274 ctx->key.len = b->last - ctx->key.data;
|
130
|
275
|
|
276 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
146
|
277 "http memcached request: \"%V\"", &ctx->key);
|
130
|
278
|
282
|
279 *b->last++ = CR; *b->last++ = LF;
|
130
|
280
|
|
281 return NGX_OK;
|
|
282 }
|
|
283
|
|
284
|
|
285 static ngx_int_t
|
|
286 ngx_http_memcached_reinit_request(ngx_http_request_t *r)
|
|
287 {
|
|
288 return NGX_OK;
|
|
289 }
|
|
290
|
|
291
|
|
292 static ngx_int_t
|
|
293 ngx_http_memcached_process_header(ngx_http_request_t *r)
|
|
294 {
|
146
|
295 u_char *p, *len;
|
|
296 ngx_str_t line;
|
|
297 ngx_http_upstream_t *u;
|
|
298 ngx_http_memcached_ctx_t *ctx;
|
130
|
299
|
|
300 u = r->upstream;
|
|
301
|
|
302 for (p = u->buffer.pos; p < u->buffer.last; p++) {
|
|
303 if (*p == LF) {
|
|
304 goto found;
|
|
305 }
|
|
306 }
|
|
307
|
|
308 return NGX_AGAIN;
|
|
309
|
|
310 found:
|
|
311
|
|
312 *p = '\0';
|
|
313
|
|
314 line.len = p - u->buffer.pos - 1;
|
|
315 line.data = u->buffer.pos;
|
|
316
|
|
317 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
318 "memcached: \"%V\"", &line);
|
|
319
|
|
320 p = u->buffer.pos;
|
|
321
|
146
|
322 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module);
|
|
323
|
130
|
324 if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) {
|
|
325
|
|
326 p += sizeof("VALUE ") - 1;
|
|
327
|
146
|
328 if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) {
|
130
|
329 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
330 "memcached sent invalid key in response \"%V\" "
|
|
331 "for key \"%V\"",
|
146
|
332 &line, &ctx->key);
|
130
|
333
|
|
334 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
335 }
|
|
336
|
146
|
337 p += ctx->key.len;
|
130
|
338
|
|
339 if (*p++ != ' ') {
|
|
340 goto no_valid;
|
|
341 }
|
|
342
|
|
343 /* skip flags */
|
|
344
|
|
345 while (*p) {
|
|
346 if (*p++ == ' ') {
|
|
347 goto length;
|
|
348 }
|
|
349 }
|
|
350
|
|
351 goto no_valid;
|
|
352
|
|
353 length:
|
|
354
|
|
355 len = p;
|
|
356
|
|
357 while (*p && *p++ != CR) { /* void */ }
|
|
358
|
|
359 r->headers_out.content_length_n = ngx_atoof(len, p - len - 1);
|
|
360 if (r->headers_out.content_length_n == -1) {
|
|
361 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
362 "memcached sent invalid length in response \"%V\" "
|
|
363 "for key \"%V\"",
|
146
|
364 &line, &ctx->key);
|
130
|
365 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
366 }
|
|
367
|
|
368 u->headers_in.status_n = 200;
|
340
|
369 u->state->status = 200;
|
130
|
370 u->buffer.pos = p + 1;
|
|
371
|
|
372 return NGX_OK;
|
|
373 }
|
|
374
|
|
375 if (ngx_strcmp(p, "END\x0d") == 0) {
|
|
376 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
146
|
377 "key: \"%V\" was not found by memcached", &ctx->key);
|
130
|
378
|
|
379 u->headers_in.status_n = 404;
|
340
|
380 u->state->status = 404;
|
130
|
381
|
|
382 return NGX_OK;
|
|
383 }
|
|
384
|
|
385 no_valid:
|
|
386
|
|
387 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
388 "memcached sent invalid response: \"%V\"", &line);
|
|
389
|
|
390 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
391 }
|
|
392
|
|
393
|
|
394 static ngx_int_t
|
|
395 ngx_http_memcached_filter_init(void *data)
|
|
396 {
|
|
397 ngx_http_memcached_ctx_t *ctx = data;
|
|
398
|
|
399 ngx_http_upstream_t *u;
|
|
400
|
|
401 u = ctx->request->upstream;
|
|
402
|
|
403 u->length += NGX_HTTP_MEMCACHED_END;
|
|
404
|
|
405 return NGX_OK;
|
|
406 }
|
|
407
|
|
408
|
|
409 static ngx_int_t
|
|
410 ngx_http_memcached_filter(void *data, ssize_t bytes)
|
|
411 {
|
|
412 ngx_http_memcached_ctx_t *ctx = data;
|
|
413
|
|
414 u_char *last;
|
|
415 ngx_buf_t *b;
|
|
416 ngx_chain_t *cl, **ll;
|
|
417 ngx_http_upstream_t *u;
|
|
418
|
|
419 u = ctx->request->upstream;
|
|
420 b = &u->buffer;
|
|
421
|
|
422 if (u->length == ctx->rest) {
|
|
423
|
|
424 if (ngx_strncmp(b->last,
|
340
|
425 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest,
|
|
426 ctx->rest)
|
|
427 != 0)
|
130
|
428 {
|
|
429 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
|
|
430 "memcached sent invalid trailer");
|
|
431 }
|
|
432
|
340
|
433 u->length = 0;
|
|
434 ctx->rest = 0;
|
130
|
435
|
|
436 return NGX_OK;
|
|
437 }
|
|
438
|
|
439 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
440 ll = &cl->next;
|
|
441 }
|
|
442
|
|
443 cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs);
|
|
444 if (cl == NULL) {
|
|
445 return NGX_ERROR;
|
|
446 }
|
|
447
|
|
448 cl->buf->flush = 1;
|
|
449 cl->buf->memory = 1;
|
|
450
|
|
451 *ll = cl;
|
|
452
|
340
|
453 last = b->last;
|
|
454 cl->buf->pos = last;
|
130
|
455 b->last += bytes;
|
|
456 cl->buf->last = b->last;
|
366
|
457 cl->buf->tag = u->output.tag;
|
130
|
458
|
|
459 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
460 "memcached filter bytes:%z size:%z length:%z rest:%z",
|
|
461 bytes, b->last - b->pos, u->length, ctx->rest);
|
|
462
|
340
|
463 if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) {
|
130
|
464 u->length -= bytes;
|
|
465 return NGX_OK;
|
|
466 }
|
|
467
|
340
|
468 last += u->length - NGX_HTTP_MEMCACHED_END;
|
130
|
469
|
|
470 if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) {
|
|
471 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
|
|
472 "memcached sent invalid trailer");
|
|
473 }
|
|
474
|
340
|
475 ctx->rest -= b->last - last;
|
130
|
476 b->last = last;
|
|
477 cl->buf->last = last;
|
|
478 u->length = ctx->rest;
|
|
479
|
|
480 return NGX_OK;
|
|
481 }
|
|
482
|
|
483
|
|
484 static void
|
|
485 ngx_http_memcached_abort_request(ngx_http_request_t *r)
|
|
486 {
|
|
487 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
488 "abort http memcached request");
|
|
489 return;
|
|
490 }
|
|
491
|
|
492
|
|
493 static void
|
|
494 ngx_http_memcached_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
|
495 {
|
|
496 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
497 "finalize http memcached request");
|
|
498 return;
|
|
499 }
|
|
500
|
|
501
|
|
502 static void *
|
|
503 ngx_http_memcached_create_loc_conf(ngx_conf_t *cf)
|
|
504 {
|
|
505 ngx_http_memcached_loc_conf_t *conf;
|
|
506
|
|
507 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memcached_loc_conf_t));
|
|
508 if (conf == NULL) {
|
496
|
509 return NULL;
|
130
|
510 }
|
|
511
|
|
512 /*
|
|
513 * set by ngx_pcalloc():
|
|
514 *
|
|
515 * conf->upstream.bufs.num = 0;
|
|
516 * conf->upstream.next_upstream = 0;
|
|
517 * conf->upstream.temp_path = NULL;
|
|
518 * conf->upstream.uri = { 0, NULL };
|
|
519 * conf->upstream.location = NULL;
|
|
520 */
|
|
521
|
|
522 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
523 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
524 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
525
|
|
526 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
|
527
|
260
|
528 /* the hardcoded values */
|
130
|
529 conf->upstream.cyclic_temp_file = 0;
|
178
|
530 conf->upstream.buffering = 0;
|
|
531 conf->upstream.ignore_client_abort = 0;
|
130
|
532 conf->upstream.send_lowat = 0;
|
|
533 conf->upstream.bufs.num = 0;
|
|
534 conf->upstream.busy_buffers_size = 0;
|
|
535 conf->upstream.max_temp_file_size = 0;
|
|
536 conf->upstream.temp_file_write_size = 0;
|
206
|
537 conf->upstream.intercept_errors = 1;
|
224
|
538 conf->upstream.intercept_404 = 1;
|
130
|
539 conf->upstream.pass_request_headers = 0;
|
|
540 conf->upstream.pass_request_body = 0;
|
|
541
|
358
|
542 conf->index = NGX_CONF_UNSET;
|
|
543
|
130
|
544 return conf;
|
|
545 }
|
|
546
|
|
547
|
|
548 static char *
|
|
549 ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
550 {
|
|
551 ngx_http_memcached_loc_conf_t *prev = parent;
|
|
552 ngx_http_memcached_loc_conf_t *conf = child;
|
|
553
|
|
554 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
555 prev->upstream.connect_timeout, 60000);
|
|
556
|
|
557 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
558 prev->upstream.send_timeout, 60000);
|
|
559
|
|
560 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
561 prev->upstream.read_timeout, 60000);
|
|
562
|
|
563 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
564 prev->upstream.buffer_size,
|
|
565 (size_t) ngx_pagesize);
|
|
566
|
|
567 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
568 prev->upstream.next_upstream,
|
|
569 (NGX_CONF_BITMASK_SET
|
|
570 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
571 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
572
|
214
|
573 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
574 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
575 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
576 }
|
|
577
|
358
|
578 if (conf->upstream.upstream == NULL) {
|
|
579 conf->upstream.upstream = prev->upstream.upstream;
|
|
580 }
|
|
581
|
|
582 if (conf->index == NGX_CONF_UNSET) {
|
|
583 conf->index = prev->index;
|
|
584 }
|
|
585
|
130
|
586 return NGX_CONF_OK;
|
|
587 }
|
|
588
|
|
589
|
|
590 static char *
|
|
591 ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
592 {
|
430
|
593 ngx_http_memcached_loc_conf_t *mlcf = conf;
|
130
|
594
|
|
595 ngx_str_t *value;
|
252
|
596 ngx_url_t u;
|
130
|
597 ngx_http_core_loc_conf_t *clcf;
|
|
598
|
430
|
599 if (mlcf->upstream.upstream) {
|
130
|
600 return "is duplicate";
|
|
601 }
|
|
602
|
|
603 value = cf->args->elts;
|
|
604
|
252
|
605 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
606
|
|
607 u.url = value[1];
|
260
|
608 u.no_resolve = 1;
|
130
|
609
|
430
|
610 mlcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
611 if (mlcf->upstream.upstream == NULL) {
|
260
|
612 return NGX_CONF_ERROR;
|
130
|
613 }
|
|
614
|
|
615 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
616
|
|
617 clcf->handler = ngx_http_memcached_handler;
|
|
618
|
|
619 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
620 clcf->auto_redirect = 1;
|
|
621 }
|
|
622
|
430
|
623 mlcf->index = ngx_http_get_variable_index(cf, &ngx_http_memcached_key);
|
278
|
624
|
430
|
625 if (mlcf->index == NGX_ERROR) {
|
278
|
626 return NGX_CONF_ERROR;
|
|
627 }
|
|
628
|
130
|
629 return NGX_CONF_OK;
|
|
630 }
|
260
|
631
|
|
632
|
|
633 static char *
|
|
634 ngx_http_memcached_upstream_max_fails_unsupported(ngx_conf_t *cf,
|
|
635 ngx_command_t *cmd, void *conf)
|
|
636 {
|
|
637 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
638 "\"memcached_upstream_max_fails\" is not supported, "
|
|
639 "use the \"max_fails\" parameter of the \"server\" directive ",
|
|
640 "inside the \"upstream\" block");
|
|
641
|
|
642 return NGX_CONF_ERROR;
|
|
643 }
|
|
644
|
|
645
|
|
646 static char *
|
|
647 ngx_http_memcached_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
|
|
648 ngx_command_t *cmd, void *conf)
|
|
649 {
|
|
650 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
651 "\"memcached_upstream_fail_timeout\" is not supported, "
|
|
652 "use the \"fail_timeout\" parameter of the \"server\" directive ",
|
|
653 "inside the \"upstream\" block");
|
|
654
|
|
655 return NGX_CONF_ERROR;
|
|
656 }
|