comparison src/http/modules/ngx_http_proxy_module.c @ 507:cd3117ad9aab release-0.1.28

nginx-0.1.28-RELEASE import *) Bugfix: nginx hogs CPU while proxying the huge files. *) Bugfix: nginx could not be built by gcc 4.0 on Linux.
author Igor Sysoev <igor@sysoev.ru>
date Fri, 08 Apr 2005 15:18:55 +0000
parents
children 9b8c906f6e63
comparison
equal deleted inserted replaced
506:005e65646622 507:cd3117ad9aab
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_http.h>
11
12
13 typedef struct {
14 ngx_http_upstream_conf_t upstream;
15
16 ngx_peers_t *peers;
17
18 ngx_array_t *headers_set_len;
19 ngx_array_t *headers_set;
20 ngx_hash_t *headers_set_hash;
21
22 ngx_flag_t preserve_host;
23 ngx_flag_t set_x_url;
24 ngx_flag_t set_x_real_ip;
25 ngx_flag_t add_x_forwarded_for;
26 ngx_flag_t pass_server;
27 ngx_flag_t pass_x_accel_expires;
28
29 ngx_str_t *location0;
30
31 ngx_str_t host_header;
32 ngx_str_t uri0;
33
34 ngx_array_t *headers_sources;
35 ngx_array_t *headers_names;
36 } ngx_http_proxy_loc_conf_t;
37
38
39 typedef struct {
40 ngx_list_t headers;
41
42 ngx_table_elt_t *date;
43 ngx_table_elt_t *server;
44
45 ngx_table_elt_t *expires;
46 ngx_table_elt_t *cache_control;
47 ngx_table_elt_t *etag;
48 ngx_table_elt_t *x_accel_expires;
49
50 ngx_table_elt_t *connection;
51 ngx_table_elt_t *content_type;
52 ngx_table_elt_t *content_length;
53
54 #if (NGX_HTTP_GZIP)
55 ngx_table_elt_t *content_encoding;
56 #endif
57
58 ngx_table_elt_t *last_modified;
59 ngx_table_elt_t *location;
60 ngx_table_elt_t *accept_ranges;
61 ngx_table_elt_t *x_pad;
62
63 off_t content_length_n;
64 } ngx_http_proxy_headers_in_t;
65
66
67 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
68 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
69 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
70 static ngx_int_t ngx_http_proxy_send_header(ngx_http_request_t *r);
71 static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
72 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
73 ngx_int_t rc);
74
75 static ngx_int_t ngx_http_proxy_compile_header_start(ngx_table_elt_t *h,
76 ngx_array_t *lengths, ngx_array_t *values, ngx_uint_t value);
77 static ngx_int_t ngx_http_proxy_compile_header_end(ngx_array_t *lengths,
78 ngx_array_t *values);
79
80 static ngx_int_t ngx_http_proxy_init(ngx_cycle_t *cycle);
81 static ngx_http_variable_value_t *ngx_http_proxy_host_variable
82 (ngx_http_request_t *r, uintptr_t data);
83 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
84 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
85 void *parent, void *child);
86
87 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
88 void *conf);
89
90 static char *ngx_http_proxy_set_x_var(ngx_conf_t *cf, ngx_command_t *cmd,
91 void *conf);
92 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
93
94 static ngx_conf_post_t ngx_http_proxy_lowat_post =
95 { ngx_http_proxy_lowat_check };
96
97 static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
98 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
99 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
100 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
101 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
102 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
103 { ngx_null_string, 0 }
104 };
105
106
107 static ngx_command_t ngx_http_proxy_commands[] = {
108
109 { ngx_string("proxy_pass"),
110 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
111 ngx_http_proxy_pass,
112 NGX_HTTP_LOC_CONF_OFFSET,
113 0,
114 NULL },
115
116 { ngx_string("proxy_connect_timeout"),
117 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
118 ngx_conf_set_msec_slot,
119 NGX_HTTP_LOC_CONF_OFFSET,
120 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
121 NULL },
122
123 { ngx_string("proxy_send_timeout"),
124 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
125 ngx_conf_set_msec_slot,
126 NGX_HTTP_LOC_CONF_OFFSET,
127 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
128 NULL },
129
130 { ngx_string("proxy_send_lowat"),
131 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
132 ngx_conf_set_size_slot,
133 NGX_HTTP_LOC_CONF_OFFSET,
134 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
135 &ngx_http_proxy_lowat_post },
136
137 { ngx_string("proxy_pass_unparsed_uri"),
138 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
139 ngx_conf_set_flag_slot,
140 NGX_HTTP_LOC_CONF_OFFSET,
141 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_unparsed_uri),
142 NULL },
143
144 { ngx_string("proxy_preserve_host"),
145 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
146 ngx_conf_set_flag_slot,
147 NGX_HTTP_LOC_CONF_OFFSET,
148 offsetof(ngx_http_proxy_loc_conf_t, preserve_host),
149 NULL },
150
151 { ngx_string("proxy_set_x_url"),
152 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
153 ngx_conf_set_flag_slot,
154 NGX_HTTP_LOC_CONF_OFFSET,
155 offsetof(ngx_http_proxy_loc_conf_t, set_x_url),
156 NULL },
157
158 { ngx_string("proxy_set_x_real_ip"),
159 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
160 ngx_conf_set_flag_slot,
161 NGX_HTTP_LOC_CONF_OFFSET,
162 offsetof(ngx_http_proxy_loc_conf_t, set_x_real_ip),
163 NULL },
164
165 { ngx_string("proxy_set_x_var"),
166 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
167 ngx_http_proxy_set_x_var,
168 NGX_HTTP_LOC_CONF_OFFSET,
169 0,
170 NULL },
171
172 { ngx_string("proxy_add_x_forwarded_for"),
173 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
174 ngx_conf_set_flag_slot,
175 NGX_HTTP_LOC_CONF_OFFSET,
176 offsetof(ngx_http_proxy_loc_conf_t, add_x_forwarded_for),
177 NULL },
178
179 { ngx_string("proxy_header_buffer_size"),
180 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
181 ngx_conf_set_size_slot,
182 NGX_HTTP_LOC_CONF_OFFSET,
183 offsetof(ngx_http_proxy_loc_conf_t, upstream.header_buffer_size),
184 NULL },
185
186 { ngx_string("proxy_read_timeout"),
187 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
188 ngx_conf_set_msec_slot,
189 NGX_HTTP_LOC_CONF_OFFSET,
190 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
191 NULL },
192
193 { ngx_string("proxy_buffers"),
194 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
195 ngx_conf_set_bufs_slot,
196 NGX_HTTP_LOC_CONF_OFFSET,
197 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
198 NULL },
199
200 { ngx_string("proxy_busy_buffers_size"),
201 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
202 ngx_conf_set_size_slot,
203 NGX_HTTP_LOC_CONF_OFFSET,
204 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size),
205 NULL },
206
207 { ngx_string("proxy_temp_path"),
208 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
209 ngx_conf_set_path_slot,
210 NGX_HTTP_LOC_CONF_OFFSET,
211 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
212 (void *) ngx_garbage_collector_temp_handler },
213
214 { ngx_string("proxy_max_temp_file_size"),
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
216 ngx_conf_set_size_slot,
217 NGX_HTTP_LOC_CONF_OFFSET,
218 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size),
219 NULL },
220
221 { ngx_string("proxy_temp_file_write_size"),
222 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
223 ngx_conf_set_size_slot,
224 NGX_HTTP_LOC_CONF_OFFSET,
225 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size),
226 NULL },
227
228 { ngx_string("proxy_next_upstream"),
229 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
230 ngx_conf_set_bitmask_slot,
231 NGX_HTTP_LOC_CONF_OFFSET,
232 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
233 &ngx_http_proxy_next_upstream_masks },
234
235 { ngx_string("proxy_pass_server"),
236 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
237 ngx_conf_set_flag_slot,
238 NGX_HTTP_LOC_CONF_OFFSET,
239 offsetof(ngx_http_proxy_loc_conf_t, pass_server),
240 NULL },
241
242 { ngx_string("proxy_pass_x_accel_expires"),
243 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
244 ngx_conf_set_flag_slot,
245 NGX_HTTP_LOC_CONF_OFFSET,
246 offsetof(ngx_http_proxy_loc_conf_t, pass_x_accel_expires),
247 NULL },
248
249 ngx_null_command
250 };
251
252
253 ngx_http_module_t ngx_http_proxy_module_ctx = {
254 NULL, /* pre conf */
255
256 NULL, /* create main configuration */
257 NULL, /* init main configuration */
258
259 NULL, /* create server configuration */
260 NULL, /* merge server configuration */
261
262 ngx_http_proxy_create_loc_conf, /* create location configration */
263 ngx_http_proxy_merge_loc_conf /* merge location configration */
264 };
265
266
267 ngx_module_t ngx_http_proxy_module = {
268 NGX_MODULE,
269 &ngx_http_proxy_module_ctx, /* module context */
270 ngx_http_proxy_commands, /* module directives */
271 NGX_HTTP_MODULE, /* module type */
272 ngx_http_proxy_init, /* init module */
273 NULL /* init process */
274 };
275
276
277 static ngx_str_t ngx_http_proxy_methods[] = {
278 ngx_string("GET"),
279 ngx_string("HEAD"),
280 ngx_string("POST")
281 };
282
283
284 static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
285
286 static ngx_str_t ngx_http_proxy_host = ngx_string("PROXY_HOST");
287
288
289 #if (NGX_PCRE)
290 static ngx_str_t ngx_http_proxy_uri = ngx_string("/");
291 #endif
292
293
294 #if 0
295
296 ngx_http_header_t ngx_http_proxy_headers_in[] = {
297 { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) },
298 { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) },
299
300 { ngx_string("Expires"), offsetof(ngx_http_proxy_headers_in_t, expires) },
301 { ngx_string("Cache-Control"),
302 offsetof(ngx_http_proxy_headers_in_t, cache_control) },
303 { ngx_string("ETag"), offsetof(ngx_http_proxy_headers_in_t, etag) },
304 { ngx_string("X-Accel-Expires"),
305 offsetof(ngx_http_proxy_headers_in_t, x_accel_expires) },
306
307 { ngx_string("Connection"),
308 offsetof(ngx_http_proxy_headers_in_t, connection) },
309 { ngx_string("Content-Type"),
310 offsetof(ngx_http_proxy_headers_in_t, content_type) },
311 { ngx_string("Content-Length"),
312 offsetof(ngx_http_proxy_headers_in_t, content_length) },
313
314 #if (NGX_HTTP_GZIP)
315 { ngx_string("Content-Encoding"),
316 offsetof(ngx_http_proxy_headers_in_t, content_encoding) },
317 #endif
318
319 { ngx_string("Last-Modified"),
320 offsetof(ngx_http_proxy_headers_in_t, last_modified) },
321 { ngx_string("Location"),
322 offsetof(ngx_http_proxy_headers_in_t, location) },
323 { ngx_string("Accept-Ranges"),
324 offsetof(ngx_http_proxy_headers_in_t, accept_ranges) },
325 { ngx_string("X-Pad"), offsetof(ngx_http_proxy_headers_in_t, x_pad) },
326
327 { ngx_null_string, 0 }
328 };
329
330 #endif
331
332
333 static ngx_int_t
334 ngx_http_proxy_handler(ngx_http_request_t *r)
335 {
336 ngx_int_t rc;
337 ngx_http_upstream_t *u;
338 ngx_http_proxy_loc_conf_t *plcf;
339
340 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
341
342 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
343 if (u == NULL) {
344 return NGX_HTTP_INTERNAL_SERVER_ERROR;
345 }
346
347 u->peer.log = r->connection->log;
348 u->peer.log_error = NGX_ERROR_ERR;
349 u->peer.peers = plcf->peers;
350 u->peer.tries = plcf->peers->number;
351 #if (NGX_THREADS)
352 u->peer.lock = &r->connection->lock;
353 #endif
354
355 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
356
357 u->conf = &plcf->upstream;
358
359 u->create_request = ngx_http_proxy_create_request;
360 u->reinit_request = ngx_http_proxy_reinit_request;
361 u->process_header = ngx_http_proxy_process_header;
362 u->send_header = ngx_http_proxy_send_header;
363 u->abort_request = ngx_http_proxy_abort_request;
364 u->finalize_request = ngx_http_proxy_finalize_request;
365
366 u->pipe.input_filter = ngx_event_pipe_copy_input_filter;
367
368 u->log_ctx = r->connection->log->data;
369 u->log_handler = ngx_http_upstream_log_error;
370
371 u->schema0.len = sizeof("http://") - 1;
372 u->schema0.data = (u_char *) "http://";
373 u->uri0 = plcf->uri0;
374 u->location0 = plcf->location0;
375
376 r->upstream = u;
377
378 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
379
380 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
381 return rc;
382 }
383
384 return NGX_DONE;
385 }
386
387
388 static ngx_int_t
389 ngx_http_proxy_create_request(ngx_http_request_t *r)
390 {
391 size_t len;
392 ngx_uint_t i, key;
393 uintptr_t escape;
394 ngx_buf_t *b;
395 ngx_str_t *hh;
396 ngx_chain_t *cl;
397 ngx_list_part_t *part;
398 ngx_table_elt_t *header;
399 ngx_http_upstream_t *u;
400 ngx_http_proxy_loc_conf_t *plcf;
401 ngx_http_script_code_pt code;
402 ngx_http_script_len_code_pt lcode;
403 ngx_http_script_lite_engine_t e;
404
405 u = r->upstream;
406
407 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
408
409 len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
410
411 if (u->method) {
412 len += ngx_http_proxy_methods[u->method - 1].len + u->uri0.len;
413 } else {
414 len += r->method_name.len + u->uri0.len;
415 }
416
417 escape = 0;
418
419 if (plcf->upstream.pass_unparsed_uri && r->valid_unparsed_uri) {
420 len += r->unparsed_uri.len - 1;
421
422 } else {
423 if (r->quoted_uri) {
424 escape = 2 * ngx_escape_uri(NULL, r->uri.data + u->location0->len,
425 r->uri.len - u->location0->len,
426 NGX_ESCAPE_URI);
427 }
428
429 len += r->uri.len - u->location0->len + escape
430 + sizeof("?") - 1 + r->args.len;
431 }
432
433
434 e.ip = plcf->headers_set_len->elts;
435 e.request = r;
436
437 while (*(uintptr_t *) e.ip) {
438 lcode = *(ngx_http_script_len_code_pt *) e.ip;
439 len += lcode(&e);
440 }
441
442
443 part = &r->headers_in.headers.part;
444 header = part->elts;
445 hh = (ngx_str_t *) plcf->headers_set_hash->buckets;
446
447 for (i = 0; /* void */; i++) {
448
449 if (i >= part->nelts) {
450 if (part->next == NULL) {
451 break;
452 }
453
454 part = part->next;
455 header = part->elts;
456 i = 0;
457 }
458
459 key = header[i].hash % plcf->headers_set_hash->hash_size;
460
461 if (hh[key].len == header[i].key.len
462 && ngx_strcasecmp(hh[key].data, header[i].key.data) == 0)
463 {
464 continue;
465 }
466
467 len += header[i].key.len + sizeof(": ") - 1
468 + header[i].value.len + sizeof(CRLF) - 1;
469 }
470
471 b = ngx_create_temp_buf(r->pool, len);
472 if (b == NULL) {
473 return NGX_ERROR;
474 }
475
476 cl = ngx_alloc_chain_link(r->pool);
477 if (cl == NULL) {
478 return NGX_ERROR;
479 }
480
481 cl->buf = b;
482 cl->next = NULL;
483
484 r->request_body->bufs = cl;
485
486
487 /* the request line */
488
489 if (u->method) {
490 b->last = ngx_cpymem(b->last,
491 ngx_http_proxy_methods[u->method - 1].data,
492 ngx_http_proxy_methods[u->method - 1].len);
493 } else {
494 b->last = ngx_cpymem(b->last, r->method_name.data, r->method_name.len);
495 }
496
497 b->last = ngx_cpymem(b->last, u->uri0.data, u->uri0.len);
498
499 if (plcf->upstream.pass_unparsed_uri && r->valid_unparsed_uri) {
500 b->last = ngx_cpymem(b->last, r->unparsed_uri.data + 1,
501 r->unparsed_uri.len - 1);
502 } else {
503 if (escape) {
504 ngx_escape_uri(b->last, r->uri.data + u->location0->len,
505 r->uri.len - u->location0->len, NGX_ESCAPE_URI);
506 b->last += r->uri.len - u->location0->len + escape;
507
508 } else {
509 b->last = ngx_cpymem(b->last, r->uri.data + u->location0->len,
510 r->uri.len - u->location0->len);
511 }
512
513 if (r->args.len > 0) {
514 *b->last++ = '?';
515 b->last = ngx_cpymem(b->last, r->args.data, r->args.len);
516 }
517 }
518
519 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
520 sizeof(ngx_http_proxy_version) - 1);
521
522
523 e.ip = plcf->headers_set->elts;
524 e.pos = b->last;
525
526 while (*(uintptr_t *) e.ip) {
527 code = *(ngx_http_script_code_pt *) e.ip;
528 code((ngx_http_script_engine_t *) &e);
529 }
530
531 b->last = e.pos;
532
533
534 part = &r->headers_in.headers.part;
535 header = part->elts;
536
537 for (i = 0; /* void */; i++) {
538
539 if (i >= part->nelts) {
540 if (part->next == NULL) {
541 break;
542 }
543
544 part = part->next;
545 header = part->elts;
546 i = 0;
547 }
548
549 key = header[i].hash % plcf->headers_set_hash->hash_size;
550
551 if (hh[key].len == header[i].key.len
552 && ngx_strcasecmp(hh[key].data, header[i].key.data) == 0)
553 {
554 continue;
555 }
556
557 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len);
558
559 *b->last++ = ':'; *b->last++ = ' ';
560
561 b->last = ngx_cpymem(b->last, header[i].value.data,
562 header[i].value.len);
563
564 *b->last++ = CR; *b->last++ = LF;
565
566 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
567 "http proxy header: \"%V: %V\"",
568 &header[i].key, &header[i].value);
569 }
570
571 /* add "\r\n" at the header end */
572 *b->last++ = CR; *b->last++ = LF;
573
574 #if (NGX_DEBUG)
575 {
576 ngx_str_t s;
577
578 s.len = b->last - b->pos;
579 s.data = b->pos;
580 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
581 "http proxy header:\n\"%V\"", &s);
582 }
583 #endif
584
585 return NGX_OK;
586 }
587
588
589 static ngx_int_t
590 ngx_http_proxy_reinit_request(ngx_http_request_t *r)
591 {
592 return NGX_OK;
593 }
594
595
596 static ngx_int_t
597 ngx_http_proxy_process_header(ngx_http_request_t *r)
598 {
599 return NGX_OK;
600 }
601
602
603 static ngx_int_t
604 ngx_http_proxy_send_header(ngx_http_request_t *r)
605 {
606 return NGX_OK;
607 }
608
609
610 static void
611 ngx_http_proxy_abort_request(ngx_http_request_t *r)
612 {
613 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
614 "abort http proxy request");
615
616 return;
617 }
618
619
620 static void
621 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
622 {
623 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
624 "finalize http proxy request");
625
626 return;
627 }
628
629
630 static ngx_int_t
631 ngx_http_proxy_init(ngx_cycle_t *cycle)
632 {
633 #if 0
634 ngx_http_variable_t *var;
635
636 var = ngx_http_add_variable(cf, &ngx_http_proxy_host, 1);
637 if (var == NULL) {
638 return NGX_ERROR;
639 }
640
641 var->handler = ngx_http_proxy_host_variable;
642 #endif
643
644 return NGX_OK;
645
646 #if 0
647 ngx_http_log_op_name_t *op;
648
649 for (op = ngx_http_proxy_log_fmt_ops; op->name.len; op++) { /* void */ }
650 op->run = NULL;
651
652 for (op = ngx_http_log_fmt_ops; op->run; op++) {
653 if (op->name.len == 0) {
654 op = (ngx_http_log_op_name_t *) op->run;
655 }
656 }
657
658 op->run = (ngx_http_log_op_run_pt) ngx_http_proxy_log_fmt_ops;
659
660 #endif
661 }
662
663
664 static ngx_http_variable_value_t *
665 ngx_http_proxy_host_variable(ngx_http_request_t *r, uintptr_t data)
666 {
667 ngx_http_variable_value_t *var;
668 ngx_http_proxy_loc_conf_t *plcf;
669
670 var = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
671 if (var == NULL) {
672 return NULL;
673 }
674
675 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
676
677 var->value = 0;
678 var->text = plcf->host_header;
679
680 return var;
681 }
682
683
684 static void *
685 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
686 {
687 ngx_http_proxy_loc_conf_t *conf;
688
689 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
690 if (conf == NULL) {
691 return NGX_CONF_ERROR;
692 }
693
694 /*
695 * set by ngx_pcalloc():
696 *
697 * conf->upstream.bufs.num = 0;
698 * conf->upstream.path = NULL;
699 * conf->upstream.next_upstream = 0;
700 * conf->upstream.temp_path = NULL;
701 */
702
703 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
704 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
705 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
706
707 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
708 conf->upstream.header_buffer_size = NGX_CONF_UNSET_SIZE;
709 conf->upstream.busy_buffers_size = NGX_CONF_UNSET_SIZE;
710 conf->upstream.max_temp_file_size = NGX_CONF_UNSET_SIZE;
711 conf->upstream.temp_file_write_size = NGX_CONF_UNSET_SIZE;
712
713 conf->upstream.redirect_errors = NGX_CONF_UNSET;
714 conf->upstream.pass_unparsed_uri = NGX_CONF_UNSET;
715 conf->upstream.x_powered_by = NGX_CONF_UNSET;
716
717 /* "proxy_cyclic_temp_file" is disabled */
718 conf->upstream.cyclic_temp_file = 0;
719
720 conf->preserve_host = NGX_CONF_UNSET;
721 conf->set_x_url = NGX_CONF_UNSET;
722 conf->set_x_real_ip = NGX_CONF_UNSET;
723 conf->add_x_forwarded_for = NGX_CONF_UNSET;
724
725 conf->pass_server = NGX_CONF_UNSET;
726 conf->pass_x_accel_expires = NGX_CONF_UNSET;
727
728 return conf;
729 }
730
731
732 static char *
733 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
734 {
735 ngx_http_proxy_loc_conf_t *prev = parent;
736 ngx_http_proxy_loc_conf_t *conf = child;
737
738 size_t size;
739 ngx_str_t *name;
740 ngx_table_elt_t *src;
741 ngx_http_variable_t *var;
742
743 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
744 prev->upstream.connect_timeout, 60000);
745
746 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
747 prev->upstream.send_timeout, 60000);
748
749 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
750 prev->upstream.read_timeout, 60000);
751
752 ngx_conf_merge_size_value(conf->upstream.send_lowat,
753 prev->upstream.send_lowat, 0);
754
755 ngx_conf_merge_size_value(conf->upstream.header_buffer_size,
756 prev->upstream.header_buffer_size,
757 (size_t) ngx_pagesize);
758
759 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
760 8, ngx_pagesize);
761
762 if (conf->upstream.bufs.num < 2) {
763 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
764 "there must be at least 2 \"proxy_buffers\"");
765 return NGX_CONF_ERROR;
766 }
767
768
769 size = conf->upstream.header_buffer_size;
770 if (size < conf->upstream.bufs.size) {
771 size = conf->upstream.bufs.size;
772 }
773
774
775 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size,
776 prev->upstream.busy_buffers_size,
777 NGX_CONF_UNSET_SIZE);
778
779 if (conf->upstream.busy_buffers_size == NGX_CONF_UNSET_SIZE) {
780 conf->upstream.busy_buffers_size = 2 * size;
781
782 } else if (conf->upstream.busy_buffers_size < size) {
783 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
784 "\"proxy_busy_buffers_size\" must be equal or bigger than "
785 "maximum of the value of \"proxy_header_buffer_size\" and "
786 "one of the \"proxy_buffers\"");
787
788 return NGX_CONF_ERROR;
789
790 } else if (conf->upstream.busy_buffers_size
791 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
792 {
793 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
794 "\"proxy_busy_buffers_size\" must be less than "
795 "the size of all \"proxy_buffers\" minus one buffer");
796
797 return NGX_CONF_ERROR;
798 }
799
800
801 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size,
802 prev->upstream.temp_file_write_size,
803 NGX_CONF_UNSET_SIZE);
804
805 if (conf->upstream.temp_file_write_size == NGX_CONF_UNSET_SIZE) {
806 conf->upstream.temp_file_write_size = 2 * size;
807
808 } else if (conf->upstream.temp_file_write_size < size) {
809 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
810 "\"proxy_temp_file_write_size\" must be equal or bigger than "
811 "maximum of the value of \"proxy_header_buffer_size\" and "
812 "one of the \"proxy_buffers\"");
813
814 return NGX_CONF_ERROR;
815 }
816
817
818 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size,
819 prev->upstream.max_temp_file_size,
820 NGX_CONF_UNSET_SIZE);
821
822 if (conf->upstream.max_temp_file_size == NGX_CONF_UNSET_SIZE) {
823
824 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
825
826 } else if (conf->upstream.max_temp_file_size != 0
827 && conf->upstream.max_temp_file_size < size)
828 {
829 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
830 "\"fastcgi_max_temp_file_size\" must be equal to zero to disable "
831 "the temporary files usage or must be equal or bigger than "
832 "maximum of the value of \"fastcgi_header_buffer_size\" and "
833 "one of the \"fastcgi_buffers\"");
834
835 return NGX_CONF_ERROR;
836 }
837
838 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
839 prev->upstream.next_upstream,
840 (NGX_CONF_BITMASK_SET
841 |NGX_HTTP_UPSTREAM_FT_ERROR
842 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
843
844 ngx_conf_merge_msec_value(conf->upstream.redirect_errors,
845 prev->upstream.redirect_errors, 0);
846
847 ngx_conf_merge_msec_value(conf->upstream.pass_unparsed_uri,
848 prev->upstream.pass_unparsed_uri, 0);
849
850 if (conf->upstream.pass_unparsed_uri && conf->location0->len > 1) {
851 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
852 "\"proxy_pass_unparsed_uri\" can be set for "
853 "location \"/\" or given by regular expression.");
854 return NGX_CONF_ERROR;
855 }
856
857 ngx_conf_merge_msec_value(conf->upstream.x_powered_by,
858 prev->upstream.x_powered_by, 1);
859
860 ngx_conf_merge_value(conf->preserve_host, prev->preserve_host, 0);
861 ngx_conf_merge_value(conf->set_x_url, prev->set_x_url, 0);
862 ngx_conf_merge_value(conf->set_x_real_ip, prev->set_x_real_ip, 0);
863 ngx_conf_merge_value(conf->add_x_forwarded_for,
864 prev->add_x_forwarded_for, 0);
865
866 if (conf->peers == NULL) {
867 conf->peers = prev->peers;
868 conf->upstream = prev->upstream;
869 }
870
871 if (conf->headers_set_hash == NULL) {
872 conf->headers_set_len = prev->headers_set_len;
873 conf->headers_set = prev->headers_set;
874 conf->headers_set_hash = prev->headers_set_hash;
875 }
876
877 if (conf->headers_set_hash == NULL) {
878
879 if (conf->headers_names == NULL) {
880 conf->headers_names = ngx_array_create(cf->pool, 4,
881 sizeof(ngx_str_t));
882 if (conf->headers_names == NULL) {
883 return NGX_CONF_ERROR;
884 }
885 }
886
887 if (conf->headers_sources == NULL) {
888 conf->headers_sources = ngx_array_create(cf->pool, 4,
889 sizeof(ngx_table_elt_t));
890 if (conf->headers_sources == NULL) {
891 return NGX_CONF_ERROR;
892 }
893 }
894
895 /* STUB */
896 var = ngx_http_add_variable(cf, &ngx_http_proxy_host, 0);
897 if (var == NULL) {
898 return NGX_CONF_ERROR;
899 }
900
901 var->handler = ngx_http_proxy_host_variable;
902 /**/
903
904
905 name = ngx_array_push(conf->headers_names);
906 if (name == NULL) {
907 return NGX_CONF_ERROR;
908 }
909
910 name->len = sizeof("Host") - 1;
911 name->data = (u_char *) "Host";
912
913 src = ngx_array_push(conf->headers_sources);
914 if (src == NULL) {
915 return NGX_CONF_ERROR;
916 }
917
918 src->hash = 0;
919 src->key.len = sizeof("Host") - 1;
920 src->key.data = (u_char *) "Host";
921 src->value.len = sizeof("$PROXY_HOST") - 1;
922 src->value.data = (u_char *) "$PROXY_HOST";
923
924
925 name = ngx_array_push(conf->headers_names);
926 if (name == NULL) {
927 return NGX_CONF_ERROR;
928 }
929
930 name->len = sizeof("Connection") - 1;
931 name->data = (u_char *) "Connection";
932
933 src = ngx_array_push(conf->headers_sources);
934 if (src == NULL) {
935 return NGX_CONF_ERROR;
936 }
937
938 src->hash = 0;
939 src->key.len = sizeof("Connection") - 1;
940 src->key.data = (u_char *) "Connection";
941 src->value.len = sizeof("close") - 1;
942 src->value.data = (u_char *) "close";
943
944
945 name = ngx_array_push(conf->headers_names);
946 if (name == NULL) {
947 return NGX_CONF_ERROR;
948 }
949
950 name->len = 0;
951 name->data = NULL;
952
953
954 if (ngx_http_script_compile_lite(cf, conf->headers_sources,
955 &conf->headers_set_len, &conf->headers_set,
956 ngx_http_proxy_compile_header_start,
957 ngx_http_proxy_compile_header_end) != NGX_OK)
958 {
959 return NGX_CONF_ERROR;
960 }
961
962
963 conf->headers_set_hash = ngx_pcalloc(cf->pool, sizeof(ngx_hash_t));
964 if (conf->headers_set_hash == NULL) {
965 return NGX_CONF_ERROR;
966 }
967
968 conf->headers_set_hash->max_size = 100;
969 conf->headers_set_hash->bucket_limit = 1;
970 conf->headers_set_hash->bucket_size = sizeof(ngx_str_t);
971 conf->headers_set_hash->name = "proxy_headers";
972
973 if (ngx_hash_init(conf->headers_set_hash, cf->pool,
974 conf->headers_names->elts) != NGX_OK)
975 {
976 return NGX_CONF_ERROR;
977 }
978
979 #if 0
980 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
981 #endif
982 ngx_log_error(NGX_LOG_NOTICE, cf->log, 0,
983 "proxy_headers hash size: %ui, "
984 "max buckets per entry: %ui",
985 conf->headers_set_hash->hash_size,
986 conf->headers_set_hash->min_buckets);
987 }
988
989 return NGX_CONF_OK;
990 }
991
992
993 static ngx_int_t
994 ngx_http_proxy_compile_header_start(ngx_table_elt_t *h,
995 ngx_array_t *lengths, ngx_array_t *values, ngx_uint_t value)
996 {
997 u_char *p;
998 size_t size;
999 ngx_http_script_copy_code_t *copy;
1000
1001 copy = ngx_array_push_n(lengths, sizeof(ngx_http_script_copy_code_t));
1002 if (copy == NULL) {
1003 return NGX_ERROR;
1004 }
1005
1006 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len;
1007 copy->len = h->key.len + sizeof(": ") - 1;
1008
1009 if (value) {
1010 copy->len += h->value.len + sizeof(CRLF) - 1;
1011 }
1012
1013 size = (copy->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
1014
1015 copy = ngx_array_push_n(values,
1016 sizeof(ngx_http_script_copy_code_t) + size);
1017 if (copy == NULL) {
1018 return NGX_ERROR;
1019 }
1020
1021 copy->code = ngx_http_script_copy;
1022 copy->len = h->key.len + sizeof(": ") - 1;
1023
1024 if (value) {
1025 copy->len += h->value.len + sizeof(CRLF) - 1;
1026 }
1027
1028 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
1029
1030 p = ngx_cpymem(p, h->key.data, h->key.len);
1031 p = ngx_cpymem(p, ": ", sizeof(": ") - 1);
1032
1033 if (value) {
1034 p = ngx_cpymem(p, h->value.data, h->value.len);
1035 ngx_memcpy(p, CRLF, sizeof(CRLF) - 1);
1036 }
1037
1038 return NGX_OK;
1039 }
1040
1041
1042 static ngx_int_t
1043 ngx_http_proxy_compile_header_end(ngx_array_t *lengths, ngx_array_t *values)
1044 {
1045 size_t size;
1046 ngx_http_script_copy_code_t *copy;
1047
1048 copy = ngx_array_push_n(lengths, sizeof(ngx_http_script_copy_code_t));
1049 if (copy == NULL) {
1050 return NGX_ERROR;
1051 }
1052
1053 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len;
1054 copy->len = sizeof(CRLF) - 1;
1055
1056 size = (sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
1057 & ~(sizeof(uintptr_t) - 1);
1058
1059 copy = ngx_array_push_n(values,
1060 sizeof(ngx_http_script_copy_code_t) + size);
1061 if (copy == NULL) {
1062 return NGX_ERROR;
1063 }
1064
1065 copy->code = ngx_http_script_copy;
1066 copy->len = sizeof(CRLF) - 1;
1067
1068 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t),
1069 CRLF, sizeof(CRLF) - 1);
1070
1071 return NGX_OK;
1072 }
1073
1074
1075 static char *
1076 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1077 {
1078 ngx_http_proxy_loc_conf_t *lcf = conf;
1079
1080 ngx_uint_t i;
1081 ngx_str_t *value, *url;
1082 ngx_inet_upstream_t inet_upstream;
1083 ngx_http_core_loc_conf_t *clcf;
1084 #if (NGX_HAVE_UNIX_DOMAIN)
1085 ngx_unix_domain_upstream_t unix_upstream;
1086 #endif
1087
1088 value = cf->args->elts;
1089
1090 url = &value[1];
1091
1092 if (ngx_strncasecmp(url->data, "http://", 7) != 0) {
1093 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
1094 return NGX_CONF_ERROR;
1095 }
1096
1097 if (ngx_strncasecmp(url->data + 7, "unix:", 5) == 0) {
1098
1099 #if (NGX_HAVE_UNIX_DOMAIN)
1100
1101 ngx_memzero(&unix_upstream, sizeof(ngx_unix_domain_upstream_t));
1102
1103 unix_upstream.name = *url;
1104 unix_upstream.url.len = url->len - 7;
1105 unix_upstream.url.data = url->data + 7;
1106 unix_upstream.uri_part = 1;
1107
1108 lcf->peers = ngx_unix_upstream_parse(cf, &unix_upstream);
1109 if (lcf->peers == NULL) {
1110 return NGX_CONF_ERROR;
1111 }
1112
1113 lcf->peers->peer[0].uri_separator = ":";
1114
1115 lcf->host_header.len = sizeof("localhost") - 1;
1116 lcf->host_header.data = (u_char *) "localhost";
1117 lcf->uri0 = unix_upstream.uri;
1118 #if 0
1119 STUB
1120 lcf->upstream->default_port = 1;
1121 #endif
1122
1123 #else
1124 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1125 "the unix domain sockets are not supported "
1126 "on this platform");
1127 return NGX_CONF_ERROR;
1128
1129 #endif
1130
1131 } else {
1132 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
1133
1134 inet_upstream.name = *url;
1135 inet_upstream.url.len = url->len - 7;
1136 inet_upstream.url.data = url->data + 7;
1137 inet_upstream.default_port_value = 80;
1138 inet_upstream.uri_part = 1;
1139
1140 lcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream);
1141 if (lcf->peers == NULL) {
1142 return NGX_CONF_ERROR;
1143 }
1144
1145 for (i = 0; i < lcf->peers->number; i++) {
1146 lcf->peers->peer[i].uri_separator = ":";
1147 }
1148
1149 lcf->host_header = inet_upstream.host_header;
1150 lcf->uri0 = inet_upstream.uri;
1151 #if 0
1152 STUB
1153 lcf->port_text = inet_upstream.port_text;
1154 lcf->upstream->default_port = inet_upstream.default_port;
1155 #endif
1156 }
1157
1158 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
1159
1160 clcf->handler = ngx_http_proxy_handler;
1161
1162 #if (NGX_PCRE)
1163 lcf->location0 = clcf->regex ? &ngx_http_proxy_uri : &clcf->name;
1164 #else
1165 lcf->location0 = &clcf->name;
1166 #endif
1167
1168 if (clcf->name.data[clcf->name.len - 1] == '/') {
1169 clcf->auto_redirect = 1;
1170 }
1171
1172 return NGX_CONF_OK;
1173 }
1174
1175
1176 static char *
1177 ngx_http_proxy_set_x_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1178 {
1179 return NGX_CONF_OK;
1180 }
1181
1182
1183 static char *
1184 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
1185 {
1186 #if (NGX_FREEBSD)
1187 ssize_t *np = data;
1188
1189 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
1190 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1191 "\"proxy_send_lowat\" must be less than %d "
1192 "(sysctl net.inet.tcp.sendspace)",
1193 ngx_freebsd_net_inet_tcp_sendspace);
1194
1195 return NGX_CONF_ERROR;
1196 }
1197
1198 #elif !(NGX_HAVE_SO_SNDLOWAT)
1199 ssize_t *np = data;
1200
1201 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1202 "\"proxy_send_lowat\" is not supported, ignored");
1203
1204 *np = 0;
1205
1206 #endif
1207
1208 return NGX_CONF_OK;
1209 }