Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_proxy_module.c @ 879:804514cb0758
bump version to 0.5.0
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 27 Nov 2006 14:34:42 +0000 |
parents | ebab9490204c |
children | 4d68c486fcb0 |
rev | line source |
---|---|
507 | 1 |
577 | 2 /* |
507 | 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 | |
509 | 13 typedef struct ngx_http_proxy_redirect_s ngx_http_proxy_redirect_t; |
14 | |
15 typedef ngx_int_t (*ngx_http_proxy_redirect_pt)(ngx_http_request_t *r, | |
16 ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_redirect_t *pr); | |
507 | 17 |
509 | 18 struct ngx_http_proxy_redirect_s { |
651 | 19 ngx_http_proxy_redirect_pt handler; |
20 ngx_str_t redirect; | |
507 | 21 |
509 | 22 union { |
651 | 23 ngx_str_t text; |
509 | 24 |
25 struct { | |
651 | 26 void *lengths; |
27 void *values; | |
509 | 28 } vars; |
507 | 29 |
651 | 30 void *regex; |
509 | 31 } replacement; |
32 }; | |
33 | |
34 | |
35 typedef struct { | |
651 | 36 ngx_http_upstream_conf_t upstream; |
37 | |
38 ngx_http_upstream_srv_conf_t *upstream_peers; | |
39 | |
40 ngx_array_t *flushes; | |
41 ngx_array_t *body_set_len; | |
42 ngx_array_t *body_set; | |
43 ngx_array_t *headers_set_len; | |
44 ngx_array_t *headers_set; | |
45 ngx_hash_t headers_set_hash; | |
46 | |
47 ngx_array_t *headers_source; | |
48 ngx_array_t *headers_names; | |
49 | |
50 ngx_array_t *redirects; | |
51 | |
52 ngx_str_t body_source; | |
53 | |
54 ngx_str_t method; | |
55 ngx_str_t host_header; | |
56 ngx_str_t port_text; | |
57 | |
58 ngx_flag_t redirect; | |
507 | 59 } ngx_http_proxy_loc_conf_t; |
60 | |
61 | |
62 typedef struct { | |
651 | 63 ngx_uint_t status; |
64 ngx_uint_t status_count; | |
65 u_char *status_start; | |
66 u_char *status_end; | |
67 | |
68 size_t internal_body_length; | |
509 | 69 } ngx_http_proxy_ctx_t; |
507 | 70 |
71 | |
509 | 72 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 20 |
507 | 73 |
74 | |
75 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r); | |
76 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r); | |
509 | 77 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r); |
78 static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r, | |
79 ngx_http_proxy_ctx_t *p); | |
507 | 80 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r); |
81 static void ngx_http_proxy_abort_request(ngx_http_request_t *r); | |
82 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r, | |
83 ngx_int_t rc); | |
84 | |
573 | 85 static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r, |
86 ngx_http_variable_value_t *v, uintptr_t data); | |
87 static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r, | |
88 ngx_http_variable_value_t *v, uintptr_t data); | |
89 static ngx_int_t | |
509 | 90 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, |
573 | 91 ngx_http_variable_value_t *v, uintptr_t data); |
577 | 92 static ngx_int_t |
93 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r, | |
94 ngx_http_variable_value_t *v, uintptr_t data); | |
509 | 95 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, |
96 ngx_table_elt_t *h, size_t prefix); | |
507 | 97 |
509 | 98 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf); |
507 | 99 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf); |
100 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, | |
101 void *parent, void *child); | |
102 | |
103 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, | |
104 void *conf); | |
509 | 105 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, |
106 void *conf); | |
507 | 107 |
108 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data); | |
109 | |
509 | 110 |
507 | 111 static ngx_conf_post_t ngx_http_proxy_lowat_post = |
509 | 112 { ngx_http_proxy_lowat_check }; |
113 | |
581 | 114 static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_header_buffer_size = { |
115 ngx_conf_deprecated, "proxy_header_buffer_size", "proxy_buffer_size" | |
116 }; | |
117 | |
657 | 118 static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_redirect_errors = { |
119 ngx_conf_deprecated, "proxy_redirect_errors", "proxy_intercept_errors" | |
120 }; | |
121 | |
507 | 122 |
123 static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = { | |
124 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
125 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
126 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
127 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | |
623 | 128 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, |
507 | 129 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, |
665 | 130 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, |
507 | 131 { ngx_null_string, 0 } |
132 }; | |
133 | |
134 | |
135 static ngx_command_t ngx_http_proxy_commands[] = { | |
136 | |
137 { ngx_string("proxy_pass"), | |
843
ebab9490204c
allow proxy_pass inside limit_except
Igor Sysoev <igor@sysoev.ru>
parents:
812
diff
changeset
|
138 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1, |
507 | 139 ngx_http_proxy_pass, |
140 NGX_HTTP_LOC_CONF_OFFSET, | |
141 0, | |
142 NULL }, | |
143 | |
509 | 144 { ngx_string("proxy_redirect"), |
145 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, | |
146 ngx_http_proxy_redirect, | |
147 NGX_HTTP_LOC_CONF_OFFSET, | |
148 0, | |
149 NULL }, | |
150 | |
581 | 151 { ngx_string("proxy_buffering"), |
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, upstream.buffering), | |
156 NULL }, | |
157 | |
629 | 158 { ngx_string("proxy_ignore_client_abort"), |
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, upstream.ignore_client_abort), | |
163 NULL }, | |
164 | |
507 | 165 { ngx_string("proxy_connect_timeout"), |
166 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
167 ngx_conf_set_msec_slot, | |
168 NGX_HTTP_LOC_CONF_OFFSET, | |
169 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout), | |
170 NULL }, | |
171 | |
172 { ngx_string("proxy_send_timeout"), | |
173 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
174 ngx_conf_set_msec_slot, | |
175 NGX_HTTP_LOC_CONF_OFFSET, | |
176 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout), | |
177 NULL }, | |
178 | |
179 { ngx_string("proxy_send_lowat"), | |
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.send_lowat), | |
184 &ngx_http_proxy_lowat_post }, | |
185 | |
657 | 186 { ngx_string("proxy_intercept_errors"), |
187 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
188 ngx_conf_set_flag_slot, | |
189 NGX_HTTP_LOC_CONF_OFFSET, | |
190 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors), | |
191 NULL }, | |
192 | |
509 | 193 { ngx_string("proxy_redirect_errors"), |
194 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
195 ngx_conf_set_flag_slot, | |
196 NGX_HTTP_LOC_CONF_OFFSET, | |
657 | 197 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors), |
198 &ngx_conf_deprecated_proxy_redirect_errors }, | |
509 | 199 |
200 { ngx_string("proxy_set_header"), | |
201 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
649 | 202 ngx_conf_set_keyval_slot, |
507 | 203 NGX_HTTP_LOC_CONF_OFFSET, |
509 | 204 offsetof(ngx_http_proxy_loc_conf_t, headers_source), |
507 | 205 NULL }, |
206 | |
577 | 207 { ngx_string("proxy_set_body"), |
208 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
209 ngx_conf_set_str_slot, | |
210 NGX_HTTP_LOC_CONF_OFFSET, | |
211 offsetof(ngx_http_proxy_loc_conf_t, body_source), | |
212 NULL }, | |
213 | |
509 | 214 { ngx_string("proxy_method"), |
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
561 | 216 ngx_conf_set_str_slot, |
509 | 217 NGX_HTTP_LOC_CONF_OFFSET, |
561 | 218 offsetof(ngx_http_proxy_loc_conf_t, method), |
219 NULL }, | |
509 | 220 |
221 { ngx_string("proxy_pass_request_headers"), | |
507 | 222 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
223 ngx_conf_set_flag_slot, | |
224 NGX_HTTP_LOC_CONF_OFFSET, | |
509 | 225 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers), |
507 | 226 NULL }, |
227 | |
509 | 228 { ngx_string("proxy_pass_request_body"), |
507 | 229 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
230 ngx_conf_set_flag_slot, | |
231 NGX_HTTP_LOC_CONF_OFFSET, | |
509 | 232 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body), |
507 | 233 NULL }, |
234 | |
581 | 235 { ngx_string("proxy_buffer_size"), |
236 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
237 ngx_conf_set_size_slot, | |
238 NGX_HTTP_LOC_CONF_OFFSET, | |
239 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size), | |
240 NULL }, | |
241 | |
507 | 242 { ngx_string("proxy_header_buffer_size"), |
243 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
244 ngx_conf_set_size_slot, | |
245 NGX_HTTP_LOC_CONF_OFFSET, | |
581 | 246 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size), |
247 &ngx_conf_deprecated_proxy_header_buffer_size }, | |
507 | 248 |
249 { ngx_string("proxy_read_timeout"), | |
250 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
251 ngx_conf_set_msec_slot, | |
252 NGX_HTTP_LOC_CONF_OFFSET, | |
253 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout), | |
254 NULL }, | |
255 | |
256 { ngx_string("proxy_buffers"), | |
257 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
258 ngx_conf_set_bufs_slot, | |
259 NGX_HTTP_LOC_CONF_OFFSET, | |
260 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs), | |
261 NULL }, | |
262 | |
263 { ngx_string("proxy_busy_buffers_size"), | |
264 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
265 ngx_conf_set_size_slot, | |
266 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 267 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf), |
507 | 268 NULL }, |
269 | |
270 { ngx_string("proxy_temp_path"), | |
271 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234, | |
272 ngx_conf_set_path_slot, | |
273 NGX_HTTP_LOC_CONF_OFFSET, | |
274 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path), | |
275 (void *) ngx_garbage_collector_temp_handler }, | |
276 | |
277 { ngx_string("proxy_max_temp_file_size"), | |
278 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
279 ngx_conf_set_size_slot, | |
280 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 281 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf), |
507 | 282 NULL }, |
283 | |
284 { ngx_string("proxy_temp_file_write_size"), | |
285 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
286 ngx_conf_set_size_slot, | |
287 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 288 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf), |
507 | 289 NULL }, |
290 | |
291 { ngx_string("proxy_next_upstream"), | |
547 | 292 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
507 | 293 ngx_conf_set_bitmask_slot, |
294 NGX_HTTP_LOC_CONF_OFFSET, | |
295 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream), | |
296 &ngx_http_proxy_next_upstream_masks }, | |
297 | |
561 | 298 { ngx_string("proxy_upstream_max_fails"), |
299 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
300 ngx_conf_set_num_slot, | |
301 NGX_HTTP_LOC_CONF_OFFSET, | |
302 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_fails), | |
303 NULL }, | |
304 | |
305 { ngx_string("proxy_upstream_fail_timeout"), | |
306 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
307 ngx_conf_set_sec_slot, | |
308 NGX_HTTP_LOC_CONF_OFFSET, | |
309 offsetof(ngx_http_proxy_loc_conf_t, upstream.fail_timeout), | |
310 NULL }, | |
311 | |
649 | 312 { ngx_string("proxy_pass_header"), |
507 | 313 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
649 | 314 ngx_conf_set_str_array_slot, |
507 | 315 NGX_HTTP_LOC_CONF_OFFSET, |
649 | 316 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers), |
507 | 317 NULL }, |
318 | |
649 | 319 { ngx_string("proxy_hide_header"), |
507 | 320 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
649 | 321 ngx_conf_set_str_array_slot, |
507 | 322 NGX_HTTP_LOC_CONF_OFFSET, |
649 | 323 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers), |
507 | 324 NULL }, |
325 | |
326 ngx_null_command | |
327 }; | |
328 | |
329 | |
667 | 330 static ngx_http_module_t ngx_http_proxy_module_ctx = { |
509 | 331 ngx_http_proxy_add_variables, /* preconfiguration */ |
332 NULL, /* postconfiguration */ | |
507 | 333 |
334 NULL, /* create main configuration */ | |
335 NULL, /* init main configuration */ | |
336 | |
337 NULL, /* create server configuration */ | |
338 NULL, /* merge server configuration */ | |
339 | |
340 ngx_http_proxy_create_loc_conf, /* create location configration */ | |
341 ngx_http_proxy_merge_loc_conf /* merge location configration */ | |
342 }; | |
343 | |
344 | |
345 ngx_module_t ngx_http_proxy_module = { | |
509 | 346 NGX_MODULE_V1, |
507 | 347 &ngx_http_proxy_module_ctx, /* module context */ |
348 ngx_http_proxy_commands, /* module directives */ | |
349 NGX_HTTP_MODULE, /* module type */ | |
541 | 350 NULL, /* init master */ |
509 | 351 NULL, /* init module */ |
541 | 352 NULL, /* init process */ |
353 NULL, /* init thread */ | |
354 NULL, /* exit thread */ | |
355 NULL, /* exit process */ | |
356 NULL, /* exit master */ | |
357 NGX_MODULE_V1_PADDING | |
507 | 358 }; |
359 | |
360 | |
361 static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF; | |
362 | |
509 | 363 |
649 | 364 static ngx_keyval_t ngx_http_proxy_headers[] = { |
365 { ngx_string("Host"), ngx_string("$proxy_host") }, | |
366 { ngx_string("Connection"), ngx_string("close") }, | |
367 { ngx_string("Keep-Alive"), ngx_string("") }, | |
368 { ngx_null_string, ngx_null_string } | |
369 }; | |
370 | |
371 | |
372 static ngx_str_t ngx_http_proxy_hide_headers[] = { | |
373 ngx_string("Date"), | |
374 ngx_string("Server"), | |
375 ngx_string("X-Pad"), | |
376 ngx_string("X-Accel-Expires"), | |
377 ngx_string("X-Accel-Redirect"), | |
378 ngx_string("X-Accel-Limit-Rate"), | |
379 ngx_string("X-Accel-Buffer"), | |
380 ngx_null_string | |
509 | 381 }; |
382 | |
383 | |
384 static ngx_http_variable_t ngx_http_proxy_vars[] = { | |
385 | |
637 | 386 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0, |
583 | 387 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 }, |
509 | 388 |
637 | 389 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0, |
583 | 390 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 }, |
509 | 391 |
637 | 392 { ngx_string("proxy_add_x_forwarded_for"), NULL, |
583 | 393 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, |
509 | 394 |
395 #if 0 | |
637 | 396 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 }, |
509 | 397 #endif |
398 | |
637 | 399 { ngx_string("proxy_internal_body_length"), NULL, |
583 | 400 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 }, |
577 | 401 |
637 | 402 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
509 | 403 }; |
507 | 404 |
405 | |
406 static ngx_int_t | |
407 ngx_http_proxy_handler(ngx_http_request_t *r) | |
577 | 408 { |
507 | 409 ngx_int_t rc; |
410 ngx_http_upstream_t *u; | |
411 ngx_http_proxy_loc_conf_t *plcf; | |
577 | 412 |
507 | 413 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); |
577 | 414 |
507 | 415 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); |
416 if (u == NULL) { | |
417 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
418 } | |
577 | 419 |
507 | 420 u->peer.log = r->connection->log; |
421 u->peer.log_error = NGX_ERROR_ERR; | |
651 | 422 u->peer.peers = plcf->upstream_peers->peers; |
423 u->peer.tries = plcf->upstream_peers->peers->number; | |
507 | 424 #if (NGX_THREADS) |
425 u->peer.lock = &r->connection->lock; | |
426 #endif | |
427 | |
428 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module; | |
429 | |
430 u->conf = &plcf->upstream; | |
431 | |
432 u->create_request = ngx_http_proxy_create_request; | |
433 u->reinit_request = ngx_http_proxy_reinit_request; | |
509 | 434 u->process_header = ngx_http_proxy_process_status_line; |
507 | 435 u->abort_request = ngx_http_proxy_abort_request; |
436 u->finalize_request = ngx_http_proxy_finalize_request; | |
437 | |
509 | 438 if (plcf->redirects) { |
439 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect; | |
440 } | |
441 | |
649 | 442 u->buffering = plcf->upstream.buffering; |
443 | |
444 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); | |
445 if (u->pipe == NULL) { | |
446 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
581 | 447 } |
507 | 448 |
649 | 449 u->pipe->input_filter = ngx_event_pipe_copy_input_filter; |
450 | |
509 | 451 u->accel = 1; |
577 | 452 |
507 | 453 r->upstream = u; |
454 | |
455 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | |
456 | |
457 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
458 return rc; | |
459 } | |
460 | |
461 return NGX_DONE; | |
462 } | |
463 | |
464 | |
465 static ngx_int_t | |
466 ngx_http_proxy_create_request(ngx_http_request_t *r) | |
467 { | |
577 | 468 size_t len, loc_len, body_len; |
509 | 469 uintptr_t escape; |
470 ngx_buf_t *b; | |
649 | 471 ngx_str_t method; |
472 ngx_uint_t i, unparsed_uri; | |
509 | 473 ngx_chain_t *cl, *body; |
474 ngx_list_part_t *part; | |
475 ngx_table_elt_t *header; | |
476 ngx_http_upstream_t *u; | |
577 | 477 ngx_http_proxy_ctx_t *p; |
509 | 478 ngx_http_script_code_pt code; |
479 ngx_http_script_engine_t e, le; | |
480 ngx_http_proxy_loc_conf_t *plcf; | |
481 ngx_http_script_len_code_pt lcode; | |
507 | 482 |
483 u = r->upstream; | |
484 | |
485 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | |
486 | |
577 | 487 p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t)); |
488 if (p == NULL) { | |
489 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
490 } | |
491 | |
492 ngx_http_set_ctx(r, p, ngx_http_proxy_module); | |
493 | |
507 | 494 len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1; |
495 | |
561 | 496 if (u->method.len) { |
497 /* HEAD was changed to GET to cache response */ | |
498 method = u->method; | |
499 method.len++; | |
500 | |
501 } else if (plcf->method.len) { | |
502 method = plcf->method; | |
503 | |
507 | 504 } else { |
561 | 505 method = r->method_name; |
506 method.len++; | |
507 | 507 } |
508 | |
561 | 509 len += method.len + u->conf->uri.len; |
510 | |
507 | 511 escape = 0; |
509 | 512 |
627 | 513 loc_len = (r->valid_location && u->conf->uri.len) ? u->conf->location.len: |
514 0; | |
509 | 515 |
609 | 516 if (u->conf->uri.len == 0 && r->valid_unparsed_uri && r == r->main) { |
517 unparsed_uri = 1; | |
539 | 518 len += r->unparsed_uri.len; |
507 | 519 |
520 } else { | |
609 | 521 unparsed_uri = 0; |
507 | 522 if (r->quoted_uri) { |
509 | 523 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, |
524 r->uri.len - loc_len, NGX_ESCAPE_URI); | |
507 | 525 } |
526 | |
509 | 527 len += r->uri.len - loc_len + escape + sizeof("?") - 1 + r->args.len; |
507 | 528 } |
529 | |
573 | 530 ngx_http_script_flush_no_cachable_variables(r, plcf->flushes); |
531 | |
577 | 532 if (plcf->body_set_len) { |
533 le.ip = plcf->body_set_len->elts; | |
534 le.request = r; | |
535 le.flushed = 1; | |
536 body_len = 0; | |
537 | |
538 while (*(uintptr_t *) le.ip) { | |
539 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
540 body_len += lcode(&le); | |
541 } | |
542 | |
543 p->internal_body_length = body_len; | |
544 len += body_len; | |
545 } | |
546 | |
509 | 547 le.ip = plcf->headers_set_len->elts; |
548 le.request = r; | |
573 | 549 le.flushed = 1; |
507 | 550 |
509 | 551 while (*(uintptr_t *) le.ip) { |
552 while (*(uintptr_t *) le.ip) { | |
553 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
554 len += lcode(&le); | |
555 } | |
556 le.ip += sizeof(uintptr_t); | |
507 | 557 } |
558 | |
559 | |
509 | 560 if (plcf->upstream.pass_request_headers) { |
561 part = &r->headers_in.headers.part; | |
562 header = part->elts; | |
563 | |
564 for (i = 0; /* void */; i++) { | |
507 | 565 |
509 | 566 if (i >= part->nelts) { |
567 if (part->next == NULL) { | |
568 break; | |
569 } | |
570 | |
571 part = part->next; | |
572 header = part->elts; | |
577 | 573 i = 0; |
507 | 574 } |
575 | |
649 | 576 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash, |
577 header[i].lowcase_key, header[i].key.len)) | |
509 | 578 { |
579 continue; | |
580 } | |
581 | |
582 len += header[i].key.len + sizeof(": ") - 1 | |
583 + header[i].value.len + sizeof(CRLF) - 1; | |
507 | 584 } |
509 | 585 } |
507 | 586 |
587 | |
588 b = ngx_create_temp_buf(r->pool, len); | |
589 if (b == NULL) { | |
590 return NGX_ERROR; | |
591 } | |
592 | |
593 cl = ngx_alloc_chain_link(r->pool); | |
594 if (cl == NULL) { | |
595 return NGX_ERROR; | |
596 } | |
597 | |
598 cl->buf = b; | |
599 | |
600 | |
601 /* the request line */ | |
602 | |
573 | 603 b->last = ngx_copy(b->last, method.data, method.len); |
507 | 604 |
555 | 605 u->uri.data = b->last; |
606 | |
609 | 607 if (unparsed_uri) { |
573 | 608 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len); |
609 | |
507 | 610 } else { |
555 | 611 if (r->valid_location) { |
573 | 612 b->last = ngx_copy(b->last, u->conf->uri.data, u->conf->uri.len); |
555 | 613 } |
551 | 614 |
507 | 615 if (escape) { |
509 | 616 ngx_escape_uri(b->last, r->uri.data + loc_len, |
617 r->uri.len - loc_len, NGX_ESCAPE_URI); | |
618 b->last += r->uri.len - loc_len + escape; | |
507 | 619 |
577 | 620 } else { |
573 | 621 b->last = ngx_copy(b->last, r->uri.data + loc_len, |
622 r->uri.len - loc_len); | |
507 | 623 } |
624 | |
625 if (r->args.len > 0) { | |
626 *b->last++ = '?'; | |
573 | 627 b->last = ngx_copy(b->last, r->args.data, r->args.len); |
507 | 628 } |
629 } | |
630 | |
555 | 631 u->uri.len = b->last - u->uri.data; |
632 | |
507 | 633 b->last = ngx_cpymem(b->last, ngx_http_proxy_version, |
634 sizeof(ngx_http_proxy_version) - 1); | |
635 | |
577 | 636 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); |
507 | 637 |
638 e.ip = plcf->headers_set->elts; | |
639 e.pos = b->last; | |
509 | 640 e.request = r; |
573 | 641 e.flushed = 1; |
507 | 642 |
509 | 643 le.ip = plcf->headers_set_len->elts; |
644 | |
645 while (*(uintptr_t *) le.ip) { | |
646 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
515 | 647 |
648 /* skip the header line name length */ | |
649 (void) lcode(&le); | |
509 | 650 |
651 if (*(ngx_http_script_len_code_pt *) le.ip) { | |
652 | |
653 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | |
654 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
655 } | |
656 | |
657 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0; | |
658 | |
659 } else { | |
660 e.skip = 0; | |
661 } | |
662 | |
663 le.ip += sizeof(uintptr_t); | |
664 | |
665 while (*(uintptr_t *) e.ip) { | |
666 code = *(ngx_http_script_code_pt *) e.ip; | |
667 code((ngx_http_script_engine_t *) &e); | |
577 | 668 } |
509 | 669 e.ip += sizeof(uintptr_t); |
507 | 670 } |
671 | |
672 b->last = e.pos; | |
673 | |
674 | |
509 | 675 if (plcf->upstream.pass_request_headers) { |
676 part = &r->headers_in.headers.part; | |
677 header = part->elts; | |
577 | 678 |
509 | 679 for (i = 0; /* void */; i++) { |
507 | 680 |
509 | 681 if (i >= part->nelts) { |
682 if (part->next == NULL) { | |
683 break; | |
684 } | |
685 | |
686 part = part->next; | |
687 header = part->elts; | |
577 | 688 i = 0; |
507 | 689 } |
690 | |
649 | 691 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash, |
692 header[i].lowcase_key, header[i].key.len)) | |
509 | 693 { |
694 continue; | |
695 } | |
507 | 696 |
573 | 697 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len); |
507 | 698 |
509 | 699 *b->last++ = ':'; *b->last++ = ' '; |
507 | 700 |
573 | 701 b->last = ngx_copy(b->last, header[i].value.data, |
702 header[i].value.len); | |
507 | 703 |
509 | 704 *b->last++ = CR; *b->last++ = LF; |
507 | 705 |
509 | 706 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
707 "http proxy header: \"%V: %V\"", | |
708 &header[i].key, &header[i].value); | |
709 } | |
507 | 710 } |
711 | |
577 | 712 |
507 | 713 /* add "\r\n" at the header end */ |
714 *b->last++ = CR; *b->last++ = LF; | |
715 | |
577 | 716 if (plcf->body_set) { |
717 e.ip = plcf->body_set->elts; | |
718 e.pos = b->last; | |
719 | |
720 while (*(uintptr_t *) e.ip) { | |
721 code = *(ngx_http_script_code_pt *) e.ip; | |
722 code((ngx_http_script_engine_t *) &e); | |
723 } | |
579 | 724 |
725 b->last = e.pos; | |
577 | 726 } |
727 | |
507 | 728 #if (NGX_DEBUG) |
729 { | |
730 ngx_str_t s; | |
731 | |
732 s.len = b->last - b->pos; | |
733 s.data = b->pos; | |
734 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
735 "http proxy header:\n\"%V\"", &s); | |
736 } | |
737 #endif | |
738 | |
577 | 739 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) { |
509 | 740 |
741 body = u->request_bufs; | |
742 u->request_bufs = cl; | |
743 | |
744 while (body) { | |
745 b = ngx_alloc_buf(r->pool); | |
746 if (b == NULL) { | |
747 return NGX_ERROR; | |
748 } | |
749 | |
750 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t)); | |
751 | |
752 cl->next = ngx_alloc_chain_link(r->pool); | |
753 if (cl->next == NULL) { | |
754 return NGX_ERROR; | |
755 } | |
756 | |
757 cl = cl->next; | |
758 cl->buf = b; | |
759 | |
760 body = body->next; | |
761 } | |
762 | |
611 | 763 b->flush = 1; |
764 | |
509 | 765 } else { |
766 u->request_bufs = cl; | |
767 } | |
768 | |
769 cl->next = NULL; | |
770 | |
507 | 771 return NGX_OK; |
772 } | |
773 | |
774 | |
775 static ngx_int_t | |
776 ngx_http_proxy_reinit_request(ngx_http_request_t *r) | |
777 { | |
509 | 778 ngx_http_proxy_ctx_t *p; |
779 | |
780 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | |
781 | |
782 if (p == NULL) { | |
783 return NGX_OK; | |
784 } | |
785 | |
786 p->status = 0; | |
787 p->status_count = 0; | |
788 p->status_start = NULL; | |
789 p->status_end = NULL; | |
790 | |
791 r->upstream->process_header = ngx_http_proxy_process_status_line; | |
792 | |
793 return NGX_OK; | |
794 } | |
795 | |
796 | |
797 static ngx_int_t | |
798 ngx_http_proxy_process_status_line(ngx_http_request_t *r) | |
799 { | |
800 ngx_int_t rc; | |
801 ngx_http_upstream_t *u; | |
802 ngx_http_proxy_ctx_t *p; | |
803 | |
804 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | |
805 | |
806 if (p == NULL) { | |
577 | 807 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
509 | 808 } |
809 | |
810 rc = ngx_http_proxy_parse_status_line(r, p); | |
811 | |
812 if (rc == NGX_AGAIN) { | |
813 return rc; | |
814 } | |
815 | |
816 u = r->upstream; | |
817 | |
818 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) { | |
819 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
577 | 820 "upstream sent no valid HTTP/1.0 header"); |
509 | 821 |
619 | 822 #if 0 |
509 | 823 if (u->accel) { |
824 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
825 } | |
619 | 826 #endif |
509 | 827 |
828 r->http_version = NGX_HTTP_VERSION_9; | |
829 p->status = NGX_HTTP_OK; | |
577 | 830 |
509 | 831 return NGX_OK; |
832 } | |
833 | |
529 | 834 u->headers_in.status_n = p->status; |
509 | 835 u->state->status = p->status; |
836 | |
529 | 837 u->headers_in.status_line.len = p->status_end - p->status_start; |
838 u->headers_in.status_line.data = ngx_palloc(r->pool, | |
839 u->headers_in.status_line.len); | |
840 if (u->headers_in.status_line.data == NULL) { | |
509 | 841 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
842 } | |
573 | 843 |
529 | 844 ngx_memcpy(u->headers_in.status_line.data, p->status_start, |
845 u->headers_in.status_line.len); | |
509 | 846 |
847 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
848 "http proxy status %ui \"%V\"", | |
529 | 849 u->headers_in.status, &u->headers_in.status_line); |
509 | 850 |
851 u->process_header = ngx_http_proxy_process_header; | |
852 | |
853 return ngx_http_proxy_process_header(r); | |
854 } | |
855 | |
856 | |
857 static ngx_int_t | |
858 ngx_http_proxy_parse_status_line(ngx_http_request_t *r, ngx_http_proxy_ctx_t *p) | |
859 { | |
860 u_char ch; | |
861 u_char *pos; | |
862 ngx_http_upstream_t *u; | |
863 enum { | |
864 sw_start = 0, | |
865 sw_H, | |
866 sw_HT, | |
867 sw_HTT, | |
868 sw_HTTP, | |
869 sw_first_major_digit, | |
870 sw_major_digit, | |
871 sw_first_minor_digit, | |
872 sw_minor_digit, | |
873 sw_status, | |
874 sw_space_after_status, | |
875 sw_status_text, | |
876 sw_almost_done | |
577 | 877 } state; |
509 | 878 |
879 u = r->upstream; | |
880 | |
881 state = r->state; | |
882 | |
581 | 883 for (pos = u->buffer.pos; pos < u->buffer.last; pos++) { |
509 | 884 ch = *pos; |
885 | |
886 switch (state) { | |
887 | |
888 /* "HTTP/" */ | |
889 case sw_start: | |
890 switch (ch) { | |
577 | 891 case 'H': |
509 | 892 state = sw_H; |
893 break; | |
894 default: | |
895 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
896 } | |
897 break; | |
898 | |
577 | 899 case sw_H: |
509 | 900 switch (ch) { |
577 | 901 case 'T': |
509 | 902 state = sw_HT; |
903 break; | |
904 default: | |
905 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
906 } | |
907 break; | |
908 | |
909 case sw_HT: | |
910 switch (ch) { | |
577 | 911 case 'T': |
509 | 912 state = sw_HTT; |
913 break; | |
914 default: | |
915 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
916 } | |
917 break; | |
918 | |
919 case sw_HTT: | |
920 switch (ch) { | |
577 | 921 case 'P': |
509 | 922 state = sw_HTTP; |
923 break; | |
924 default: | |
925 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
926 } | |
927 break; | |
928 | |
929 case sw_HTTP: | |
930 switch (ch) { | |
577 | 931 case '/': |
509 | 932 state = sw_first_major_digit; |
933 break; | |
934 default: | |
935 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
936 } | |
937 break; | |
938 | |
939 /* the first digit of major HTTP version */ | |
940 case sw_first_major_digit: | |
941 if (ch < '1' || ch > '9') { | |
942 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
943 } | |
944 | |
945 state = sw_major_digit; | |
946 break; | |
947 | |
948 /* the major HTTP version or dot */ | |
949 case sw_major_digit: | |
950 if (ch == '.') { | |
951 state = sw_first_minor_digit; | |
952 break; | |
953 } | |
954 | |
955 if (ch < '0' || ch > '9') { | |
956 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
957 } | |
958 | |
959 break; | |
960 | |
961 /* the first digit of minor HTTP version */ | |
962 case sw_first_minor_digit: | |
963 if (ch < '0' || ch > '9') { | |
964 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
965 } | |
966 | |
967 state = sw_minor_digit; | |
968 break; | |
969 | |
970 /* the minor HTTP version or the end of the request line */ | |
971 case sw_minor_digit: | |
972 if (ch == ' ') { | |
973 state = sw_status; | |
974 break; | |
975 } | |
976 | |
977 if (ch < '0' || ch > '9') { | |
978 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
979 } | |
980 | |
981 break; | |
982 | |
983 /* HTTP status code */ | |
984 case sw_status: | |
619 | 985 if (ch == ' ') { |
986 break; | |
987 } | |
988 | |
509 | 989 if (ch < '0' || ch > '9') { |
990 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
991 } | |
992 | |
993 p->status = p->status * 10 + ch - '0'; | |
994 | |
995 if (++p->status_count == 3) { | |
996 state = sw_space_after_status; | |
997 p->status_start = pos - 2; | |
998 } | |
999 | |
1000 break; | |
1001 | |
1002 /* space or end of line */ | |
1003 case sw_space_after_status: | |
1004 switch (ch) { | |
577 | 1005 case ' ': |
509 | 1006 state = sw_status_text; |
1007 break; | |
1008 case '.': /* IIS may send 403.1, 403.2, etc */ | |
1009 state = sw_status_text; | |
1010 break; | |
1011 case CR: | |
1012 state = sw_almost_done; | |
1013 break; | |
1014 case LF: | |
1015 goto done; | |
1016 default: | |
1017 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1018 } | |
1019 break; | |
1020 | |
1021 /* any text until end of line */ | |
1022 case sw_status_text: | |
1023 switch (ch) { | |
1024 case CR: | |
1025 state = sw_almost_done; | |
1026 | |
1027 break; | |
1028 case LF: | |
1029 goto done; | |
1030 } | |
1031 break; | |
1032 | |
527 | 1033 /* end of status line */ |
509 | 1034 case sw_almost_done: |
1035 p->status_end = pos - 1; | |
1036 switch (ch) { | |
1037 case LF: | |
1038 goto done; | |
1039 default: | |
1040 return NGX_HTTP_PROXY_PARSE_NO_HEADER; | |
1041 } | |
1042 } | |
1043 } | |
1044 | |
581 | 1045 u->buffer.pos = pos; |
509 | 1046 r->state = state; |
1047 | |
1048 return NGX_AGAIN; | |
1049 | |
1050 done: | |
1051 | |
581 | 1052 u->buffer.pos = pos + 1; |
509 | 1053 |
1054 if (p->status_end == NULL) { | |
1055 p->status_end = pos; | |
1056 } | |
1057 | |
1058 r->state = sw_start; | |
1059 | |
507 | 1060 return NGX_OK; |
1061 } | |
1062 | |
1063 | |
1064 static ngx_int_t | |
1065 ngx_http_proxy_process_header(ngx_http_request_t *r) | |
1066 { | |
509 | 1067 ngx_int_t rc; |
649 | 1068 ngx_uint_t i; |
509 | 1069 ngx_table_elt_t *h; |
1070 ngx_http_upstream_header_t *hh; | |
1071 ngx_http_upstream_main_conf_t *umcf; | |
1072 | |
1073 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); | |
1074 | |
1075 for ( ;; ) { | |
1076 | |
581 | 1077 rc = ngx_http_parse_header_line(r, &r->upstream->buffer); |
509 | 1078 |
1079 if (rc == NGX_OK) { | |
1080 | |
1081 /* a header line has been parsed successfully */ | |
507 | 1082 |
509 | 1083 h = ngx_list_push(&r->upstream->headers_in.headers); |
1084 if (h == NULL) { | |
1085 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1086 } | |
1087 | |
1088 h->hash = r->header_hash; | |
1089 | |
1090 h->key.len = r->header_name_end - r->header_name_start; | |
1091 h->value.len = r->header_end - r->header_start; | |
1092 | |
1093 h->key.data = ngx_palloc(r->pool, | |
649 | 1094 h->key.len + 1 + h->value.len + 1 + h->key.len); |
509 | 1095 if (h->key.data == NULL) { |
1096 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1097 } | |
1098 | |
1099 h->value.data = h->key.data + h->key.len + 1; | |
649 | 1100 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1; |
507 | 1101 |
509 | 1102 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); |
1103 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); | |
1104 | |
649 | 1105 if (h->key.len == r->lowcase_index) { |
1106 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len); | |
1107 | |
1108 } else { | |
1109 for (i = 0; i < h->key.len; i++) { | |
1110 h->lowcase_key[i] = ngx_tolower(h->lowcase_key[i]); | |
509 | 1111 } |
1112 } | |
1113 | |
649 | 1114 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash, |
1115 h->lowcase_key, h->key.len); | |
1116 | |
1117 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { | |
1118 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1119 } | |
1120 | |
509 | 1121 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1122 "http proxy header: \"%V: %V\"", | |
1123 &h->key, &h->value); | |
1124 | |
1125 continue; | |
1126 } | |
1127 | |
1128 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
1129 | |
1130 /* a whole header has been parsed successfully */ | |
1131 | |
1132 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1133 "http proxy header done"); | |
1134 | |
649 | 1135 /* |
1136 * if no "Server" and "Date" in header line, | |
1137 * then add the special empty headers | |
1138 */ | |
1139 | |
1140 if (r->upstream->headers_in.server == NULL) { | |
1141 h = ngx_list_push(&r->upstream->headers_in.headers); | |
1142 if (h == NULL) { | |
1143 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1144 } | |
1145 | |
1146 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash( | |
1147 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r'); | |
1148 | |
1149 h->key.len = sizeof("Server") - 1; | |
1150 h->key.data = (u_char *) "Server"; | |
1151 h->value.len = 0; | |
1152 h->value.data = NULL; | |
1153 h->lowcase_key = (u_char *) "server"; | |
1154 } | |
1155 | |
1156 if (r->upstream->headers_in.date == NULL) { | |
1157 h = ngx_list_push(&r->upstream->headers_in.headers); | |
1158 if (h == NULL) { | |
1159 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1160 } | |
1161 | |
1162 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e'); | |
1163 | |
1164 h->key.len = sizeof("Date") - 1; | |
1165 h->key.data = (u_char *) "Date"; | |
1166 h->value.len = 0; | |
1167 h->value.data = NULL; | |
1168 h->lowcase_key = (u_char *) "date"; | |
1169 } | |
1170 | |
509 | 1171 return NGX_OK; |
1172 } | |
1173 | |
521 | 1174 if (rc == NGX_AGAIN) { |
1175 return NGX_AGAIN; | |
1176 } | |
1177 | |
509 | 1178 /* there was error while a header line parsing */ |
1179 | |
1180 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
619 | 1181 "upstream sent invalid header"); |
509 | 1182 |
1183 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1184 } | |
507 | 1185 } |
1186 | |
1187 | |
1188 static void | |
1189 ngx_http_proxy_abort_request(ngx_http_request_t *r) | |
1190 { | |
1191 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1192 "abort http proxy request"); | |
577 | 1193 |
507 | 1194 return; |
1195 } | |
1196 | |
1197 | |
1198 static void | |
1199 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | |
577 | 1200 { |
507 | 1201 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1202 "finalize http proxy request"); | |
1203 | |
1204 return; | |
1205 } | |
1206 | |
1207 | |
573 | 1208 static ngx_int_t |
1209 ngx_http_proxy_host_variable(ngx_http_request_t *r, | |
1210 ngx_http_variable_value_t *v, uintptr_t data) | |
507 | 1211 { |
509 | 1212 ngx_http_proxy_loc_conf_t *plcf; |
507 | 1213 |
509 | 1214 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); |
507 | 1215 |
573 | 1216 v->len = plcf->host_header.len; |
577 | 1217 v->valid = 1; |
573 | 1218 v->no_cachable = 0; |
1219 v->not_found = 0; | |
1220 v->data = plcf->host_header.data; | |
507 | 1221 |
573 | 1222 return NGX_OK; |
507 | 1223 } |
1224 | |
1225 | |
573 | 1226 static ngx_int_t |
1227 ngx_http_proxy_port_variable(ngx_http_request_t *r, | |
1228 ngx_http_variable_value_t *v, uintptr_t data) | |
507 | 1229 { |
1230 ngx_http_proxy_loc_conf_t *plcf; | |
1231 | |
1232 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | |
1233 | |
573 | 1234 v->len = plcf->port_text.len; |
577 | 1235 v->valid = 1; |
573 | 1236 v->no_cachable = 0; |
1237 v->not_found = 0; | |
1238 v->data = plcf->port_text.data; | |
509 | 1239 |
573 | 1240 return NGX_OK; |
509 | 1241 } |
1242 | |
1243 | |
573 | 1244 static ngx_int_t |
509 | 1245 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, |
573 | 1246 ngx_http_variable_value_t *v, uintptr_t data) |
509 | 1247 { |
573 | 1248 u_char *p; |
509 | 1249 |
577 | 1250 v->valid = 1; |
573 | 1251 v->no_cachable = 0; |
1252 v->not_found = 0; | |
509 | 1253 |
1254 if (r->headers_in.x_forwarded_for == NULL) { | |
573 | 1255 v->len = r->connection->addr_text.len; |
1256 v->data = r->connection->addr_text.data; | |
1257 return NGX_OK; | |
509 | 1258 } |
1259 | |
573 | 1260 v->len = r->headers_in.x_forwarded_for->value.len |
1261 + sizeof(", ") - 1 + r->connection->addr_text.len; | |
509 | 1262 |
573 | 1263 p = ngx_palloc(r->pool, v->len); |
509 | 1264 if (p == NULL) { |
573 | 1265 return NGX_ERROR; |
509 | 1266 } |
1267 | |
573 | 1268 v->data = p; |
509 | 1269 |
573 | 1270 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data, |
1271 r->headers_in.x_forwarded_for->value.len); | |
509 | 1272 |
1273 *p++ = ','; *p++ = ' '; | |
1274 | |
1275 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); | |
1276 | |
573 | 1277 return NGX_OK; |
509 | 1278 } |
1279 | |
1280 | |
1281 static ngx_int_t | |
577 | 1282 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r, |
1283 ngx_http_variable_value_t *v, uintptr_t data) | |
1284 { | |
1285 ngx_http_proxy_ctx_t *p; | |
1286 | |
1287 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); | |
1288 | |
1289 if (p == NULL) { | |
1290 v->not_found = 1; | |
1291 return NGX_OK; | |
1292 } | |
1293 | |
1294 v->valid = 1; | |
1295 v->no_cachable = 0; | |
1296 v->not_found = 0; | |
1297 | |
1298 v->data = ngx_palloc(r->connection->pool, NGX_SIZE_T_LEN); | |
1299 | |
1300 if (v->data == NULL) { | |
1301 return NGX_ERROR; | |
1302 } | |
1303 | |
1304 v->len = ngx_sprintf(v->data, "%uz", p->internal_body_length) - v->data; | |
1305 | |
1306 return NGX_OK; | |
1307 } | |
1308 | |
1309 | |
1310 static ngx_int_t | |
509 | 1311 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h, |
1312 size_t prefix) | |
1313 { | |
1314 ngx_int_t rc; | |
1315 ngx_uint_t i; | |
1316 ngx_http_proxy_loc_conf_t *plcf; | |
1317 ngx_http_proxy_redirect_t *pr; | |
1318 | |
1319 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); | |
1320 | |
1321 pr = plcf->redirects->elts; | |
1322 | |
1323 if (pr == NULL) { | |
1324 return NGX_DECLINED; | |
1325 } | |
1326 | |
1327 for (i = 0; i < plcf->redirects->nelts; i++) { | |
605 | 1328 rc = pr[i].handler(r, h, prefix, &pr[i]); |
509 | 1329 |
1330 if (rc != NGX_DECLINED) { | |
1331 return rc; | |
1332 } | |
1333 } | |
1334 | |
1335 return NGX_DECLINED; | |
1336 } | |
1337 | |
1338 | |
1339 static ngx_int_t | |
1340 ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h, | |
1341 size_t prefix, ngx_http_proxy_redirect_t *pr) | |
1342 { | |
1343 size_t len; | |
1344 u_char *data, *p; | |
1345 | |
1346 if (pr->redirect.len > h->value.len - prefix | |
1347 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data, | |
1348 pr->redirect.len) != 0) | |
1349 { | |
1350 return NGX_DECLINED; | |
1351 } | |
1352 | |
1353 len = prefix + pr->replacement.text.len + h->value.len - pr->redirect.len; | |
507 | 1354 |
509 | 1355 data = ngx_palloc(r->pool, len); |
1356 if (data == NULL) { | |
1357 return NGX_ERROR; | |
1358 } | |
1359 | |
1360 p = data; | |
1361 | |
573 | 1362 p = ngx_copy(p, h->value.data, prefix); |
509 | 1363 |
653 | 1364 if (pr->replacement.text.len) { |
1365 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len); | |
1366 } | |
509 | 1367 |
1368 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len, | |
1369 h->value.len - pr->redirect.len - prefix); | |
1370 | |
1371 h->value.len = len; | |
1372 h->value.data = data; | |
1373 | |
1374 return NGX_OK; | |
1375 } | |
1376 | |
1377 | |
1378 static ngx_int_t | |
1379 ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h, | |
1380 size_t prefix, ngx_http_proxy_redirect_t *pr) | |
1381 { | |
1382 size_t len; | |
1383 u_char *data, *p; | |
1384 ngx_http_script_code_pt code; | |
1385 ngx_http_script_engine_t e; | |
1386 ngx_http_script_len_code_pt lcode; | |
1387 | |
1388 if (pr->redirect.len > h->value.len - prefix | |
1389 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data, | |
1390 pr->redirect.len) != 0) | |
1391 { | |
1392 return NGX_DECLINED; | |
1393 } | |
1394 | |
1395 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
1396 | |
1397 e.ip = pr->replacement.vars.lengths; | |
1398 e.request = r; | |
1399 | |
723
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1400 len = prefix + h->value.len - pr->redirect.len; |
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1401 |
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1402 while (*(uintptr_t *) e.ip) { |
509 | 1403 lcode = *(ngx_http_script_len_code_pt *) e.ip; |
723
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1404 len += lcode(&e); |
509 | 1405 } |
1406 | |
1407 data = ngx_palloc(r->pool, len); | |
1408 if (data == NULL) { | |
1409 return NGX_ERROR; | |
1410 } | |
1411 | |
1412 p = data; | |
1413 | |
573 | 1414 p = ngx_copy(p, h->value.data, prefix); |
509 | 1415 |
1416 e.ip = pr->replacement.vars.values; | |
1417 e.pos = p; | |
1418 | |
1419 while (*(uintptr_t *) e.ip) { | |
1420 code = *(ngx_http_script_code_pt *) e.ip; | |
1421 code(&e); | |
1422 } | |
1423 | |
723
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1424 ngx_memcpy(e.pos, h->value.data + prefix + pr->redirect.len, |
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1425 h->value.len - pr->redirect.len - prefix); |
ceb5ef29b119
fix proxy_redirect with variable
Igor Sysoev <igor@sysoev.ru>
parents:
673
diff
changeset
|
1426 |
509 | 1427 h->value.len = len; |
1428 h->value.data = data; | |
1429 | |
1430 return NGX_OK; | |
1431 } | |
1432 | |
1433 | |
1434 static ngx_int_t | |
1435 ngx_http_proxy_add_variables(ngx_conf_t *cf) | |
1436 { | |
1437 ngx_http_variable_t *var, *v; | |
1438 | |
1439 for (v = ngx_http_proxy_vars; v->name.len; v++) { | |
1440 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
1441 if (var == NULL) { | |
1442 return NGX_ERROR; | |
1443 } | |
1444 | |
637 | 1445 var->get_handler = v->get_handler; |
509 | 1446 var->data = v->data; |
1447 } | |
1448 | |
1449 return NGX_OK; | |
507 | 1450 } |
1451 | |
1452 | |
1453 static void * | |
1454 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf) | |
1455 { | |
1456 ngx_http_proxy_loc_conf_t *conf; | |
1457 | |
1458 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t)); | |
1459 if (conf == NULL) { | |
1460 return NGX_CONF_ERROR; | |
1461 } | |
1462 | |
1463 /* | |
1464 * set by ngx_pcalloc(): | |
1465 * | |
1466 * conf->upstream.bufs.num = 0; | |
1467 * conf->upstream.next_upstream = 0; | |
1468 * conf->upstream.temp_path = NULL; | |
649 | 1469 * conf->upstream.hide_headers_hash = { NULL, 0 }; |
1470 * conf->upstream.hide_headers = NULL; | |
1471 * conf->upstream.pass_headers = NULL; | |
509 | 1472 * conf->upstream.schema = { 0, NULL }; |
1473 * conf->upstream.uri = { 0, NULL }; | |
1474 * conf->upstream.location = NULL; | |
1475 * | |
561 | 1476 * conf->method = NULL; |
509 | 1477 * conf->headers_source = NULL; |
1478 * conf->headers_set_len = NULL; | |
1479 * conf->headers_set = NULL; | |
1480 * conf->headers_set_hash = NULL; | |
577 | 1481 * conf->body_set_len = NULL; |
1482 * conf->body_set = NULL; | |
1483 * conf->body_source = { 0, NULL }; | |
509 | 1484 * conf->rewrite_locations = NULL; |
507 | 1485 */ |
577 | 1486 |
581 | 1487 conf->upstream.buffering = NGX_CONF_UNSET; |
629 | 1488 conf->upstream.ignore_client_abort = NGX_CONF_UNSET; |
581 | 1489 |
507 | 1490 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; |
1491 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | |
1492 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; | |
1493 | |
1494 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE; | |
581 | 1495 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; |
529 | 1496 |
1497 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE; | |
577 | 1498 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE; |
529 | 1499 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE; |
509 | 1500 |
561 | 1501 conf->upstream.max_fails = NGX_CONF_UNSET_UINT; |
1502 conf->upstream.fail_timeout = NGX_CONF_UNSET; | |
1503 | |
509 | 1504 conf->upstream.pass_request_headers = NGX_CONF_UNSET; |
1505 conf->upstream.pass_request_body = NGX_CONF_UNSET; | |
561 | 1506 |
657 | 1507 conf->upstream.intercept_errors = NGX_CONF_UNSET; |
507 | 1508 |
1509 /* "proxy_cyclic_temp_file" is disabled */ | |
1510 conf->upstream.cyclic_temp_file = 0; | |
1511 | |
509 | 1512 conf->redirect = NGX_CONF_UNSET; |
649 | 1513 conf->upstream.change_buffering = 1; |
507 | 1514 |
1515 return conf; | |
1516 } | |
1517 | |
1518 | |
1519 static char * | |
1520 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
1521 { | |
1522 ngx_http_proxy_loc_conf_t *prev = parent; | |
1523 ngx_http_proxy_loc_conf_t *conf = child; | |
1524 | |
509 | 1525 u_char *p; |
1526 size_t size; | |
1527 uintptr_t *code; | |
649 | 1528 ngx_str_t *header; |
1529 ngx_uint_t i, j; | |
663 | 1530 ngx_peer_t *peer; |
649 | 1531 ngx_array_t hide_headers; |
1532 ngx_keyval_t *src, *s, *h; | |
1533 ngx_hash_key_t *hk; | |
1534 ngx_hash_init_t hash; | |
509 | 1535 ngx_http_proxy_redirect_t *pr; |
1536 ngx_http_script_compile_t sc; | |
1537 ngx_http_script_copy_code_t *copy; | |
1538 | |
581 | 1539 ngx_conf_merge_value(conf->upstream.buffering, |
1540 prev->upstream.buffering, 1); | |
1541 | |
629 | 1542 ngx_conf_merge_value(conf->upstream.ignore_client_abort, |
1543 prev->upstream.ignore_client_abort, 0); | |
1544 | |
507 | 1545 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, |
1546 prev->upstream.connect_timeout, 60000); | |
1547 | |
1548 ngx_conf_merge_msec_value(conf->upstream.send_timeout, | |
1549 prev->upstream.send_timeout, 60000); | |
1550 | |
1551 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
1552 prev->upstream.read_timeout, 60000); | |
1553 | |
1554 ngx_conf_merge_size_value(conf->upstream.send_lowat, | |
1555 prev->upstream.send_lowat, 0); | |
1556 | |
581 | 1557 ngx_conf_merge_size_value(conf->upstream.buffer_size, |
1558 prev->upstream.buffer_size, | |
507 | 1559 (size_t) ngx_pagesize); |
1560 | |
1561 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, | |
1562 8, ngx_pagesize); | |
1563 | |
1564 if (conf->upstream.bufs.num < 2) { | |
577 | 1565 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
507 | 1566 "there must be at least 2 \"proxy_buffers\""); |
1567 return NGX_CONF_ERROR; | |
1568 } | |
577 | 1569 |
507 | 1570 |
581 | 1571 size = conf->upstream.buffer_size; |
577 | 1572 if (size < conf->upstream.bufs.size) { |
507 | 1573 size = conf->upstream.bufs.size; |
1574 } | |
1575 | |
1576 | |
529 | 1577 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf, |
1578 prev->upstream.busy_buffers_size_conf, | |
507 | 1579 NGX_CONF_UNSET_SIZE); |
1580 | |
529 | 1581 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) { |
507 | 1582 conf->upstream.busy_buffers_size = 2 * size; |
529 | 1583 } else { |
1584 conf->upstream.busy_buffers_size = | |
1585 conf->upstream.busy_buffers_size_conf; | |
1586 } | |
577 | 1587 |
529 | 1588 if (conf->upstream.busy_buffers_size < size) { |
507 | 1589 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1590 "\"proxy_busy_buffers_size\" must be equal or bigger than " | |
1591 "maximum of the value of \"proxy_header_buffer_size\" and " | |
1592 "one of the \"proxy_buffers\""); | |
1593 | |
1594 return NGX_CONF_ERROR; | |
529 | 1595 } |
507 | 1596 |
529 | 1597 if (conf->upstream.busy_buffers_size |
1598 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size) | |
507 | 1599 { |
1600 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1601 "\"proxy_busy_buffers_size\" must be less than " | |
1602 "the size of all \"proxy_buffers\" minus one buffer"); | |
1603 | |
1604 return NGX_CONF_ERROR; | |
1605 } | |
529 | 1606 |
507 | 1607 |
529 | 1608 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf, |
1609 prev->upstream.temp_file_write_size_conf, | |
507 | 1610 NGX_CONF_UNSET_SIZE); |
1611 | |
529 | 1612 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) { |
507 | 1613 conf->upstream.temp_file_write_size = 2 * size; |
529 | 1614 } else { |
1615 conf->upstream.temp_file_write_size = | |
1616 conf->upstream.temp_file_write_size_conf; | |
1617 } | |
577 | 1618 |
529 | 1619 if (conf->upstream.temp_file_write_size < size) { |
507 | 1620 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1621 "\"proxy_temp_file_write_size\" must be equal or bigger than " | |
1622 "maximum of the value of \"proxy_header_buffer_size\" and " | |
1623 "one of the \"proxy_buffers\""); | |
1624 | |
1625 return NGX_CONF_ERROR; | |
1626 } | |
1627 | |
529 | 1628 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf, |
1629 prev->upstream.max_temp_file_size_conf, | |
507 | 1630 NGX_CONF_UNSET_SIZE); |
1631 | |
529 | 1632 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) { |
507 | 1633 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024; |
529 | 1634 } else { |
1635 conf->upstream.max_temp_file_size = | |
1636 conf->upstream.max_temp_file_size_conf; | |
1637 } | |
507 | 1638 |
529 | 1639 if (conf->upstream.max_temp_file_size != 0 |
1640 && conf->upstream.max_temp_file_size < size) | |
507 | 1641 { |
1642 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
561 | 1643 "\"proxy_max_temp_file_size\" must be equal to zero to disable " |
507 | 1644 "the temporary files usage or must be equal or bigger than " |
561 | 1645 "maximum of the value of \"proxy_header_buffer_size\" and " |
1646 "one of the \"proxy_buffers\""); | |
507 | 1647 |
1648 return NGX_CONF_ERROR; | |
1649 } | |
1650 | |
529 | 1651 |
507 | 1652 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, |
1653 prev->upstream.next_upstream, | |
1654 (NGX_CONF_BITMASK_SET | |
1655 |NGX_HTTP_UPSTREAM_FT_ERROR | |
1656 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
1657 | |
665 | 1658 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { |
1659 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
1660 |NGX_HTTP_UPSTREAM_FT_OFF; | |
1661 } | |
1662 | |
663 | 1663 ngx_conf_merge_uint_value(conf->upstream.max_fails, |
561 | 1664 prev->upstream.max_fails, 1); |
1665 | |
1666 ngx_conf_merge_sec_value(conf->upstream.fail_timeout, | |
1667 prev->upstream.fail_timeout, 10); | |
1668 | |
663 | 1669 if (conf->upstream_peers) { |
1670 peer = conf->upstream_peers->peers->peer; | |
651 | 1671 for (i = 0; i < conf->upstream_peers->peers->number; i++) { |
663 | 1672 ngx_conf_init_uint_value(peer[i].weight, 1); |
1673 peer[i].current_weight = peer[i].weight; | |
1674 ngx_conf_init_uint_value(peer[i].max_fails, | |
1675 conf->upstream.max_fails); | |
1676 ngx_conf_init_value(peer[i].fail_timeout, | |
1677 conf->upstream.fail_timeout); | |
561 | 1678 } |
1679 } | |
1680 | |
509 | 1681 ngx_conf_merge_path_value(conf->upstream.temp_path, |
1682 prev->upstream.temp_path, | |
1683 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0, | |
1684 ngx_garbage_collector_temp_handler, cf); | |
507 | 1685 |
561 | 1686 if (conf->method.len == 0) { |
1687 conf->method = prev->method; | |
1688 | |
1689 } else { | |
1690 conf->method.data[conf->method.len] = ' '; | |
1691 conf->method.len++; | |
509 | 1692 } |
1693 | |
1694 ngx_conf_merge_value(conf->upstream.pass_request_headers, | |
1695 prev->upstream.pass_request_headers, 1); | |
1696 ngx_conf_merge_value(conf->upstream.pass_request_body, | |
1697 prev->upstream.pass_request_body, 1); | |
1698 | |
657 | 1699 ngx_conf_merge_value(conf->upstream.intercept_errors, |
1700 prev->upstream.intercept_errors, 0); | |
509 | 1701 |
1702 ngx_conf_merge_value(conf->redirect, prev->redirect, 1); | |
1703 | |
1704 if (conf->redirect) { | |
507 | 1705 |
509 | 1706 if (conf->redirects == NULL) { |
1707 conf->redirects = prev->redirects; | |
1708 } | |
1709 | |
1710 if (conf->redirects == NULL && conf->upstream.url.data) { | |
1711 | |
1712 conf->redirects = ngx_array_create(cf->pool, 1, | |
1713 sizeof(ngx_http_proxy_redirect_t)); | |
1714 if (conf->redirects == NULL) { | |
1715 return NGX_CONF_ERROR; | |
1716 } | |
1717 | |
1718 pr = ngx_array_push(conf->redirects); | |
1719 if (pr == NULL) { | |
1720 return NGX_CONF_ERROR; | |
1721 } | |
1722 | |
1723 pr->handler = ngx_http_proxy_rewrite_redirect_text; | |
1724 pr->redirect = conf->upstream.url; | |
653 | 1725 |
1726 if (conf->upstream.uri.len) { | |
1727 pr->replacement.text = conf->upstream.location; | |
1728 | |
1729 } else { | |
1730 pr->replacement.text.len = 0; | |
1731 pr->replacement.text.data = NULL; | |
1732 } | |
509 | 1733 } |
1734 } | |
1735 | |
649 | 1736 if (conf->upstream.hide_headers == NULL |
1737 && conf->upstream.pass_headers == NULL) | |
1738 { | |
1739 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1740 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1741 conf->upstream.hide_headers_hash = prev->upstream.hide_headers_hash; | |
1742 | |
1743 if (conf->upstream.hide_headers_hash.buckets) { | |
1744 goto peers; | |
1745 } | |
1746 | |
1747 } else { | |
1748 if (conf->upstream.hide_headers == NULL) { | |
1749 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1750 } | |
1751 | |
1752 if (conf->upstream.pass_headers == NULL) { | |
1753 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1754 } | |
1755 } | |
1756 | |
1757 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | |
1758 != NGX_OK) | |
1759 { | |
1760 return NGX_CONF_ERROR; | |
1761 } | |
1762 | |
1763 for (header = ngx_http_proxy_hide_headers; header->len; header++) { | |
1764 hk = ngx_array_push(&hide_headers); | |
1765 if (hk == NULL) { | |
1766 return NGX_CONF_ERROR; | |
1767 } | |
1768 | |
1769 hk->key = *header; | |
1770 hk->key_hash = ngx_hash_key_lc(header->data, header->len); | |
1771 hk->value = (void *) 1; | |
1772 } | |
1773 | |
1774 if (conf->upstream.hide_headers) { | |
1775 | |
1776 header = conf->upstream.hide_headers->elts; | |
1777 | |
1778 for (i = 0; i < conf->upstream.hide_headers->nelts; i++) { | |
1779 | |
1780 hk = hide_headers.elts; | |
1781 | |
1782 for (j = 0; j < hide_headers.nelts; j++) { | |
1783 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1784 goto exist; | |
1785 } | |
1786 } | |
1787 | |
1788 hk = ngx_array_push(&hide_headers); | |
1789 if (hk == NULL) { | |
1790 return NGX_CONF_ERROR; | |
1791 } | |
1792 | |
1793 hk->key = header[i]; | |
1794 hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len); | |
1795 hk->value = (void *) 1; | |
1796 | |
1797 exist: | |
1798 | |
1799 continue; | |
1800 } | |
1801 } | |
1802 | |
1803 if (conf->upstream.pass_headers) { | |
1804 | |
1805 hk = hide_headers.elts; | |
1806 header = conf->upstream.pass_headers->elts; | |
1807 | |
1808 for (i = 0; i < conf->upstream.pass_headers->nelts; i++) { | |
1809 for (j = 0; j < hide_headers.nelts; j++) { | |
1810 | |
1811 if (hk[j].key.data == NULL) { | |
1812 continue; | |
1813 } | |
1814 | |
1815 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1816 hk[j].key.data = NULL; | |
1817 break; | |
1818 } | |
1819 } | |
1820 } | |
1821 } | |
1822 | |
1823 hash.hash = &conf->upstream.hide_headers_hash; | |
1824 hash.key = ngx_hash_key_lc; | |
1825 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
1826 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 1827 hash.name = "proxy_hide_headers_hash"; |
1828 hash.pool = cf->pool; | |
1829 hash.temp_pool = NULL; | |
1830 | |
1831 if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) { | |
1832 return NGX_CONF_ERROR; | |
1833 } | |
1834 | |
1835 peers: | |
507 | 1836 |
651 | 1837 if (conf->upstream_peers == NULL) { |
1838 conf->upstream_peers = prev->upstream_peers; | |
629 | 1839 |
1840 conf->host_header = prev->host_header; | |
1841 conf->port_text = prev->port_text; | |
1842 conf->upstream.schema = prev->upstream.schema; | |
507 | 1843 } |
1844 | |
577 | 1845 |
1846 if (conf->body_source.data == NULL) { | |
1847 conf->body_source = prev->body_source; | |
1848 conf->body_set_len = prev->body_set_len; | |
1849 conf->body_set = prev->body_set; | |
1850 } | |
1851 | |
579 | 1852 if (conf->body_source.data && conf->body_set_len == NULL) { |
577 | 1853 |
1854 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1855 | |
1856 sc.cf = cf; | |
1857 sc.source = &conf->body_source; | |
1858 sc.flushes = &conf->flushes; | |
1859 sc.lengths = &conf->body_set_len; | |
1860 sc.values = &conf->body_set; | |
1861 sc.complete_lengths = 1; | |
1862 sc.complete_values = 1; | |
1863 | |
1864 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1865 return NGX_CONF_ERROR; | |
1866 } | |
1867 | |
1868 if (conf->headers_source == NULL) { | |
1869 conf->headers_source = ngx_array_create(cf->pool, 4, | |
649 | 1870 sizeof(ngx_keyval_t)); |
577 | 1871 if (conf->headers_source == NULL) { |
1872 return NGX_CONF_ERROR; | |
1873 } | |
1874 } | |
1875 | |
1876 s = ngx_array_push(conf->headers_source); | |
1877 if (s == NULL) { | |
1878 return NGX_CONF_ERROR; | |
1879 } | |
1880 | |
1881 s->key.len = sizeof("Content-Length") - 1; | |
1882 s->key.data = (u_char *) "Content-Length"; | |
1883 s->value.len = sizeof("$proxy_internal_body_length") - 1; | |
1884 s->value.data = (u_char *) "$proxy_internal_body_length"; | |
1885 } | |
1886 | |
1887 | |
509 | 1888 if (conf->headers_source == NULL) { |
573 | 1889 conf->flushes = prev->flushes; |
507 | 1890 conf->headers_set_len = prev->headers_set_len; |
1891 conf->headers_set = prev->headers_set; | |
1892 conf->headers_set_hash = prev->headers_set_hash; | |
573 | 1893 conf->headers_source = prev->headers_source; |
507 | 1894 } |
1895 | |
649 | 1896 if (conf->headers_set_hash.buckets) { |
509 | 1897 return NGX_CONF_OK; |
1898 } | |
1899 | |
1900 | |
649 | 1901 conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t)); |
509 | 1902 if (conf->headers_names == NULL) { |
1903 return NGX_CONF_ERROR; | |
1904 } | |
1905 | |
1906 if (conf->headers_source == NULL) { | |
1907 conf->headers_source = ngx_array_create(cf->pool, 4, | |
649 | 1908 sizeof(ngx_keyval_t)); |
509 | 1909 if (conf->headers_source == NULL) { |
1910 return NGX_CONF_ERROR; | |
1911 } | |
1912 } | |
507 | 1913 |
509 | 1914 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1); |
1915 if (conf->headers_set_len == NULL) { | |
1916 return NGX_CONF_ERROR; | |
1917 } | |
1918 | |
1919 conf->headers_set = ngx_array_create(cf->pool, 512, 1); | |
1920 if (conf->headers_set == NULL) { | |
1921 return NGX_CONF_ERROR; | |
1922 } | |
1923 | |
1924 | |
1925 src = conf->headers_source->elts; | |
1926 | |
1927 for (h = ngx_http_proxy_headers; h->key.len; h++) { | |
1928 | |
1929 for (i = 0; i < conf->headers_source->nelts; i++) { | |
1930 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) { | |
1931 goto next; | |
507 | 1932 } |
1933 } | |
1934 | |
509 | 1935 s = ngx_array_push(conf->headers_source); |
1936 if (s == NULL) { | |
507 | 1937 return NGX_CONF_ERROR; |
1938 } | |
1939 | |
509 | 1940 *s = *h; |
507 | 1941 |
517 | 1942 src = conf->headers_source->elts; |
1943 | |
509 | 1944 next: |
507 | 1945 |
509 | 1946 continue; |
1947 } | |
507 | 1948 |
577 | 1949 |
1950 src = conf->headers_source->elts; | |
509 | 1951 for (i = 0; i < conf->headers_source->nelts; i++) { |
507 | 1952 |
649 | 1953 hk = ngx_array_push(conf->headers_names); |
1954 if (hk == NULL) { | |
507 | 1955 return NGX_CONF_ERROR; |
1956 } | |
1957 | |
649 | 1958 hk->key = src[i].key; |
1959 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len); | |
1960 hk->value = (void *) 1; | |
509 | 1961 |
519 | 1962 if (src[i].value.len == 0) { |
1963 continue; | |
1964 } | |
1965 | |
509 | 1966 if (ngx_http_script_variables_count(&src[i].value) == 0) { |
1967 copy = ngx_array_push_n(conf->headers_set_len, | |
1968 sizeof(ngx_http_script_copy_code_t)); | |
1969 if (copy == NULL) { | |
1970 return NGX_CONF_ERROR; | |
1971 } | |
1972 | |
1973 copy->code = (ngx_http_script_code_pt) | |
1974 ngx_http_script_copy_len_code; | |
1975 copy->len = src[i].key.len + sizeof(": ") - 1 | |
1976 + src[i].value.len + sizeof(CRLF) - 1; | |
507 | 1977 |
1978 | |
509 | 1979 size = (sizeof(ngx_http_script_copy_code_t) |
1980 + src[i].key.len + sizeof(": ") - 1 | |
1981 + src[i].value.len + sizeof(CRLF) - 1 | |
1982 + sizeof(uintptr_t) - 1) | |
1983 & ~(sizeof(uintptr_t) - 1); | |
1984 | |
1985 copy = ngx_array_push_n(conf->headers_set, size); | |
1986 if (copy == NULL) { | |
1987 return NGX_CONF_ERROR; | |
1988 } | |
1989 | |
1990 copy->code = ngx_http_script_copy_code; | |
1991 copy->len = src[i].key.len + sizeof(": ") - 1 | |
1992 + src[i].value.len + sizeof(CRLF) - 1; | |
1993 | |
1994 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1995 | |
1996 p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | |
1997 *p++ = ':'; *p++ = ' '; | |
1998 p = ngx_cpymem(p, src[i].value.data, src[i].value.len); | |
1999 *p++ = CR; *p = LF; | |
2000 | |
2001 } else { | |
2002 copy = ngx_array_push_n(conf->headers_set_len, | |
2003 sizeof(ngx_http_script_copy_code_t)); | |
2004 if (copy == NULL) { | |
2005 return NGX_CONF_ERROR; | |
2006 } | |
2007 | |
2008 copy->code = (ngx_http_script_code_pt) | |
2009 ngx_http_script_copy_len_code; | |
2010 copy->len = src[i].key.len + sizeof(": ") - 1; | |
507 | 2011 |
2012 | |
509 | 2013 size = (sizeof(ngx_http_script_copy_code_t) |
2014 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1) | |
2015 & ~(sizeof(uintptr_t) - 1); | |
2016 | |
2017 copy = ngx_array_push_n(conf->headers_set, size); | |
2018 if (copy == NULL) { | |
2019 return NGX_CONF_ERROR; | |
2020 } | |
2021 | |
2022 copy->code = ngx_http_script_copy_code; | |
2023 copy->len = src[i].key.len + sizeof(": ") - 1; | |
2024 | |
2025 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
2026 p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | |
2027 *p++ = ':'; *p = ' '; | |
2028 | |
2029 | |
2030 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
2031 | |
2032 sc.cf = cf; | |
2033 sc.source = &src[i].value; | |
573 | 2034 sc.flushes = &conf->flushes; |
509 | 2035 sc.lengths = &conf->headers_set_len; |
2036 sc.values = &conf->headers_set; | |
2037 | |
2038 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
2039 return NGX_CONF_ERROR; | |
2040 } | |
2041 | |
2042 | |
2043 copy = ngx_array_push_n(conf->headers_set_len, | |
2044 sizeof(ngx_http_script_copy_code_t)); | |
2045 if (copy == NULL) { | |
2046 return NGX_CONF_ERROR; | |
2047 } | |
2048 | |
2049 copy->code = (ngx_http_script_code_pt) | |
2050 ngx_http_script_copy_len_code; | |
2051 copy->len = sizeof(CRLF) - 1; | |
2052 | |
2053 | |
2054 size = (sizeof(ngx_http_script_copy_code_t) | |
2055 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1) | |
2056 & ~(sizeof(uintptr_t) - 1); | |
2057 | |
2058 copy = ngx_array_push_n(conf->headers_set, size); | |
2059 if (copy == NULL) { | |
2060 return NGX_CONF_ERROR; | |
2061 } | |
2062 | |
2063 copy->code = ngx_http_script_copy_code; | |
2064 copy->len = sizeof(CRLF) - 1; | |
2065 | |
2066 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
2067 *p++ = CR; *p = LF; | |
507 | 2068 } |
2069 | |
509 | 2070 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t)); |
2071 if (code == NULL) { | |
507 | 2072 return NGX_CONF_ERROR; |
2073 } | |
2074 | |
509 | 2075 *code = (uintptr_t) NULL; |
2076 | |
2077 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t)); | |
2078 if (code == NULL) { | |
2079 return NGX_CONF_ERROR; | |
2080 } | |
2081 | |
2082 *code = (uintptr_t) NULL; | |
507 | 2083 } |
2084 | |
509 | 2085 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t)); |
2086 if (code == NULL) { | |
2087 return NGX_CONF_ERROR; | |
507 | 2088 } |
2089 | |
509 | 2090 *code = (uintptr_t) NULL; |
507 | 2091 |
2092 | |
649 | 2093 hash.hash = &conf->headers_set_hash; |
2094 hash.key = ngx_hash_key_lc; | |
2095 hash.max_size = 512; | |
2096 hash.bucket_size = ngx_cacheline_size; | |
2097 hash.name = "proxy_set_header_hash"; | |
2098 hash.pool = cf->pool; | |
2099 hash.temp_pool = NULL; | |
2100 | |
2101 if (ngx_hash_init(&hash, conf->headers_names->elts, | |
2102 conf->headers_names->nelts) | |
2103 != NGX_OK) | |
2104 { | |
2105 return NGX_CONF_ERROR; | |
2106 } | |
2107 | |
509 | 2108 return NGX_CONF_OK; |
507 | 2109 } |
2110 | |
2111 | |
2112 static char * | |
2113 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2114 { | |
509 | 2115 ngx_http_proxy_loc_conf_t *plcf = conf; |
507 | 2116 |
577 | 2117 size_t add; |
593 | 2118 u_short port; |
507 | 2119 ngx_str_t *value, *url; |
651 | 2120 ngx_url_t u; |
507 | 2121 ngx_http_core_loc_conf_t *clcf; |
577 | 2122 #if (NGX_HTTP_SSL) |
2123 ngx_pool_cleanup_t *cln; | |
2124 #endif | |
507 | 2125 |
555 | 2126 if (plcf->upstream.schema.len) { |
2127 return "is duplicate"; | |
2128 } | |
2129 | |
507 | 2130 value = cf->args->elts; |
2131 | |
2132 url = &value[1]; | |
2133 | |
577 | 2134 if (ngx_strncasecmp(url->data, "http://", 7) == 0) { |
2135 add = 7; | |
591 | 2136 port = 80; |
577 | 2137 |
2138 } else if (ngx_strncasecmp(url->data, "https://", 8) == 0) { | |
2139 | |
2140 #if (NGX_HTTP_SSL) | |
2141 | |
2142 add = 8; | |
591 | 2143 port = 443; |
577 | 2144 |
2145 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); | |
2146 if (plcf->upstream.ssl == NULL) { | |
2147 return NGX_CONF_ERROR; | |
2148 } | |
2149 | |
2150 plcf->upstream.ssl->log = cf->log; | |
2151 | |
2152 if (ngx_ssl_create(plcf->upstream.ssl, | |
2153 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1) | |
2154 != NGX_OK) | |
2155 { | |
2156 return NGX_CONF_ERROR; | |
2157 } | |
2158 | |
2159 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
2160 if (cln == NULL) { | |
2161 return NGX_CONF_ERROR; | |
2162 } | |
2163 | |
2164 cln->handler = ngx_ssl_cleanup_ctx; | |
2165 cln->data = plcf->upstream.ssl; | |
2166 | |
2167 #else | |
2168 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2169 "https protocol requires SSL support"); | |
2170 return NGX_CONF_ERROR; | |
2171 #endif | |
2172 | |
2173 } else { | |
507 | 2174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix"); |
2175 return NGX_CONF_ERROR; | |
2176 } | |
2177 | |
651 | 2178 ngx_memzero(&u, sizeof(ngx_url_t)); |
2179 | |
2180 u.url.len = url->len - add; | |
2181 u.url.data = url->data + add; | |
2182 u.default_portn = port; | |
2183 u.uri_part = 1; | |
2184 u.upstream = 1; | |
2185 | |
2186 plcf->upstream_peers = ngx_http_upstream_add(cf, &u); | |
2187 if (plcf->upstream_peers == NULL) { | |
507 | 2188 return NGX_CONF_ERROR; |
2189 } | |
2190 | |
651 | 2191 plcf->host_header = u.host_header; |
2192 plcf->port_text = u.port; | |
2193 plcf->upstream.uri = u.uri; | |
2194 | |
591 | 2195 plcf->upstream.schema.len = add; |
2196 plcf->upstream.schema.data = url->data; | |
509 | 2197 |
507 | 2198 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
2199 | |
2200 clcf->handler = ngx_http_proxy_handler; | |
2201 | |
573 | 2202 plcf->upstream.location = clcf->name; |
555 | 2203 |
507 | 2204 #if (NGX_PCRE) |
555 | 2205 |
629 | 2206 if (clcf->regex || clcf->noname) { |
573 | 2207 if (plcf->upstream.uri.len) { |
2208 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2209 "\"proxy_pass\" may not have URI part in " | |
843
ebab9490204c
allow proxy_pass inside limit_except
Igor Sysoev <igor@sysoev.ru>
parents:
812
diff
changeset
|
2210 "location given by regular expression, " |
ebab9490204c
allow proxy_pass inside limit_except
Igor Sysoev <igor@sysoev.ru>
parents:
812
diff
changeset
|
2211 "or inside the \"if\" statement, " |
ebab9490204c
allow proxy_pass inside limit_except
Igor Sysoev <igor@sysoev.ru>
parents:
812
diff
changeset
|
2212 "or inside the \"limit_except\" block"); |
573 | 2213 return NGX_CONF_ERROR; |
2214 } | |
2215 | |
2216 plcf->upstream.location.len = 0; | |
555 | 2217 } |
2218 | |
507 | 2219 #endif |
2220 | |
509 | 2221 plcf->upstream.url = *url; |
2222 | |
507 | 2223 if (clcf->name.data[clcf->name.len - 1] == '/') { |
2224 clcf->auto_redirect = 1; | |
2225 } | |
2226 | |
2227 return NGX_CONF_OK; | |
2228 } | |
2229 | |
2230 | |
2231 static char * | |
509 | 2232 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
507 | 2233 { |
509 | 2234 ngx_http_proxy_loc_conf_t *plcf = conf; |
2235 | |
2236 ngx_str_t *value; | |
2237 ngx_array_t *vars_lengths, *vars_values; | |
2238 ngx_http_script_compile_t sc; | |
2239 ngx_http_proxy_redirect_t *pr; | |
2240 | |
2241 if (plcf->redirect == 0) { | |
2242 return NGX_CONF_OK; | |
2243 } | |
2244 | |
2245 value = cf->args->elts; | |
2246 | |
2247 if (ngx_strcmp(value[1].data, "off") == 0) { | |
2248 plcf->redirect = 0; | |
2249 plcf->redirects = NULL; | |
2250 return NGX_CONF_OK; | |
2251 } | |
2252 | |
2253 if (plcf->redirects == NULL) { | |
2254 plcf->redirects = ngx_array_create(cf->pool, 1, | |
2255 sizeof(ngx_http_proxy_redirect_t)); | |
2256 if (plcf->redirects == NULL) { | |
2257 return NGX_CONF_ERROR; | |
2258 } | |
2259 } | |
2260 | |
2261 pr = ngx_array_push(plcf->redirects); | |
2262 if (pr == NULL) { | |
2263 return NGX_CONF_ERROR; | |
2264 } | |
2265 | |
2266 if (cf->args->nelts == 2 && ngx_strcmp(value[1].data, "default") == 0) { | |
2267 if (plcf->upstream.url.data == NULL) { | |
2268 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2269 "\"proxy_rewrite_location default\" must go " | |
2270 "after the \"proxy_pass\" directive"); | |
2271 return NGX_CONF_ERROR; | |
2272 } | |
2273 | |
2274 pr->handler = ngx_http_proxy_rewrite_redirect_text; | |
2275 pr->redirect = plcf->upstream.url; | |
653 | 2276 |
2277 if (plcf->upstream.uri.len) { | |
2278 pr->replacement.text = plcf->upstream.location; | |
2279 | |
2280 } else { | |
2281 pr->replacement.text.len = 0; | |
2282 pr->replacement.text.data = NULL; | |
2283 } | |
509 | 2284 |
2285 return NGX_CONF_OK; | |
2286 } | |
2287 | |
2288 if (ngx_http_script_variables_count(&value[2]) == 0) { | |
2289 pr->handler = ngx_http_proxy_rewrite_redirect_text; | |
2290 pr->redirect = value[1]; | |
2291 pr->replacement.text = value[2]; | |
2292 | |
2293 return NGX_CONF_OK; | |
2294 } | |
2295 | |
2296 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
2297 | |
2298 vars_lengths = NULL; | |
2299 vars_values = NULL; | |
2300 | |
2301 sc.cf = cf; | |
2302 sc.source = &value[2]; | |
2303 sc.lengths = &vars_lengths; | |
2304 sc.values = &vars_values; | |
2305 sc.complete_lengths = 1; | |
2306 sc.complete_values = 1; | |
2307 | |
2308 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
2309 return NGX_CONF_ERROR; | |
2310 } | |
2311 | |
2312 pr->handler = ngx_http_proxy_rewrite_redirect_vars; | |
2313 pr->redirect = value[1]; | |
2314 pr->replacement.vars.lengths = vars_lengths->elts; | |
2315 pr->replacement.vars.values = vars_values->elts; | |
2316 | |
507 | 2317 return NGX_CONF_OK; |
2318 } | |
2319 | |
2320 | |
2321 static char * | |
2322 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data) | |
2323 { | |
2324 #if (NGX_FREEBSD) | |
2325 ssize_t *np = data; | |
2326 | |
673 | 2327 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) { |
507 | 2328 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2329 "\"proxy_send_lowat\" must be less than %d " | |
2330 "(sysctl net.inet.tcp.sendspace)", | |
2331 ngx_freebsd_net_inet_tcp_sendspace); | |
2332 | |
2333 return NGX_CONF_ERROR; | |
2334 } | |
2335 | |
2336 #elif !(NGX_HAVE_SO_SNDLOWAT) | |
2337 ssize_t *np = data; | |
2338 | |
2339 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2340 "\"proxy_send_lowat\" is not supported, ignored"); | |
2341 | |
2342 *np = 0; | |
2343 | |
2344 #endif | |
2345 | |
2346 return NGX_CONF_OK; | |
2347 } |