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
|
550
|
179 if (ngx_http_upstream_create(r) != NGX_OK) {
|
130
|
180 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
181 }
|
|
182
|
550
|
183 u = r->upstream;
|
|
184
|
459
|
185 u->schema.len = sizeof("memcached://") - 1;
|
|
186 u->schema.data = (u_char *) "memcached://";
|
348
|
187
|
550
|
188 u->output.tag = (ngx_buf_tag_t) &ngx_http_memcached_module;
|
130
|
189
|
550
|
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
|
560
|
214 r->main->count++;
|
|
215
|
130
|
216 ngx_http_upstream_init(r);
|
|
217
|
|
218 return NGX_DONE;
|
|
219 }
|
|
220
|
|
221
|
|
222 static ngx_int_t
|
|
223 ngx_http_memcached_create_request(ngx_http_request_t *r)
|
|
224 {
|
278
|
225 size_t len;
|
322
|
226 uintptr_t escape;
|
278
|
227 ngx_buf_t *b;
|
|
228 ngx_chain_t *cl;
|
|
229 ngx_http_memcached_ctx_t *ctx;
|
|
230 ngx_http_variable_value_t *vv;
|
|
231 ngx_http_memcached_loc_conf_t *mlcf;
|
|
232
|
|
233 mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module);
|
130
|
234
|
278
|
235 vv = ngx_http_get_indexed_variable(r, mlcf->index);
|
|
236
|
|
237 if (vv == NULL || vv->not_found || vv->len == 0) {
|
|
238 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
239 "the \"$memcached_key\" variable is not set");
|
|
240 return NGX_ERROR;
|
|
241 }
|
|
242
|
322
|
243 escape = 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_MEMCACHED);
|
|
244
|
|
245 len = sizeof("get ") - 1 + vv->len + escape + sizeof(CRLF) - 1;
|
130
|
246
|
|
247 b = ngx_create_temp_buf(r->pool, len);
|
|
248 if (b == NULL) {
|
|
249 return NGX_ERROR;
|
|
250 }
|
|
251
|
|
252 cl = ngx_alloc_chain_link(r->pool);
|
|
253 if (cl == NULL) {
|
|
254 return NGX_ERROR;
|
|
255 }
|
|
256
|
|
257 cl->buf = b;
|
|
258 cl->next = NULL;
|
|
259
|
|
260 r->upstream->request_bufs = cl;
|
|
261
|
|
262 *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' ';
|
|
263
|
146
|
264 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module);
|
|
265
|
|
266 ctx->key.data = b->last;
|
|
267
|
322
|
268 if (escape == 0) {
|
|
269 b->last = ngx_copy(b->last, vv->data, vv->len);
|
|
270
|
|
271 } else {
|
|
272 b->last = (u_char *) ngx_escape_uri(b->last, vv->data, vv->len,
|
|
273 NGX_ESCAPE_MEMCACHED);
|
|
274 }
|
130
|
275
|
146
|
276 ctx->key.len = b->last - ctx->key.data;
|
130
|
277
|
|
278 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
146
|
279 "http memcached request: \"%V\"", &ctx->key);
|
130
|
280
|
282
|
281 *b->last++ = CR; *b->last++ = LF;
|
130
|
282
|
|
283 return NGX_OK;
|
|
284 }
|
|
285
|
|
286
|
|
287 static ngx_int_t
|
|
288 ngx_http_memcached_reinit_request(ngx_http_request_t *r)
|
|
289 {
|
|
290 return NGX_OK;
|
|
291 }
|
|
292
|
|
293
|
|
294 static ngx_int_t
|
|
295 ngx_http_memcached_process_header(ngx_http_request_t *r)
|
|
296 {
|
146
|
297 u_char *p, *len;
|
|
298 ngx_str_t line;
|
|
299 ngx_http_upstream_t *u;
|
|
300 ngx_http_memcached_ctx_t *ctx;
|
130
|
301
|
|
302 u = r->upstream;
|
|
303
|
|
304 for (p = u->buffer.pos; p < u->buffer.last; p++) {
|
|
305 if (*p == LF) {
|
|
306 goto found;
|
|
307 }
|
|
308 }
|
|
309
|
|
310 return NGX_AGAIN;
|
|
311
|
|
312 found:
|
|
313
|
|
314 *p = '\0';
|
|
315
|
|
316 line.len = p - u->buffer.pos - 1;
|
|
317 line.data = u->buffer.pos;
|
|
318
|
|
319 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
320 "memcached: \"%V\"", &line);
|
|
321
|
|
322 p = u->buffer.pos;
|
|
323
|
146
|
324 ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_module);
|
|
325
|
130
|
326 if (ngx_strncmp(p, "VALUE ", sizeof("VALUE ") - 1) == 0) {
|
|
327
|
|
328 p += sizeof("VALUE ") - 1;
|
|
329
|
146
|
330 if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) {
|
130
|
331 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
332 "memcached sent invalid key in response \"%V\" "
|
|
333 "for key \"%V\"",
|
146
|
334 &line, &ctx->key);
|
130
|
335
|
|
336 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
337 }
|
|
338
|
146
|
339 p += ctx->key.len;
|
130
|
340
|
|
341 if (*p++ != ' ') {
|
|
342 goto no_valid;
|
|
343 }
|
|
344
|
|
345 /* skip flags */
|
|
346
|
|
347 while (*p) {
|
|
348 if (*p++ == ' ') {
|
|
349 goto length;
|
|
350 }
|
|
351 }
|
|
352
|
|
353 goto no_valid;
|
|
354
|
|
355 length:
|
|
356
|
|
357 len = p;
|
|
358
|
|
359 while (*p && *p++ != CR) { /* void */ }
|
|
360
|
|
361 r->headers_out.content_length_n = ngx_atoof(len, p - len - 1);
|
|
362 if (r->headers_out.content_length_n == -1) {
|
|
363 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
364 "memcached sent invalid length in response \"%V\" "
|
|
365 "for key \"%V\"",
|
146
|
366 &line, &ctx->key);
|
130
|
367 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
368 }
|
|
369
|
|
370 u->headers_in.status_n = 200;
|
340
|
371 u->state->status = 200;
|
130
|
372 u->buffer.pos = p + 1;
|
|
373
|
|
374 return NGX_OK;
|
|
375 }
|
|
376
|
|
377 if (ngx_strcmp(p, "END\x0d") == 0) {
|
|
378 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
146
|
379 "key: \"%V\" was not found by memcached", &ctx->key);
|
130
|
380
|
|
381 u->headers_in.status_n = 404;
|
340
|
382 u->state->status = 404;
|
130
|
383
|
|
384 return NGX_OK;
|
|
385 }
|
|
386
|
|
387 no_valid:
|
|
388
|
|
389 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
390 "memcached sent invalid response: \"%V\"", &line);
|
|
391
|
|
392 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
393 }
|
|
394
|
|
395
|
|
396 static ngx_int_t
|
|
397 ngx_http_memcached_filter_init(void *data)
|
|
398 {
|
|
399 ngx_http_memcached_ctx_t *ctx = data;
|
|
400
|
|
401 ngx_http_upstream_t *u;
|
|
402
|
|
403 u = ctx->request->upstream;
|
|
404
|
|
405 u->length += NGX_HTTP_MEMCACHED_END;
|
|
406
|
|
407 return NGX_OK;
|
|
408 }
|
|
409
|
|
410
|
|
411 static ngx_int_t
|
|
412 ngx_http_memcached_filter(void *data, ssize_t bytes)
|
|
413 {
|
|
414 ngx_http_memcached_ctx_t *ctx = data;
|
|
415
|
|
416 u_char *last;
|
|
417 ngx_buf_t *b;
|
|
418 ngx_chain_t *cl, **ll;
|
|
419 ngx_http_upstream_t *u;
|
|
420
|
|
421 u = ctx->request->upstream;
|
|
422 b = &u->buffer;
|
|
423
|
|
424 if (u->length == ctx->rest) {
|
|
425
|
|
426 if (ngx_strncmp(b->last,
|
340
|
427 ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest,
|
|
428 ctx->rest)
|
|
429 != 0)
|
130
|
430 {
|
|
431 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
|
|
432 "memcached sent invalid trailer");
|
|
433 }
|
|
434
|
340
|
435 u->length = 0;
|
|
436 ctx->rest = 0;
|
130
|
437
|
|
438 return NGX_OK;
|
|
439 }
|
|
440
|
|
441 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
442 ll = &cl->next;
|
|
443 }
|
|
444
|
|
445 cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs);
|
|
446 if (cl == NULL) {
|
|
447 return NGX_ERROR;
|
|
448 }
|
|
449
|
|
450 cl->buf->flush = 1;
|
|
451 cl->buf->memory = 1;
|
|
452
|
|
453 *ll = cl;
|
|
454
|
340
|
455 last = b->last;
|
|
456 cl->buf->pos = last;
|
130
|
457 b->last += bytes;
|
|
458 cl->buf->last = b->last;
|
366
|
459 cl->buf->tag = u->output.tag;
|
130
|
460
|
|
461 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
462 "memcached filter bytes:%z size:%z length:%z rest:%z",
|
|
463 bytes, b->last - b->pos, u->length, ctx->rest);
|
|
464
|
340
|
465 if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) {
|
130
|
466 u->length -= bytes;
|
|
467 return NGX_OK;
|
|
468 }
|
|
469
|
340
|
470 last += u->length - NGX_HTTP_MEMCACHED_END;
|
130
|
471
|
|
472 if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) {
|
|
473 ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,
|
|
474 "memcached sent invalid trailer");
|
|
475 }
|
|
476
|
340
|
477 ctx->rest -= b->last - last;
|
130
|
478 b->last = last;
|
|
479 cl->buf->last = last;
|
|
480 u->length = ctx->rest;
|
|
481
|
|
482 return NGX_OK;
|
|
483 }
|
|
484
|
|
485
|
|
486 static void
|
|
487 ngx_http_memcached_abort_request(ngx_http_request_t *r)
|
|
488 {
|
|
489 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
490 "abort http memcached request");
|
|
491 return;
|
|
492 }
|
|
493
|
|
494
|
|
495 static void
|
|
496 ngx_http_memcached_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
|
497 {
|
|
498 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
499 "finalize http memcached request");
|
|
500 return;
|
|
501 }
|
|
502
|
|
503
|
|
504 static void *
|
|
505 ngx_http_memcached_create_loc_conf(ngx_conf_t *cf)
|
|
506 {
|
|
507 ngx_http_memcached_loc_conf_t *conf;
|
|
508
|
|
509 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memcached_loc_conf_t));
|
|
510 if (conf == NULL) {
|
536
|
511 return NULL;
|
130
|
512 }
|
|
513
|
|
514 /*
|
|
515 * set by ngx_pcalloc():
|
|
516 *
|
|
517 * conf->upstream.bufs.num = 0;
|
|
518 * conf->upstream.next_upstream = 0;
|
|
519 * conf->upstream.temp_path = NULL;
|
|
520 * conf->upstream.uri = { 0, NULL };
|
|
521 * conf->upstream.location = NULL;
|
|
522 */
|
|
523
|
|
524 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
525 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
526 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
527
|
|
528 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
|
529
|
260
|
530 /* the hardcoded values */
|
130
|
531 conf->upstream.cyclic_temp_file = 0;
|
178
|
532 conf->upstream.buffering = 0;
|
|
533 conf->upstream.ignore_client_abort = 0;
|
130
|
534 conf->upstream.send_lowat = 0;
|
|
535 conf->upstream.bufs.num = 0;
|
|
536 conf->upstream.busy_buffers_size = 0;
|
|
537 conf->upstream.max_temp_file_size = 0;
|
|
538 conf->upstream.temp_file_write_size = 0;
|
206
|
539 conf->upstream.intercept_errors = 1;
|
224
|
540 conf->upstream.intercept_404 = 1;
|
130
|
541 conf->upstream.pass_request_headers = 0;
|
|
542 conf->upstream.pass_request_body = 0;
|
|
543
|
358
|
544 conf->index = NGX_CONF_UNSET;
|
|
545
|
130
|
546 return conf;
|
|
547 }
|
|
548
|
|
549
|
|
550 static char *
|
|
551 ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
552 {
|
|
553 ngx_http_memcached_loc_conf_t *prev = parent;
|
|
554 ngx_http_memcached_loc_conf_t *conf = child;
|
|
555
|
|
556 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
557 prev->upstream.connect_timeout, 60000);
|
|
558
|
|
559 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
560 prev->upstream.send_timeout, 60000);
|
|
561
|
|
562 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
563 prev->upstream.read_timeout, 60000);
|
|
564
|
|
565 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
566 prev->upstream.buffer_size,
|
|
567 (size_t) ngx_pagesize);
|
|
568
|
|
569 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
570 prev->upstream.next_upstream,
|
|
571 (NGX_CONF_BITMASK_SET
|
|
572 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
573 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
574
|
214
|
575 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
576 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
577 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
578 }
|
|
579
|
358
|
580 if (conf->upstream.upstream == NULL) {
|
|
581 conf->upstream.upstream = prev->upstream.upstream;
|
|
582 }
|
|
583
|
|
584 if (conf->index == NGX_CONF_UNSET) {
|
|
585 conf->index = prev->index;
|
|
586 }
|
|
587
|
130
|
588 return NGX_CONF_OK;
|
|
589 }
|
|
590
|
|
591
|
|
592 static char *
|
|
593 ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
594 {
|
459
|
595 ngx_http_memcached_loc_conf_t *mlcf = conf;
|
130
|
596
|
|
597 ngx_str_t *value;
|
252
|
598 ngx_url_t u;
|
130
|
599 ngx_http_core_loc_conf_t *clcf;
|
|
600
|
459
|
601 if (mlcf->upstream.upstream) {
|
130
|
602 return "is duplicate";
|
|
603 }
|
|
604
|
|
605 value = cf->args->elts;
|
|
606
|
252
|
607 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
608
|
|
609 u.url = value[1];
|
260
|
610 u.no_resolve = 1;
|
130
|
611
|
459
|
612 mlcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
613 if (mlcf->upstream.upstream == NULL) {
|
260
|
614 return NGX_CONF_ERROR;
|
130
|
615 }
|
|
616
|
|
617 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
618
|
|
619 clcf->handler = ngx_http_memcached_handler;
|
|
620
|
|
621 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
622 clcf->auto_redirect = 1;
|
|
623 }
|
|
624
|
459
|
625 mlcf->index = ngx_http_get_variable_index(cf, &ngx_http_memcached_key);
|
278
|
626
|
459
|
627 if (mlcf->index == NGX_ERROR) {
|
278
|
628 return NGX_CONF_ERROR;
|
|
629 }
|
|
630
|
130
|
631 return NGX_CONF_OK;
|
|
632 }
|
260
|
633
|
|
634
|
|
635 static char *
|
|
636 ngx_http_memcached_upstream_max_fails_unsupported(ngx_conf_t *cf,
|
|
637 ngx_command_t *cmd, void *conf)
|
|
638 {
|
|
639 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
640 "\"memcached_upstream_max_fails\" is not supported, "
|
|
641 "use the \"max_fails\" parameter of the \"server\" directive ",
|
|
642 "inside the \"upstream\" block");
|
|
643
|
|
644 return NGX_CONF_ERROR;
|
|
645 }
|
|
646
|
|
647
|
|
648 static char *
|
|
649 ngx_http_memcached_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
|
|
650 ngx_command_t *cmd, void *conf)
|
|
651 {
|
|
652 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
653 "\"memcached_upstream_fail_timeout\" is not supported, "
|
|
654 "use the \"fail_timeout\" parameter of the \"server\" directive ",
|
|
655 "inside the \"upstream\" block");
|
|
656
|
|
657 return NGX_CONF_ERROR;
|
|
658 }
|