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