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