58
|
1
|
126
|
2 /*
|
58
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
58
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
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);
|
|
17
|
|
18 struct ngx_http_proxy_redirect_s {
|
200
|
19 ngx_http_proxy_redirect_pt handler;
|
58
|
20
|
|
21 union {
|
654
|
22 ngx_http_complex_value_t complex;
|
|
23 #if (NGX_PCRE)
|
|
24 ngx_http_regex_t *regex;
|
|
25 #endif
|
|
26 } redirect;
|
|
27
|
|
28 ngx_http_complex_value_t replacement;
|
58
|
29 };
|
|
30
|
|
31
|
|
32 typedef struct {
|
464
|
33 ngx_str_t key_start;
|
430
|
34 ngx_str_t schema;
|
348
|
35 ngx_str_t host_header;
|
|
36 ngx_str_t port;
|
|
37 ngx_str_t uri;
|
|
38 } ngx_http_proxy_vars_t;
|
|
39
|
|
40
|
|
41 typedef struct {
|
200
|
42 ngx_http_upstream_conf_t upstream;
|
|
43
|
|
44 ngx_array_t *flushes;
|
|
45 ngx_array_t *body_set_len;
|
|
46 ngx_array_t *body_set;
|
|
47 ngx_array_t *headers_set_len;
|
|
48 ngx_array_t *headers_set;
|
|
49 ngx_hash_t headers_set_hash;
|
|
50
|
|
51 ngx_array_t *headers_source;
|
|
52
|
348
|
53 ngx_array_t *proxy_lengths;
|
|
54 ngx_array_t *proxy_values;
|
|
55
|
200
|
56 ngx_array_t *redirects;
|
|
57
|
|
58 ngx_str_t body_source;
|
|
59
|
|
60 ngx_str_t method;
|
348
|
61 ngx_str_t location;
|
|
62 ngx_str_t url;
|
|
63
|
472
|
64 #if (NGX_HTTP_CACHE)
|
|
65 ngx_http_complex_value_t cache_key;
|
|
66 #endif
|
|
67
|
348
|
68 ngx_http_proxy_vars_t vars;
|
200
|
69
|
|
70 ngx_flag_t redirect;
|
298
|
71
|
640
|
72 ngx_uint_t http_version;
|
|
73
|
298
|
74 ngx_uint_t headers_hash_max_size;
|
|
75 ngx_uint_t headers_hash_bucket_size;
|
58
|
76 } ngx_http_proxy_loc_conf_t;
|
|
77
|
|
78
|
|
79 typedef struct {
|
580
|
80 ngx_http_status_t status;
|
348
|
81 ngx_http_proxy_vars_t vars;
|
200
|
82 size_t internal_body_length;
|
640
|
83
|
|
84 ngx_uint_t state;
|
|
85 off_t size;
|
|
86 off_t length;
|
|
87
|
|
88 ngx_uint_t head; /* unsigned head:1 */
|
58
|
89 } ngx_http_proxy_ctx_t;
|
|
90
|
|
91
|
348
|
92 static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r,
|
|
93 ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf);
|
464
|
94 #if (NGX_HTTP_CACHE)
|
|
95 static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r);
|
|
96 #endif
|
58
|
97 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
|
|
98 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
|
|
99 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
|
|
100 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
|
640
|
101 static ngx_int_t ngx_http_proxy_input_filter_init(void *data);
|
|
102 static ngx_int_t ngx_http_proxy_copy_filter(ngx_event_pipe_t *p,
|
|
103 ngx_buf_t *buf);
|
|
104 static ngx_int_t ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p,
|
|
105 ngx_buf_t *buf);
|
|
106 static ngx_int_t ngx_http_proxy_non_buffered_copy_filter(void *data,
|
|
107 ssize_t bytes);
|
|
108 static ngx_int_t ngx_http_proxy_non_buffered_chunked_filter(void *data,
|
|
109 ssize_t bytes);
|
58
|
110 static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
|
|
111 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
|
|
112 ngx_int_t rc);
|
|
113
|
122
|
114 static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
115 ngx_http_variable_value_t *v, uintptr_t data);
|
|
116 static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
117 ngx_http_variable_value_t *v, uintptr_t data);
|
|
118 static ngx_int_t
|
58
|
119 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
120 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
121 static ngx_int_t
|
|
122 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
123 ngx_http_variable_value_t *v, uintptr_t data);
|
58
|
124 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
|
|
125 ngx_table_elt_t *h, size_t prefix);
|
|
126
|
|
127 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
|
|
128 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
|
|
129 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
130 void *parent, void *child);
|
464
|
131 static ngx_int_t ngx_http_proxy_merge_headers(ngx_conf_t *cf,
|
|
132 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_loc_conf_t *prev);
|
58
|
133
|
|
134 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
135 void *conf);
|
|
136 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
137 void *conf);
|
320
|
138 static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
139 void *conf);
|
464
|
140 #if (NGX_HTTP_CACHE)
|
|
141 static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
142 void *conf);
|
472
|
143 static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
144 void *conf);
|
464
|
145 #endif
|
58
|
146
|
|
147 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
|
148
|
348
|
149 #if (NGX_HTTP_SSL)
|
|
150 static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf,
|
|
151 ngx_http_proxy_loc_conf_t *plcf);
|
|
152 #endif
|
464
|
153 static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);
|
348
|
154
|
58
|
155
|
|
156 static ngx_conf_post_t ngx_http_proxy_lowat_post =
|
|
157 { ngx_http_proxy_lowat_check };
|
|
158
|
|
159
|
|
160 static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
|
|
161 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
|
|
162 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
|
|
163 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
|
|
164 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
|
412
|
165 { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 },
|
172
|
166 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
|
412
|
167 { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 },
|
58
|
168 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
|
498
|
169 { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
|
214
|
170 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
|
58
|
171 { ngx_null_string, 0 }
|
|
172 };
|
|
173
|
|
174
|
640
|
175 static ngx_conf_enum_t ngx_http_proxy_http_version[] = {
|
|
176 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
|
|
177 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
|
|
178 { ngx_null_string, 0 }
|
|
179 };
|
|
180
|
|
181
|
464
|
182 ngx_module_t ngx_http_proxy_module;
|
|
183
|
|
184
|
58
|
185 static ngx_command_t ngx_http_proxy_commands[] = {
|
|
186
|
|
187 { ngx_string("proxy_pass"),
|
256
|
188 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
|
58
|
189 ngx_http_proxy_pass,
|
|
190 NGX_HTTP_LOC_CONF_OFFSET,
|
|
191 0,
|
|
192 NULL },
|
|
193
|
|
194 { ngx_string("proxy_redirect"),
|
|
195 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
196 ngx_http_proxy_redirect,
|
|
197 NGX_HTTP_LOC_CONF_OFFSET,
|
|
198 0,
|
|
199 NULL },
|
|
200
|
318
|
201 { ngx_string("proxy_store"),
|
320
|
202 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
203 ngx_http_proxy_store,
|
|
204 NGX_HTTP_LOC_CONF_OFFSET,
|
|
205 0,
|
|
206 NULL },
|
|
207
|
|
208 { ngx_string("proxy_store_access"),
|
318
|
209 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
|
210 ngx_conf_set_access_slot,
|
|
211 NGX_HTTP_LOC_CONF_OFFSET,
|
320
|
212 offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access),
|
318
|
213 NULL },
|
|
214
|
130
|
215 { ngx_string("proxy_buffering"),
|
|
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,
|
|
219 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
|
|
220 NULL },
|
|
221
|
178
|
222 { ngx_string("proxy_ignore_client_abort"),
|
|
223 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
224 ngx_conf_set_flag_slot,
|
|
225 NGX_HTTP_LOC_CONF_OFFSET,
|
|
226 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
|
|
227 NULL },
|
|
228
|
540
|
229 { ngx_string("proxy_bind"),
|
|
230 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
558
|
231 ngx_http_upstream_bind_set_slot,
|
540
|
232 NGX_HTTP_LOC_CONF_OFFSET,
|
|
233 offsetof(ngx_http_proxy_loc_conf_t, upstream.local),
|
|
234 NULL },
|
|
235
|
58
|
236 { ngx_string("proxy_connect_timeout"),
|
|
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
238 ngx_conf_set_msec_slot,
|
|
239 NGX_HTTP_LOC_CONF_OFFSET,
|
|
240 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
|
|
241 NULL },
|
|
242
|
|
243 { ngx_string("proxy_send_timeout"),
|
|
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
245 ngx_conf_set_msec_slot,
|
|
246 NGX_HTTP_LOC_CONF_OFFSET,
|
|
247 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
|
|
248 NULL },
|
|
249
|
|
250 { ngx_string("proxy_send_lowat"),
|
|
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
252 ngx_conf_set_size_slot,
|
|
253 NGX_HTTP_LOC_CONF_OFFSET,
|
|
254 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
|
|
255 &ngx_http_proxy_lowat_post },
|
|
256
|
206
|
257 { ngx_string("proxy_intercept_errors"),
|
|
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,
|
|
261 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
|
|
262 NULL },
|
|
263
|
58
|
264 { ngx_string("proxy_set_header"),
|
|
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
198
|
266 ngx_conf_set_keyval_slot,
|
58
|
267 NGX_HTTP_LOC_CONF_OFFSET,
|
|
268 offsetof(ngx_http_proxy_loc_conf_t, headers_source),
|
|
269 NULL },
|
|
270
|
298
|
271 { ngx_string("proxy_headers_hash_max_size"),
|
|
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
273 ngx_conf_set_num_slot,
|
|
274 NGX_HTTP_LOC_CONF_OFFSET,
|
|
275 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
|
|
276 NULL },
|
|
277
|
|
278 { ngx_string("proxy_headers_hash_bucket_size"),
|
|
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
280 ngx_conf_set_num_slot,
|
|
281 NGX_HTTP_LOC_CONF_OFFSET,
|
|
282 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
|
|
283 NULL },
|
|
284
|
126
|
285 { ngx_string("proxy_set_body"),
|
|
286 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
287 ngx_conf_set_str_slot,
|
|
288 NGX_HTTP_LOC_CONF_OFFSET,
|
|
289 offsetof(ngx_http_proxy_loc_conf_t, body_source),
|
|
290 NULL },
|
|
291
|
58
|
292 { ngx_string("proxy_method"),
|
|
293 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
110
|
294 ngx_conf_set_str_slot,
|
58
|
295 NGX_HTTP_LOC_CONF_OFFSET,
|
110
|
296 offsetof(ngx_http_proxy_loc_conf_t, method),
|
|
297 NULL },
|
58
|
298
|
|
299 { ngx_string("proxy_pass_request_headers"),
|
|
300 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
301 ngx_conf_set_flag_slot,
|
|
302 NGX_HTTP_LOC_CONF_OFFSET,
|
|
303 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
|
|
304 NULL },
|
|
305
|
|
306 { ngx_string("proxy_pass_request_body"),
|
|
307 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
308 ngx_conf_set_flag_slot,
|
|
309 NGX_HTTP_LOC_CONF_OFFSET,
|
|
310 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
|
|
311 NULL },
|
|
312
|
130
|
313 { ngx_string("proxy_buffer_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,
|
|
317 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
|
|
318 NULL },
|
|
319
|
58
|
320 { ngx_string("proxy_read_timeout"),
|
|
321 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
322 ngx_conf_set_msec_slot,
|
|
323 NGX_HTTP_LOC_CONF_OFFSET,
|
|
324 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
|
|
325 NULL },
|
|
326
|
|
327 { ngx_string("proxy_buffers"),
|
|
328 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
329 ngx_conf_set_bufs_slot,
|
|
330 NGX_HTTP_LOC_CONF_OFFSET,
|
|
331 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
|
|
332 NULL },
|
|
333
|
|
334 { ngx_string("proxy_busy_buffers_size"),
|
|
335 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
336 ngx_conf_set_size_slot,
|
|
337 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
338 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
|
58
|
339 NULL },
|
|
340
|
464
|
341 #if (NGX_HTTP_CACHE)
|
|
342
|
|
343 { ngx_string("proxy_cache"),
|
592
|
344 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
464
|
345 ngx_http_proxy_cache,
|
|
346 NGX_HTTP_LOC_CONF_OFFSET,
|
466
|
347 0,
|
464
|
348 NULL },
|
|
349
|
472
|
350 { ngx_string("proxy_cache_key"),
|
592
|
351 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
472
|
352 ngx_http_proxy_cache_key,
|
|
353 NGX_HTTP_LOC_CONF_OFFSET,
|
|
354 0,
|
|
355 NULL },
|
|
356
|
464
|
357 { ngx_string("proxy_cache_path"),
|
|
358 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
|
|
359 ngx_http_file_cache_set_slot,
|
|
360 0,
|
|
361 0,
|
|
362 &ngx_http_proxy_module },
|
|
363
|
588
|
364 { ngx_string("proxy_cache_bypass"),
|
|
365 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
366 ngx_http_set_predicate_slot,
|
|
367 NGX_HTTP_LOC_CONF_OFFSET,
|
|
368 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass),
|
|
369 NULL },
|
|
370
|
572
|
371 { ngx_string("proxy_no_cache"),
|
|
372 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
588
|
373 ngx_http_set_predicate_slot,
|
572
|
374 NGX_HTTP_LOC_CONF_OFFSET,
|
|
375 offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache),
|
|
376 NULL },
|
|
377
|
464
|
378 { ngx_string("proxy_cache_valid"),
|
|
379 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
380 ngx_http_file_cache_valid_set_slot,
|
|
381 NGX_HTTP_LOC_CONF_OFFSET,
|
|
382 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid),
|
|
383 NULL },
|
|
384
|
|
385 { ngx_string("proxy_cache_min_uses"),
|
|
386 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
387 ngx_conf_set_num_slot,
|
|
388 NGX_HTTP_LOC_CONF_OFFSET,
|
|
389 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
|
|
390 NULL },
|
|
391
|
|
392 { ngx_string("proxy_cache_use_stale"),
|
|
393 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
394 ngx_conf_set_bitmask_slot,
|
|
395 NGX_HTTP_LOC_CONF_OFFSET,
|
|
396 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale),
|
|
397 &ngx_http_proxy_next_upstream_masks },
|
|
398
|
494
|
399 { ngx_string("proxy_cache_methods"),
|
|
400 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
401 ngx_conf_set_bitmask_slot,
|
|
402 NGX_HTTP_LOC_CONF_OFFSET,
|
|
403 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods),
|
|
404 &ngx_http_upstream_cache_method_mask },
|
|
405
|
656
|
406 { ngx_string("proxy_cache_lock"),
|
|
407 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
408 ngx_conf_set_flag_slot,
|
|
409 NGX_HTTP_LOC_CONF_OFFSET,
|
|
410 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock),
|
|
411 NULL },
|
|
412
|
|
413 { ngx_string("proxy_cache_lock_timeout"),
|
|
414 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
415 ngx_conf_set_msec_slot,
|
|
416 NGX_HTTP_LOC_CONF_OFFSET,
|
|
417 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout),
|
|
418 NULL },
|
|
419
|
464
|
420 #endif
|
|
421
|
58
|
422 { ngx_string("proxy_temp_path"),
|
|
423 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
424 ngx_conf_set_path_slot,
|
|
425 NGX_HTTP_LOC_CONF_OFFSET,
|
|
426 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
|
464
|
427 NULL },
|
58
|
428
|
|
429 { ngx_string("proxy_max_temp_file_size"),
|
|
430 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
431 ngx_conf_set_size_slot,
|
|
432 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
433 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
|
58
|
434 NULL },
|
|
435
|
|
436 { ngx_string("proxy_temp_file_write_size"),
|
|
437 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
438 ngx_conf_set_size_slot,
|
|
439 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
440 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
|
58
|
441 NULL },
|
|
442
|
|
443 { ngx_string("proxy_next_upstream"),
|
96
|
444 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
58
|
445 ngx_conf_set_bitmask_slot,
|
|
446 NGX_HTTP_LOC_CONF_OFFSET,
|
|
447 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
|
|
448 &ngx_http_proxy_next_upstream_masks },
|
|
449
|
198
|
450 { ngx_string("proxy_pass_header"),
|
592
|
451 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
198
|
452 ngx_conf_set_str_array_slot,
|
58
|
453 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
454 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
|
58
|
455 NULL },
|
|
456
|
198
|
457 { ngx_string("proxy_hide_header"),
|
592
|
458 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
198
|
459 ngx_conf_set_str_array_slot,
|
58
|
460 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
461 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
|
58
|
462 NULL },
|
|
463
|
484
|
464 { ngx_string("proxy_ignore_headers"),
|
|
465 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
466 ngx_conf_set_bitmask_slot,
|
|
467 NGX_HTTP_LOC_CONF_OFFSET,
|
|
468 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers),
|
584
|
469 &ngx_http_upstream_ignore_headers_masks },
|
484
|
470
|
640
|
471 { ngx_string("proxy_http_version"),
|
|
472 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
473 ngx_conf_set_enum_slot,
|
|
474 NGX_HTTP_LOC_CONF_OFFSET,
|
|
475 offsetof(ngx_http_proxy_loc_conf_t, http_version),
|
|
476 &ngx_http_proxy_http_version },
|
|
477
|
398
|
478 #if (NGX_HTTP_SSL)
|
|
479
|
|
480 { ngx_string("proxy_ssl_session_reuse"),
|
|
481 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
482 ngx_conf_set_flag_slot,
|
|
483 NGX_HTTP_LOC_CONF_OFFSET,
|
|
484 offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
|
|
485 NULL },
|
|
486
|
|
487 #endif
|
|
488
|
58
|
489 ngx_null_command
|
|
490 };
|
|
491
|
|
492
|
216
|
493 static ngx_http_module_t ngx_http_proxy_module_ctx = {
|
58
|
494 ngx_http_proxy_add_variables, /* preconfiguration */
|
|
495 NULL, /* postconfiguration */
|
|
496
|
|
497 NULL, /* create main configuration */
|
|
498 NULL, /* init main configuration */
|
|
499
|
|
500 NULL, /* create server configuration */
|
|
501 NULL, /* merge server configuration */
|
|
502
|
|
503 ngx_http_proxy_create_loc_conf, /* create location configration */
|
|
504 ngx_http_proxy_merge_loc_conf /* merge location configration */
|
|
505 };
|
|
506
|
|
507
|
|
508 ngx_module_t ngx_http_proxy_module = {
|
|
509 NGX_MODULE_V1,
|
|
510 &ngx_http_proxy_module_ctx, /* module context */
|
|
511 ngx_http_proxy_commands, /* module directives */
|
|
512 NGX_HTTP_MODULE, /* module type */
|
90
|
513 NULL, /* init master */
|
58
|
514 NULL, /* init module */
|
90
|
515 NULL, /* init process */
|
|
516 NULL, /* init thread */
|
|
517 NULL, /* exit thread */
|
|
518 NULL, /* exit process */
|
|
519 NULL, /* exit master */
|
|
520 NGX_MODULE_V1_PADDING
|
58
|
521 };
|
|
522
|
|
523
|
|
524 static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
|
640
|
525 static char ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF;
|
58
|
526
|
|
527
|
198
|
528 static ngx_keyval_t ngx_http_proxy_headers[] = {
|
|
529 { ngx_string("Host"), ngx_string("$proxy_host") },
|
|
530 { ngx_string("Connection"), ngx_string("close") },
|
|
531 { ngx_string("Keep-Alive"), ngx_string("") },
|
384
|
532 { ngx_string("Expect"), ngx_string("") },
|
640
|
533 { ngx_string("Upgrade"), ngx_string("") },
|
198
|
534 { ngx_null_string, ngx_null_string }
|
|
535 };
|
|
536
|
|
537
|
|
538 static ngx_str_t ngx_http_proxy_hide_headers[] = {
|
|
539 ngx_string("Date"),
|
|
540 ngx_string("Server"),
|
|
541 ngx_string("X-Pad"),
|
|
542 ngx_string("X-Accel-Expires"),
|
|
543 ngx_string("X-Accel-Redirect"),
|
|
544 ngx_string("X-Accel-Limit-Rate"),
|
364
|
545 ngx_string("X-Accel-Buffering"),
|
|
546 ngx_string("X-Accel-Charset"),
|
198
|
547 ngx_null_string
|
58
|
548 };
|
|
549
|
|
550
|
480
|
551 #if (NGX_HTTP_CACHE)
|
|
552
|
|
553 static ngx_keyval_t ngx_http_proxy_cache_headers[] = {
|
|
554 { ngx_string("Host"), ngx_string("$proxy_host") },
|
|
555 { ngx_string("Connection"), ngx_string("close") },
|
|
556 { ngx_string("Keep-Alive"), ngx_string("") },
|
|
557 { ngx_string("Expect"), ngx_string("") },
|
648
|
558 { ngx_string("Upgrade"), ngx_string("") },
|
480
|
559 { ngx_string("If-Modified-Since"), ngx_string("") },
|
|
560 { ngx_string("If-Unmodified-Since"), ngx_string("") },
|
558
|
561 { ngx_string("If-None-Match"), ngx_string("") },
|
480
|
562 { ngx_string("If-Match"), ngx_string("") },
|
|
563 { ngx_string("Range"), ngx_string("") },
|
|
564 { ngx_string("If-Range"), ngx_string("") },
|
|
565 { ngx_null_string, ngx_null_string }
|
|
566 };
|
|
567
|
|
568 #endif
|
|
569
|
|
570
|
58
|
571 static ngx_http_variable_t ngx_http_proxy_vars[] = {
|
|
572
|
186
|
573 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
|
366
|
574 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
575
|
186
|
576 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
|
366
|
577 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
578
|
186
|
579 { ngx_string("proxy_add_x_forwarded_for"), NULL,
|
132
|
580 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
581
|
|
582 #if 0
|
186
|
583 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
584 #endif
|
|
585
|
186
|
586 { ngx_string("proxy_internal_body_length"), NULL,
|
132
|
587 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
126
|
588
|
186
|
589 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
58
|
590 };
|
|
591
|
|
592
|
464
|
593 static ngx_path_init_t ngx_http_proxy_temp_path = {
|
|
594 ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 }
|
|
595 };
|
|
596
|
|
597
|
58
|
598 static ngx_int_t
|
|
599 ngx_http_proxy_handler(ngx_http_request_t *r)
|
126
|
600 {
|
58
|
601 ngx_int_t rc;
|
|
602 ngx_http_upstream_t *u;
|
348
|
603 ngx_http_proxy_ctx_t *ctx;
|
58
|
604 ngx_http_proxy_loc_conf_t *plcf;
|
126
|
605
|
510
|
606 if (ngx_http_upstream_create(r) != NGX_OK) {
|
58
|
607 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
608 }
|
126
|
609
|
348
|
610 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
|
|
611 if (ctx == NULL) {
|
|
612 return NGX_ERROR;
|
|
613 }
|
|
614
|
|
615 ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);
|
|
616
|
|
617 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
618
|
510
|
619 u = r->upstream;
|
|
620
|
592
|
621 if (plcf->proxy_lengths == NULL) {
|
348
|
622 ctx->vars = plcf->vars;
|
430
|
623 u->schema = plcf->vars.schema;
|
348
|
624 #if (NGX_HTTP_SSL)
|
|
625 u->ssl = (plcf->upstream.ssl != NULL);
|
|
626 #endif
|
|
627
|
|
628 } else {
|
|
629 if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
|
|
630 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
631 }
|
|
632 }
|
|
633
|
58
|
634 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
635
|
|
636 u->conf = &plcf->upstream;
|
|
637
|
464
|
638 #if (NGX_HTTP_CACHE)
|
|
639 u->create_key = ngx_http_proxy_create_key;
|
|
640 #endif
|
58
|
641 u->create_request = ngx_http_proxy_create_request;
|
|
642 u->reinit_request = ngx_http_proxy_reinit_request;
|
|
643 u->process_header = ngx_http_proxy_process_status_line;
|
|
644 u->abort_request = ngx_http_proxy_abort_request;
|
|
645 u->finalize_request = ngx_http_proxy_finalize_request;
|
566
|
646 r->state = 0;
|
58
|
647
|
|
648 if (plcf->redirects) {
|
|
649 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
|
|
650 }
|
|
651
|
198
|
652 u->buffering = plcf->upstream.buffering;
|
|
653
|
|
654 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
|
|
655 if (u->pipe == NULL) {
|
|
656 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
130
|
657 }
|
58
|
658
|
640
|
659 u->pipe->input_filter = ngx_http_proxy_copy_filter;
|
|
660 u->pipe->input_ctx = r;
|
|
661
|
|
662 u->input_filter_init = ngx_http_proxy_input_filter_init;
|
|
663 u->input_filter = ngx_http_proxy_non_buffered_copy_filter;
|
|
664 u->input_filter_ctx = r;
|
198
|
665
|
58
|
666 u->accel = 1;
|
126
|
667
|
58
|
668 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
|
|
669
|
|
670 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
671 return rc;
|
|
672 }
|
|
673
|
|
674 return NGX_DONE;
|
|
675 }
|
|
676
|
|
677
|
|
678 static ngx_int_t
|
348
|
679 ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
|
|
680 ngx_http_proxy_loc_conf_t *plcf)
|
|
681 {
|
464
|
682 u_char *p;
|
|
683 size_t add;
|
|
684 u_short port;
|
|
685 ngx_str_t proxy;
|
|
686 ngx_url_t url;
|
|
687 ngx_http_upstream_t *u;
|
348
|
688
|
|
689 if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
|
|
690 plcf->proxy_values->elts)
|
|
691 == NULL)
|
|
692 {
|
|
693 return NGX_ERROR;
|
|
694 }
|
|
695
|
632
|
696 if (proxy.len > 7
|
|
697 && ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0)
|
|
698 {
|
348
|
699 add = 7;
|
|
700 port = 80;
|
|
701
|
|
702 #if (NGX_HTTP_SSL)
|
|
703
|
632
|
704 } else if (proxy.len > 8
|
|
705 && ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0)
|
|
706 {
|
348
|
707 add = 8;
|
|
708 port = 443;
|
|
709 r->upstream->ssl = 1;
|
|
710
|
|
711 #endif
|
|
712
|
|
713 } else {
|
|
714 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
715 "invalid URL prefix in \"%V\"", &proxy);
|
|
716 return NGX_ERROR;
|
|
717 }
|
|
718
|
464
|
719 u = r->upstream;
|
|
720
|
|
721 u->schema.len = add;
|
|
722 u->schema.data = proxy.data;
|
|
723
|
|
724 ngx_memzero(&url, sizeof(ngx_url_t));
|
|
725
|
|
726 url.url.len = proxy.len - add;
|
|
727 url.url.data = proxy.data + add;
|
|
728 url.default_port = port;
|
|
729 url.uri_part = 1;
|
|
730 url.no_resolve = 1;
|
|
731
|
|
732 if (ngx_parse_url(r->pool, &url) != NGX_OK) {
|
|
733 if (url.err) {
|
348
|
734 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
464
|
735 "%s in upstream \"%V\"", url.err, &url.url);
|
348
|
736 }
|
|
737
|
|
738 return NGX_ERROR;
|
|
739 }
|
|
740
|
546
|
741 if (url.uri.len) {
|
|
742 if (url.uri.data[0] == '?') {
|
|
743 p = ngx_pnalloc(r->pool, url.uri.len + 1);
|
|
744 if (p == NULL) {
|
|
745 return NGX_ERROR;
|
|
746 }
|
|
747
|
|
748 *p++ = '/';
|
|
749 ngx_memcpy(p, url.uri.data, url.uri.len);
|
|
750
|
|
751 url.uri.len++;
|
|
752 url.uri.data = p - 1;
|
416
|
753 }
|
|
754 }
|
|
755
|
464
|
756 ctx->vars.key_start = u->schema;
|
|
757
|
|
758 ngx_http_proxy_set_vars(&url, &ctx->vars);
|
|
759
|
|
760 u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
|
|
761 if (u->resolved == NULL) {
|
348
|
762 return NGX_ERROR;
|
|
763 }
|
|
764
|
464
|
765 if (url.addrs && url.addrs[0].sockaddr) {
|
|
766 u->resolved->sockaddr = url.addrs[0].sockaddr;
|
|
767 u->resolved->socklen = url.addrs[0].socklen;
|
|
768 u->resolved->naddrs = 1;
|
|
769 u->resolved->host = url.addrs[0].name;
|
434
|
770
|
|
771 } else {
|
464
|
772 u->resolved->host = url.host;
|
|
773 u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
|
|
774 u->resolved->no_port = url.no_port;
|
434
|
775 }
|
348
|
776
|
|
777 return NGX_OK;
|
|
778 }
|
|
779
|
|
780
|
464
|
781 #if (NGX_HTTP_CACHE)
|
|
782
|
|
783 static ngx_int_t
|
|
784 ngx_http_proxy_create_key(ngx_http_request_t *r)
|
|
785 {
|
|
786 size_t len, loc_len;
|
|
787 u_char *p;
|
|
788 uintptr_t escape;
|
|
789 ngx_str_t *key;
|
|
790 ngx_http_upstream_t *u;
|
|
791 ngx_http_proxy_ctx_t *ctx;
|
|
792 ngx_http_proxy_loc_conf_t *plcf;
|
|
793
|
|
794 u = r->upstream;
|
|
795
|
|
796 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
797
|
|
798 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
799
|
|
800 key = ngx_array_push(&r->cache->keys);
|
|
801 if (key == NULL) {
|
|
802 return NGX_ERROR;
|
|
803 }
|
|
804
|
472
|
805 if (plcf->cache_key.value.len) {
|
|
806
|
|
807 if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) {
|
|
808 return NGX_ERROR;
|
|
809 }
|
|
810
|
|
811 return NGX_OK;
|
|
812 }
|
|
813
|
464
|
814 *key = ctx->vars.key_start;
|
|
815
|
|
816 key = ngx_array_push(&r->cache->keys);
|
|
817 if (key == NULL) {
|
|
818 return NGX_ERROR;
|
|
819 }
|
|
820
|
656
|
821 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
464
|
822
|
|
823 *key = ctx->vars.uri;
|
|
824 u->uri = ctx->vars.uri;
|
|
825
|
|
826 return NGX_OK;
|
|
827
|
|
828 } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
|
|
829 {
|
|
830 *key = r->unparsed_uri;
|
|
831 u->uri = r->unparsed_uri;
|
|
832
|
|
833 return NGX_OK;
|
|
834 }
|
|
835
|
|
836 loc_len = (r->valid_location && ctx->vars.uri.len) ? plcf->location.len : 0;
|
|
837
|
|
838 if (r->quoted_uri || r->internal) {
|
|
839 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
|
|
840 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
841 } else {
|
|
842 escape = 0;
|
|
843 }
|
|
844
|
|
845 len = ctx->vars.uri.len + r->uri.len - loc_len + escape
|
|
846 + sizeof("?") - 1 + r->args.len;
|
|
847
|
|
848 p = ngx_pnalloc(r->pool, len);
|
|
849 if (p == NULL) {
|
|
850 return NGX_ERROR;
|
|
851 }
|
|
852
|
|
853 key->data = p;
|
|
854
|
|
855 if (r->valid_location) {
|
|
856 p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
|
|
857 }
|
|
858
|
|
859 if (escape) {
|
|
860 ngx_escape_uri(p, r->uri.data + loc_len,
|
|
861 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
862 p += r->uri.len - loc_len + escape;
|
|
863
|
|
864 } else {
|
|
865 p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len);
|
|
866 }
|
|
867
|
|
868 if (r->args.len > 0) {
|
|
869 *p++ = '?';
|
|
870 p = ngx_copy(p, r->args.data, r->args.len);
|
|
871 }
|
|
872
|
|
873 key->len = p - key->data;
|
|
874 u->uri = *key;
|
|
875
|
|
876 return NGX_OK;
|
|
877 }
|
|
878
|
|
879 #endif
|
|
880
|
|
881
|
348
|
882 static ngx_int_t
|
58
|
883 ngx_http_proxy_create_request(ngx_http_request_t *r)
|
|
884 {
|
464
|
885 size_t len, uri_len, loc_len, body_len;
|
58
|
886 uintptr_t escape;
|
|
887 ngx_buf_t *b;
|
198
|
888 ngx_str_t method;
|
|
889 ngx_uint_t i, unparsed_uri;
|
58
|
890 ngx_chain_t *cl, *body;
|
|
891 ngx_list_part_t *part;
|
|
892 ngx_table_elt_t *header;
|
|
893 ngx_http_upstream_t *u;
|
348
|
894 ngx_http_proxy_ctx_t *ctx;
|
58
|
895 ngx_http_script_code_pt code;
|
|
896 ngx_http_script_engine_t e, le;
|
|
897 ngx_http_proxy_loc_conf_t *plcf;
|
|
898 ngx_http_script_len_code_pt lcode;
|
|
899
|
|
900 u = r->upstream;
|
|
901
|
|
902 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
903
|
110
|
904 if (u->method.len) {
|
|
905 /* HEAD was changed to GET to cache response */
|
|
906 method = u->method;
|
|
907 method.len++;
|
|
908
|
|
909 } else if (plcf->method.len) {
|
|
910 method = plcf->method;
|
|
911
|
58
|
912 } else {
|
110
|
913 method = r->method_name;
|
|
914 method.len++;
|
58
|
915 }
|
|
916
|
640
|
917 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
918
|
|
919 if (method.len == 5
|
|
920 && ngx_strncasecmp(method.data, (u_char *) "HEAD ", 5) == 0)
|
|
921 {
|
|
922 ctx->head = 1;
|
|
923 }
|
|
924
|
352
|
925 len = method.len + sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
|
|
926
|
58
|
927 escape = 0;
|
348
|
928 loc_len = 0;
|
|
929 unparsed_uri = 0;
|
|
930
|
656
|
931 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
464
|
932 uri_len = ctx->vars.uri.len;
|
348
|
933
|
|
934 } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
|
|
935 {
|
158
|
936 unparsed_uri = 1;
|
464
|
937 uri_len = r->unparsed_uri.len;
|
58
|
938
|
|
939 } else {
|
348
|
940 loc_len = (r->valid_location && ctx->vars.uri.len) ?
|
|
941 plcf->location.len : 0;
|
|
942
|
578
|
943 if (r->quoted_uri || r->space_in_uri || r->internal) {
|
58
|
944 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
|
|
945 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
946 }
|
|
947
|
464
|
948 uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape
|
|
949 + sizeof("?") - 1 + r->args.len;
|
58
|
950 }
|
|
951
|
464
|
952 if (uri_len == 0) {
|
|
953 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
954 "zero length URI to proxy");
|
|
955 return NGX_ERROR;
|
|
956 }
|
|
957
|
|
958 len += uri_len;
|
|
959
|
340
|
960 ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes);
|
122
|
961
|
126
|
962 if (plcf->body_set_len) {
|
|
963 le.ip = plcf->body_set_len->elts;
|
|
964 le.request = r;
|
|
965 le.flushed = 1;
|
|
966 body_len = 0;
|
|
967
|
|
968 while (*(uintptr_t *) le.ip) {
|
|
969 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
970 body_len += lcode(&le);
|
|
971 }
|
|
972
|
348
|
973 ctx->internal_body_length = body_len;
|
126
|
974 len += body_len;
|
|
975 }
|
|
976
|
58
|
977 le.ip = plcf->headers_set_len->elts;
|
|
978 le.request = r;
|
122
|
979 le.flushed = 1;
|
58
|
980
|
|
981 while (*(uintptr_t *) le.ip) {
|
|
982 while (*(uintptr_t *) le.ip) {
|
|
983 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
984 len += lcode(&le);
|
|
985 }
|
|
986 le.ip += sizeof(uintptr_t);
|
|
987 }
|
|
988
|
|
989
|
|
990 if (plcf->upstream.pass_request_headers) {
|
|
991 part = &r->headers_in.headers.part;
|
|
992 header = part->elts;
|
|
993
|
|
994 for (i = 0; /* void */; i++) {
|
|
995
|
|
996 if (i >= part->nelts) {
|
|
997 if (part->next == NULL) {
|
|
998 break;
|
|
999 }
|
|
1000
|
|
1001 part = part->next;
|
|
1002 header = part->elts;
|
126
|
1003 i = 0;
|
58
|
1004 }
|
|
1005
|
198
|
1006 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
1007 header[i].lowcase_key, header[i].key.len))
|
58
|
1008 {
|
|
1009 continue;
|
|
1010 }
|
|
1011
|
|
1012 len += header[i].key.len + sizeof(": ") - 1
|
|
1013 + header[i].value.len + sizeof(CRLF) - 1;
|
|
1014 }
|
|
1015 }
|
|
1016
|
|
1017
|
|
1018 b = ngx_create_temp_buf(r->pool, len);
|
|
1019 if (b == NULL) {
|
|
1020 return NGX_ERROR;
|
|
1021 }
|
|
1022
|
|
1023 cl = ngx_alloc_chain_link(r->pool);
|
|
1024 if (cl == NULL) {
|
|
1025 return NGX_ERROR;
|
|
1026 }
|
|
1027
|
|
1028 cl->buf = b;
|
|
1029
|
|
1030
|
|
1031 /* the request line */
|
|
1032
|
122
|
1033 b->last = ngx_copy(b->last, method.data, method.len);
|
58
|
1034
|
104
|
1035 u->uri.data = b->last;
|
|
1036
|
656
|
1037 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
348
|
1038 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
|
1039
|
|
1040 } else if (unparsed_uri) {
|
122
|
1041 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
|
|
1042
|
58
|
1043 } else {
|
104
|
1044 if (r->valid_location) {
|
348
|
1045 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
104
|
1046 }
|
100
|
1047
|
58
|
1048 if (escape) {
|
|
1049 ngx_escape_uri(b->last, r->uri.data + loc_len,
|
|
1050 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
1051 b->last += r->uri.len - loc_len + escape;
|
|
1052
|
126
|
1053 } else {
|
122
|
1054 b->last = ngx_copy(b->last, r->uri.data + loc_len,
|
|
1055 r->uri.len - loc_len);
|
58
|
1056 }
|
|
1057
|
|
1058 if (r->args.len > 0) {
|
|
1059 *b->last++ = '?';
|
122
|
1060 b->last = ngx_copy(b->last, r->args.data, r->args.len);
|
58
|
1061 }
|
|
1062 }
|
|
1063
|
104
|
1064 u->uri.len = b->last - u->uri.data;
|
|
1065
|
640
|
1066 if (plcf->http_version == NGX_HTTP_VERSION_11) {
|
|
1067 b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,
|
|
1068 sizeof(ngx_http_proxy_version_11) - 1);
|
|
1069
|
|
1070 } else {
|
|
1071 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
|
|
1072 sizeof(ngx_http_proxy_version) - 1);
|
|
1073 }
|
58
|
1074
|
126
|
1075 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
58
|
1076
|
|
1077 e.ip = plcf->headers_set->elts;
|
|
1078 e.pos = b->last;
|
|
1079 e.request = r;
|
122
|
1080 e.flushed = 1;
|
58
|
1081
|
|
1082 le.ip = plcf->headers_set_len->elts;
|
|
1083
|
|
1084 while (*(uintptr_t *) le.ip) {
|
|
1085 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
64
|
1086
|
|
1087 /* skip the header line name length */
|
|
1088 (void) lcode(&le);
|
58
|
1089
|
|
1090 if (*(ngx_http_script_len_code_pt *) le.ip) {
|
|
1091
|
|
1092 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
|
|
1093 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1094 }
|
|
1095
|
|
1096 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
|
|
1097
|
|
1098 } else {
|
|
1099 e.skip = 0;
|
|
1100 }
|
|
1101
|
|
1102 le.ip += sizeof(uintptr_t);
|
|
1103
|
|
1104 while (*(uintptr_t *) e.ip) {
|
|
1105 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1106 code((ngx_http_script_engine_t *) &e);
|
126
|
1107 }
|
58
|
1108 e.ip += sizeof(uintptr_t);
|
|
1109 }
|
|
1110
|
|
1111 b->last = e.pos;
|
|
1112
|
|
1113
|
|
1114 if (plcf->upstream.pass_request_headers) {
|
|
1115 part = &r->headers_in.headers.part;
|
|
1116 header = part->elts;
|
126
|
1117
|
58
|
1118 for (i = 0; /* void */; i++) {
|
|
1119
|
|
1120 if (i >= part->nelts) {
|
|
1121 if (part->next == NULL) {
|
|
1122 break;
|
|
1123 }
|
|
1124
|
|
1125 part = part->next;
|
|
1126 header = part->elts;
|
126
|
1127 i = 0;
|
58
|
1128 }
|
|
1129
|
198
|
1130 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
1131 header[i].lowcase_key, header[i].key.len))
|
58
|
1132 {
|
|
1133 continue;
|
|
1134 }
|
|
1135
|
122
|
1136 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
|
58
|
1137
|
|
1138 *b->last++ = ':'; *b->last++ = ' ';
|
|
1139
|
122
|
1140 b->last = ngx_copy(b->last, header[i].value.data,
|
|
1141 header[i].value.len);
|
58
|
1142
|
|
1143 *b->last++ = CR; *b->last++ = LF;
|
|
1144
|
|
1145 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1146 "http proxy header: \"%V: %V\"",
|
|
1147 &header[i].key, &header[i].value);
|
|
1148 }
|
|
1149 }
|
|
1150
|
126
|
1151
|
58
|
1152 /* add "\r\n" at the header end */
|
|
1153 *b->last++ = CR; *b->last++ = LF;
|
|
1154
|
126
|
1155 if (plcf->body_set) {
|
|
1156 e.ip = plcf->body_set->elts;
|
|
1157 e.pos = b->last;
|
|
1158
|
|
1159 while (*(uintptr_t *) e.ip) {
|
|
1160 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1161 code((ngx_http_script_engine_t *) &e);
|
|
1162 }
|
128
|
1163
|
|
1164 b->last = e.pos;
|
126
|
1165 }
|
|
1166
|
358
|
1167 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1168 "http proxy header:\n\"%*s\"",
|
|
1169 (size_t) (b->last - b->pos), b->pos);
|
58
|
1170
|
126
|
1171 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
|
58
|
1172
|
|
1173 body = u->request_bufs;
|
|
1174 u->request_bufs = cl;
|
|
1175
|
|
1176 while (body) {
|
|
1177 b = ngx_alloc_buf(r->pool);
|
|
1178 if (b == NULL) {
|
|
1179 return NGX_ERROR;
|
|
1180 }
|
|
1181
|
|
1182 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
|
|
1183
|
|
1184 cl->next = ngx_alloc_chain_link(r->pool);
|
|
1185 if (cl->next == NULL) {
|
|
1186 return NGX_ERROR;
|
|
1187 }
|
|
1188
|
|
1189 cl = cl->next;
|
|
1190 cl->buf = b;
|
|
1191
|
|
1192 body = body->next;
|
|
1193 }
|
|
1194
|
|
1195 } else {
|
|
1196 u->request_bufs = cl;
|
|
1197 }
|
|
1198
|
634
|
1199 b->flush = 1;
|
58
|
1200 cl->next = NULL;
|
|
1201
|
|
1202 return NGX_OK;
|
|
1203 }
|
|
1204
|
|
1205
|
|
1206 static ngx_int_t
|
|
1207 ngx_http_proxy_reinit_request(ngx_http_request_t *r)
|
|
1208 {
|
348
|
1209 ngx_http_proxy_ctx_t *ctx;
|
|
1210
|
|
1211 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1212
|
|
1213 if (ctx == NULL) {
|
58
|
1214 return NGX_OK;
|
|
1215 }
|
|
1216
|
580
|
1217 ctx->status.code = 0;
|
|
1218 ctx->status.count = 0;
|
|
1219 ctx->status.start = NULL;
|
|
1220 ctx->status.end = NULL;
|
640
|
1221 ctx->state = 0;
|
58
|
1222
|
|
1223 r->upstream->process_header = ngx_http_proxy_process_status_line;
|
640
|
1224 r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter;
|
|
1225 r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter;
|
566
|
1226 r->state = 0;
|
58
|
1227
|
|
1228 return NGX_OK;
|
|
1229 }
|
|
1230
|
|
1231
|
|
1232 static ngx_int_t
|
|
1233 ngx_http_proxy_process_status_line(ngx_http_request_t *r)
|
|
1234 {
|
580
|
1235 size_t len;
|
58
|
1236 ngx_int_t rc;
|
|
1237 ngx_http_upstream_t *u;
|
348
|
1238 ngx_http_proxy_ctx_t *ctx;
|
|
1239
|
|
1240 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1241
|
|
1242 if (ctx == NULL) {
|
356
|
1243 return NGX_ERROR;
|
58
|
1244 }
|
|
1245
|
580
|
1246 u = r->upstream;
|
|
1247
|
|
1248 rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status);
|
58
|
1249
|
|
1250 if (rc == NGX_AGAIN) {
|
|
1251 return rc;
|
|
1252 }
|
|
1253
|
580
|
1254 if (rc == NGX_ERROR) {
|
464
|
1255
|
|
1256 #if (NGX_HTTP_CACHE)
|
|
1257
|
|
1258 if (r->cache) {
|
|
1259 r->http_version = NGX_HTTP_VERSION_9;
|
|
1260 return NGX_OK;
|
|
1261 }
|
|
1262
|
|
1263 #endif
|
|
1264
|
58
|
1265 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
126
|
1266 "upstream sent no valid HTTP/1.0 header");
|
58
|
1267
|
168
|
1268 #if 0
|
58
|
1269 if (u->accel) {
|
|
1270 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1271 }
|
168
|
1272 #endif
|
58
|
1273
|
|
1274 r->http_version = NGX_HTTP_VERSION_9;
|
346
|
1275 u->state->status = NGX_HTTP_OK;
|
640
|
1276 u->headers_in.connection_close = 1;
|
126
|
1277
|
58
|
1278 return NGX_OK;
|
|
1279 }
|
|
1280
|
464
|
1281 if (u->state) {
|
580
|
1282 u->state->status = ctx->status.code;
|
464
|
1283 }
|
|
1284
|
580
|
1285 u->headers_in.status_n = ctx->status.code;
|
|
1286
|
|
1287 len = ctx->status.end - ctx->status.start;
|
|
1288 u->headers_in.status_line.len = len;
|
|
1289
|
|
1290 u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
|
78
|
1291 if (u->headers_in.status_line.data == NULL) {
|
356
|
1292 return NGX_ERROR;
|
58
|
1293 }
|
122
|
1294
|
580
|
1295 ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len);
|
58
|
1296
|
|
1297 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1298 "http proxy status %ui \"%V\"",
|
270
|
1299 u->headers_in.status_n, &u->headers_in.status_line);
|
58
|
1300
|
640
|
1301 if (ctx->status.http_version < NGX_HTTP_VERSION_11) {
|
|
1302 u->headers_in.connection_close = 1;
|
|
1303 }
|
|
1304
|
58
|
1305 u->process_header = ngx_http_proxy_process_header;
|
|
1306
|
|
1307 return ngx_http_proxy_process_header(r);
|
|
1308 }
|
|
1309
|
|
1310
|
|
1311 static ngx_int_t
|
|
1312 ngx_http_proxy_process_header(ngx_http_request_t *r)
|
|
1313 {
|
|
1314 ngx_int_t rc;
|
|
1315 ngx_table_elt_t *h;
|
640
|
1316 ngx_http_upstream_t *u;
|
|
1317 ngx_http_proxy_ctx_t *ctx;
|
58
|
1318 ngx_http_upstream_header_t *hh;
|
|
1319 ngx_http_upstream_main_conf_t *umcf;
|
|
1320
|
|
1321 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
|
|
1322
|
378
|
1323 for ( ;; ) {
|
58
|
1324
|
412
|
1325 rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);
|
58
|
1326
|
|
1327 if (rc == NGX_OK) {
|
|
1328
|
|
1329 /* a header line has been parsed successfully */
|
|
1330
|
|
1331 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1332 if (h == NULL) {
|
356
|
1333 return NGX_ERROR;
|
58
|
1334 }
|
|
1335
|
|
1336 h->hash = r->header_hash;
|
|
1337
|
|
1338 h->key.len = r->header_name_end - r->header_name_start;
|
|
1339 h->value.len = r->header_end - r->header_start;
|
|
1340
|
382
|
1341 h->key.data = ngx_pnalloc(r->pool,
|
198
|
1342 h->key.len + 1 + h->value.len + 1 + h->key.len);
|
58
|
1343 if (h->key.data == NULL) {
|
356
|
1344 return NGX_ERROR;
|
58
|
1345 }
|
|
1346
|
|
1347 h->value.data = h->key.data + h->key.len + 1;
|
198
|
1348 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
|
58
|
1349
|
|
1350 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
|
|
1351 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
|
|
1352
|
198
|
1353 if (h->key.len == r->lowcase_index) {
|
|
1354 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1355
|
|
1356 } else {
|
392
|
1357 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
58
|
1358 }
|
|
1359
|
198
|
1360 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
|
|
1361 h->lowcase_key, h->key.len);
|
|
1362
|
|
1363 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
356
|
1364 return NGX_ERROR;
|
198
|
1365 }
|
|
1366
|
58
|
1367 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1368 "http proxy header: \"%V: %V\"",
|
|
1369 &h->key, &h->value);
|
|
1370
|
|
1371 continue;
|
|
1372 }
|
|
1373
|
|
1374 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1375
|
|
1376 /* a whole header has been parsed successfully */
|
|
1377
|
|
1378 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1379 "http proxy header done");
|
|
1380
|
198
|
1381 /*
|
|
1382 * if no "Server" and "Date" in header line,
|
|
1383 * then add the special empty headers
|
|
1384 */
|
|
1385
|
|
1386 if (r->upstream->headers_in.server == NULL) {
|
|
1387 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1388 if (h == NULL) {
|
356
|
1389 return NGX_ERROR;
|
198
|
1390 }
|
|
1391
|
|
1392 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
|
|
1393 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
|
|
1394
|
570
|
1395 ngx_str_set(&h->key, "Server");
|
|
1396 ngx_str_null(&h->value);
|
198
|
1397 h->lowcase_key = (u_char *) "server";
|
|
1398 }
|
|
1399
|
|
1400 if (r->upstream->headers_in.date == NULL) {
|
|
1401 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1402 if (h == NULL) {
|
356
|
1403 return NGX_ERROR;
|
198
|
1404 }
|
|
1405
|
|
1406 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
|
|
1407
|
570
|
1408 ngx_str_set(&h->key, "Date");
|
|
1409 ngx_str_null(&h->value);
|
198
|
1410 h->lowcase_key = (u_char *) "date";
|
|
1411 }
|
|
1412
|
640
|
1413 /* clear content length if response is chunked */
|
|
1414
|
|
1415 u = r->upstream;
|
|
1416
|
|
1417 if (u->headers_in.chunked) {
|
|
1418 u->headers_in.content_length_n = -1;
|
|
1419 }
|
|
1420
|
|
1421 /*
|
|
1422 * set u->keepalive if response has no body; this allows to keep
|
|
1423 * connections alive in case of r->header_only or X-Accel-Redirect
|
|
1424 */
|
|
1425
|
|
1426 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1427
|
|
1428 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1429 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1430 || ctx->head
|
|
1431 || (!u->headers_in.chunked
|
|
1432 && u->headers_in.content_length_n == 0))
|
|
1433 {
|
|
1434 u->keepalive = !u->headers_in.connection_close;
|
|
1435 }
|
|
1436
|
58
|
1437 return NGX_OK;
|
|
1438 }
|
|
1439
|
70
|
1440 if (rc == NGX_AGAIN) {
|
|
1441 return NGX_AGAIN;
|
|
1442 }
|
|
1443
|
58
|
1444 /* there was error while a header line parsing */
|
|
1445
|
|
1446 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
168
|
1447 "upstream sent invalid header");
|
58
|
1448
|
|
1449 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1450 }
|
|
1451 }
|
|
1452
|
|
1453
|
640
|
1454 static ngx_int_t
|
|
1455 ngx_http_proxy_input_filter_init(void *data)
|
|
1456 {
|
|
1457 ngx_http_request_t *r = data;
|
|
1458 ngx_http_upstream_t *u;
|
|
1459 ngx_http_proxy_ctx_t *ctx;
|
|
1460
|
|
1461 u = r->upstream;
|
|
1462 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1463
|
|
1464 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1465 "http proxy filter init s:%d h:%d c:%d l:%O",
|
|
1466 u->headers_in.status_n, ctx->head, u->headers_in.chunked,
|
|
1467 u->headers_in.content_length_n);
|
|
1468
|
|
1469 /* as per RFC2616, 4.4 Message Length */
|
|
1470
|
|
1471 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1472 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1473 || ctx->head)
|
|
1474 {
|
|
1475 /* 1xx, 204, and 304 and replies to HEAD requests */
|
|
1476 /* no 1xx since we don't send Expect and Upgrade */
|
|
1477
|
|
1478 u->pipe->length = 0;
|
|
1479 u->length = 0;
|
|
1480 u->keepalive = !u->headers_in.connection_close;
|
|
1481
|
|
1482 } else if (u->headers_in.chunked) {
|
|
1483 /* chunked */
|
|
1484
|
|
1485 u->pipe->input_filter = ngx_http_proxy_chunked_filter;
|
|
1486 u->pipe->length = 3; /* "0" LF LF */
|
|
1487
|
|
1488 u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
|
|
1489 u->length = -1;
|
|
1490
|
|
1491 } else if (u->headers_in.content_length_n == 0) {
|
|
1492 /* empty body: special case as filter won't be called */
|
|
1493
|
|
1494 u->pipe->length = 0;
|
|
1495 u->length = 0;
|
|
1496 u->keepalive = !u->headers_in.connection_close;
|
|
1497
|
|
1498 } else {
|
|
1499 /* content length or connection close */
|
|
1500
|
|
1501 u->pipe->length = u->headers_in.content_length_n;
|
|
1502 u->length = u->headers_in.content_length_n;
|
|
1503 }
|
|
1504
|
|
1505 return NGX_OK;
|
|
1506 }
|
|
1507
|
|
1508
|
|
1509 static ngx_int_t
|
|
1510 ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1511 {
|
|
1512 ngx_buf_t *b;
|
|
1513 ngx_chain_t *cl;
|
|
1514 ngx_http_request_t *r;
|
|
1515
|
|
1516 if (buf->pos == buf->last) {
|
|
1517 return NGX_OK;
|
|
1518 }
|
|
1519
|
|
1520 if (p->free) {
|
|
1521 cl = p->free;
|
|
1522 b = cl->buf;
|
|
1523 p->free = cl->next;
|
|
1524 ngx_free_chain(p->pool, cl);
|
|
1525
|
|
1526 } else {
|
|
1527 b = ngx_alloc_buf(p->pool);
|
|
1528 if (b == NULL) {
|
|
1529 return NGX_ERROR;
|
|
1530 }
|
|
1531 }
|
|
1532
|
|
1533 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
1534 b->shadow = buf;
|
|
1535 b->tag = p->tag;
|
|
1536 b->last_shadow = 1;
|
|
1537 b->recycled = 1;
|
|
1538 buf->shadow = b;
|
|
1539
|
|
1540 cl = ngx_alloc_chain_link(p->pool);
|
|
1541 if (cl == NULL) {
|
|
1542 return NGX_ERROR;
|
|
1543 }
|
|
1544
|
|
1545 cl->buf = b;
|
|
1546 cl->next = NULL;
|
|
1547
|
|
1548 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
|
|
1549
|
|
1550 if (p->in) {
|
|
1551 *p->last_in = cl;
|
|
1552 } else {
|
|
1553 p->in = cl;
|
|
1554 }
|
|
1555 p->last_in = &cl->next;
|
|
1556
|
|
1557 if (p->length == -1) {
|
|
1558 return NGX_OK;
|
|
1559 }
|
|
1560
|
|
1561 p->length -= b->last - b->pos;
|
|
1562
|
|
1563 if (p->length == 0) {
|
|
1564 r = p->input_ctx;
|
|
1565 p->upstream_done = 1;
|
|
1566 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1567
|
|
1568 } else if (p->length < 0) {
|
|
1569 r = p->input_ctx;
|
|
1570 p->upstream_done = 1;
|
|
1571
|
642
|
1572 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
644
|
1573 "upstream sent too much data");
|
640
|
1574 }
|
|
1575
|
|
1576 return NGX_OK;
|
|
1577 }
|
|
1578
|
|
1579
|
|
1580 static ngx_inline ngx_int_t
|
|
1581 ngx_http_proxy_parse_chunked(ngx_http_request_t *r, ngx_buf_t *buf)
|
|
1582 {
|
|
1583 u_char *pos, ch, c;
|
|
1584 ngx_int_t rc;
|
|
1585 ngx_http_proxy_ctx_t *ctx;
|
|
1586 enum {
|
|
1587 sw_chunk_start = 0,
|
|
1588 sw_chunk_size,
|
|
1589 sw_chunk_extension,
|
|
1590 sw_chunk_extension_almost_done,
|
|
1591 sw_chunk_data,
|
|
1592 sw_after_data,
|
|
1593 sw_after_data_almost_done,
|
|
1594 sw_last_chunk_extension,
|
|
1595 sw_last_chunk_extension_almost_done,
|
|
1596 sw_trailer,
|
|
1597 sw_trailer_almost_done,
|
|
1598 sw_trailer_header,
|
|
1599 sw_trailer_header_almost_done
|
|
1600 } state;
|
|
1601
|
|
1602 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1603 state = ctx->state;
|
|
1604
|
|
1605 if (state == sw_chunk_data && ctx->size == 0) {
|
|
1606 state = sw_after_data;
|
|
1607 }
|
|
1608
|
|
1609 rc = NGX_AGAIN;
|
|
1610
|
|
1611 for (pos = buf->pos; pos < buf->last; pos++) {
|
|
1612
|
|
1613 ch = *pos;
|
|
1614
|
|
1615 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1616 "http proxy chunked byte: %02Xd s:%d", ch, state);
|
|
1617
|
|
1618 switch (state) {
|
|
1619
|
|
1620 case sw_chunk_start:
|
|
1621 if (ch >= '0' && ch <= '9') {
|
|
1622 state = sw_chunk_size;
|
|
1623 ctx->size = ch - '0';
|
|
1624 break;
|
|
1625 }
|
|
1626
|
|
1627 c = (u_char) (ch | 0x20);
|
|
1628
|
|
1629 if (c >= 'a' && c <= 'f') {
|
|
1630 state = sw_chunk_size;
|
|
1631 ctx->size = c - 'a' + 10;
|
|
1632 break;
|
|
1633 }
|
|
1634
|
|
1635 goto invalid;
|
|
1636
|
|
1637 case sw_chunk_size:
|
|
1638 if (ch >= '0' && ch <= '9') {
|
|
1639 ctx->size = ctx->size * 16 + (ch - '0');
|
|
1640 break;
|
|
1641 }
|
|
1642
|
|
1643 c = (u_char) (ch | 0x20);
|
|
1644
|
|
1645 if (c >= 'a' && c <= 'f') {
|
|
1646 ctx->size = ctx->size * 16 + (c - 'a' + 10);
|
|
1647 break;
|
|
1648 }
|
|
1649
|
|
1650 if (ctx->size == 0) {
|
|
1651
|
|
1652 switch (ch) {
|
|
1653 case CR:
|
|
1654 state = sw_last_chunk_extension_almost_done;
|
|
1655 break;
|
|
1656 case LF:
|
|
1657 state = sw_trailer;
|
|
1658 break;
|
|
1659 case ';':
|
642
|
1660 case ' ':
|
|
1661 case '\t':
|
640
|
1662 state = sw_last_chunk_extension;
|
|
1663 break;
|
|
1664 default:
|
|
1665 goto invalid;
|
|
1666 }
|
|
1667
|
|
1668 break;
|
|
1669 }
|
|
1670
|
|
1671 switch (ch) {
|
|
1672 case CR:
|
|
1673 state = sw_chunk_extension_almost_done;
|
|
1674 break;
|
|
1675 case LF:
|
|
1676 state = sw_chunk_data;
|
|
1677 break;
|
|
1678 case ';':
|
642
|
1679 case ' ':
|
|
1680 case '\t':
|
640
|
1681 state = sw_chunk_extension;
|
|
1682 break;
|
|
1683 default:
|
|
1684 goto invalid;
|
|
1685 }
|
|
1686
|
|
1687 break;
|
|
1688
|
|
1689 case sw_chunk_extension:
|
|
1690 switch (ch) {
|
|
1691 case CR:
|
|
1692 state = sw_chunk_extension_almost_done;
|
|
1693 break;
|
|
1694 case LF:
|
|
1695 state = sw_chunk_data;
|
|
1696 }
|
|
1697 break;
|
|
1698
|
|
1699 case sw_chunk_extension_almost_done:
|
|
1700 if (ch == LF) {
|
|
1701 state = sw_chunk_data;
|
|
1702 break;
|
|
1703 }
|
|
1704 goto invalid;
|
|
1705
|
|
1706 case sw_chunk_data:
|
|
1707 rc = NGX_OK;
|
|
1708 goto data;
|
|
1709
|
|
1710 case sw_after_data:
|
|
1711 switch (ch) {
|
|
1712 case CR:
|
|
1713 state = sw_after_data_almost_done;
|
|
1714 break;
|
|
1715 case LF:
|
|
1716 state = sw_chunk_start;
|
|
1717 }
|
|
1718 break;
|
|
1719
|
|
1720 case sw_after_data_almost_done:
|
|
1721 if (ch == LF) {
|
|
1722 state = sw_chunk_start;
|
|
1723 break;
|
|
1724 }
|
|
1725 goto invalid;
|
|
1726
|
|
1727 case sw_last_chunk_extension:
|
|
1728 switch (ch) {
|
|
1729 case CR:
|
|
1730 state = sw_last_chunk_extension_almost_done;
|
|
1731 break;
|
|
1732 case LF:
|
|
1733 state = sw_trailer;
|
|
1734 }
|
|
1735 break;
|
|
1736
|
|
1737 case sw_last_chunk_extension_almost_done:
|
|
1738 if (ch == LF) {
|
|
1739 state = sw_trailer;
|
|
1740 break;
|
|
1741 }
|
|
1742 goto invalid;
|
|
1743
|
|
1744 case sw_trailer:
|
|
1745 switch (ch) {
|
|
1746 case CR:
|
|
1747 state = sw_trailer_almost_done;
|
|
1748 break;
|
|
1749 case LF:
|
|
1750 goto done;
|
|
1751 default:
|
|
1752 state = sw_trailer_header;
|
|
1753 }
|
|
1754 break;
|
|
1755
|
|
1756 case sw_trailer_almost_done:
|
|
1757 if (ch == LF) {
|
|
1758 goto done;
|
|
1759 }
|
|
1760 goto invalid;
|
|
1761
|
|
1762 case sw_trailer_header:
|
|
1763 switch (ch) {
|
|
1764 case CR:
|
|
1765 state = sw_trailer_header_almost_done;
|
|
1766 break;
|
|
1767 case LF:
|
|
1768 state = sw_trailer;
|
|
1769 }
|
|
1770 break;
|
|
1771
|
|
1772 case sw_trailer_header_almost_done:
|
|
1773 if (ch == LF) {
|
|
1774 state = sw_trailer;
|
|
1775 break;
|
|
1776 }
|
|
1777 goto invalid;
|
|
1778
|
|
1779 }
|
|
1780 }
|
|
1781
|
|
1782 data:
|
|
1783
|
|
1784 ctx->state = state;
|
|
1785 buf->pos = pos;
|
|
1786
|
|
1787 switch (state) {
|
|
1788
|
|
1789 case sw_chunk_start:
|
|
1790 ctx->length = 3 /* "0" LF LF */;
|
|
1791 break;
|
|
1792 case sw_chunk_size:
|
|
1793 ctx->length = 2 /* LF LF */
|
|
1794 + (ctx->size ? ctx->size + 4 /* LF "0" LF LF */ : 0);
|
|
1795 break;
|
|
1796 case sw_chunk_extension:
|
|
1797 case sw_chunk_extension_almost_done:
|
|
1798 ctx->length = 1 /* LF */ + ctx->size + 4 /* LF "0" LF LF */;
|
|
1799 break;
|
|
1800 case sw_chunk_data:
|
|
1801 ctx->length = ctx->size + 4 /* LF "0" LF LF */;
|
|
1802 break;
|
|
1803 case sw_after_data:
|
|
1804 case sw_after_data_almost_done:
|
|
1805 ctx->length = 4 /* LF "0" LF LF */;
|
|
1806 break;
|
|
1807 case sw_last_chunk_extension:
|
|
1808 case sw_last_chunk_extension_almost_done:
|
|
1809 ctx->length = 2 /* LF LF */;
|
|
1810 break;
|
|
1811 case sw_trailer:
|
|
1812 case sw_trailer_almost_done:
|
|
1813 ctx->length = 1 /* LF */;
|
|
1814 break;
|
|
1815 case sw_trailer_header:
|
|
1816 case sw_trailer_header_almost_done:
|
|
1817 ctx->length = 2 /* LF LF */;
|
|
1818 break;
|
|
1819
|
|
1820 }
|
|
1821
|
|
1822 return rc;
|
|
1823
|
|
1824 done:
|
|
1825
|
|
1826 return NGX_DONE;
|
|
1827
|
|
1828 invalid:
|
|
1829
|
|
1830 return NGX_ERROR;
|
|
1831 }
|
|
1832
|
|
1833
|
|
1834 static ngx_int_t
|
|
1835 ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1836 {
|
|
1837 ngx_int_t rc;
|
|
1838 ngx_buf_t *b, **prev;
|
|
1839 ngx_chain_t *cl;
|
|
1840 ngx_http_request_t *r;
|
|
1841 ngx_http_proxy_ctx_t *ctx;
|
|
1842
|
|
1843 if (buf->pos == buf->last) {
|
|
1844 return NGX_OK;
|
|
1845 }
|
|
1846
|
|
1847 r = p->input_ctx;
|
|
1848 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1849
|
|
1850 b = NULL;
|
|
1851 prev = &buf->shadow;
|
|
1852
|
|
1853 for ( ;; ) {
|
|
1854
|
|
1855 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
1856
|
|
1857 if (rc == NGX_OK) {
|
|
1858
|
|
1859 /* a chunk has been parsed successfully */
|
|
1860
|
|
1861 if (p->free) {
|
|
1862 cl = p->free;
|
|
1863 b = cl->buf;
|
|
1864 p->free = cl->next;
|
|
1865 ngx_free_chain(p->pool, cl);
|
|
1866
|
|
1867 } else {
|
|
1868 b = ngx_alloc_buf(p->pool);
|
|
1869 if (b == NULL) {
|
|
1870 return NGX_ERROR;
|
|
1871 }
|
|
1872 }
|
|
1873
|
|
1874 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
1875
|
|
1876 b->pos = buf->pos;
|
|
1877 b->start = buf->start;
|
|
1878 b->end = buf->end;
|
|
1879 b->tag = p->tag;
|
|
1880 b->temporary = 1;
|
|
1881 b->recycled = 1;
|
|
1882
|
|
1883 *prev = b;
|
|
1884 prev = &b->shadow;
|
|
1885
|
|
1886 cl = ngx_alloc_chain_link(p->pool);
|
|
1887 if (cl == NULL) {
|
|
1888 return NGX_ERROR;
|
|
1889 }
|
|
1890
|
|
1891 cl->buf = b;
|
|
1892 cl->next = NULL;
|
|
1893
|
|
1894 if (p->in) {
|
|
1895 *p->last_in = cl;
|
|
1896 } else {
|
|
1897 p->in = cl;
|
|
1898 }
|
|
1899 p->last_in = &cl->next;
|
|
1900
|
|
1901 /* STUB */ b->num = buf->num;
|
|
1902
|
|
1903 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1904 "input buf #%d %p", b->num, b->pos);
|
|
1905
|
|
1906 if (buf->last - buf->pos >= ctx->size) {
|
|
1907
|
|
1908 buf->pos += ctx->size;
|
|
1909 b->last = buf->pos;
|
|
1910 ctx->size = 0;
|
|
1911
|
|
1912 continue;
|
|
1913 }
|
|
1914
|
|
1915 ctx->size -= buf->last - buf->pos;
|
|
1916 buf->pos = buf->last;
|
|
1917 b->last = buf->last;
|
|
1918
|
|
1919 continue;
|
|
1920 }
|
|
1921
|
|
1922 if (rc == NGX_DONE) {
|
|
1923
|
|
1924 /* a whole response has been parsed successfully */
|
|
1925
|
|
1926 p->upstream_done = 1;
|
|
1927 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1928
|
|
1929 break;
|
|
1930 }
|
|
1931
|
|
1932 if (rc == NGX_AGAIN) {
|
|
1933
|
|
1934 /* set p->length, minimal amount of data we want to see */
|
|
1935
|
|
1936 p->length = ctx->length;
|
|
1937
|
|
1938 break;
|
|
1939 }
|
|
1940
|
|
1941 /* invalid response */
|
|
1942
|
642
|
1943 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
1944 "upstream sent invalid chunked response");
|
|
1945
|
|
1946 return NGX_ERROR;
|
|
1947 }
|
|
1948
|
|
1949 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1950 "http proxy chunked state %d, length %d",
|
|
1951 ctx->state, p->length);
|
|
1952
|
|
1953 if (b) {
|
|
1954 b->shadow = buf;
|
|
1955 b->last_shadow = 1;
|
|
1956
|
|
1957 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1958 "input buf %p %z", b->pos, b->last - b->pos);
|
|
1959
|
|
1960 return NGX_OK;
|
|
1961 }
|
|
1962
|
|
1963 /* there is no data record in the buf, add it to free chain */
|
|
1964
|
|
1965 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
|
|
1966 return NGX_ERROR;
|
|
1967 }
|
|
1968
|
|
1969 return NGX_OK;
|
|
1970 }
|
|
1971
|
|
1972
|
|
1973 static ngx_int_t
|
|
1974 ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
|
|
1975 {
|
|
1976 ngx_http_request_t *r = data;
|
|
1977
|
|
1978 ngx_buf_t *b;
|
|
1979 ngx_chain_t *cl, **ll;
|
|
1980 ngx_http_upstream_t *u;
|
|
1981
|
|
1982 u = r->upstream;
|
|
1983
|
|
1984 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
1985 ll = &cl->next;
|
|
1986 }
|
|
1987
|
|
1988 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
1989 if (cl == NULL) {
|
|
1990 return NGX_ERROR;
|
|
1991 }
|
|
1992
|
|
1993 *ll = cl;
|
|
1994
|
|
1995 cl->buf->flush = 1;
|
|
1996 cl->buf->memory = 1;
|
|
1997
|
|
1998 b = &u->buffer;
|
|
1999
|
|
2000 cl->buf->pos = b->last;
|
|
2001 b->last += bytes;
|
|
2002 cl->buf->last = b->last;
|
|
2003 cl->buf->tag = u->output.tag;
|
|
2004
|
|
2005 if (u->length == -1) {
|
|
2006 return NGX_OK;
|
|
2007 }
|
|
2008
|
|
2009 u->length -= bytes;
|
|
2010
|
|
2011 if (u->length == 0) {
|
|
2012 u->keepalive = !u->headers_in.connection_close;
|
|
2013 }
|
|
2014
|
|
2015 return NGX_OK;
|
|
2016 }
|
|
2017
|
|
2018
|
|
2019 static ngx_int_t
|
|
2020 ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
|
|
2021 {
|
|
2022 ngx_http_request_t *r = data;
|
|
2023
|
|
2024 ngx_int_t rc;
|
|
2025 ngx_buf_t *b, *buf;
|
|
2026 ngx_chain_t *cl, **ll;
|
|
2027 ngx_http_upstream_t *u;
|
|
2028 ngx_http_proxy_ctx_t *ctx;
|
|
2029
|
|
2030 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2031 u = r->upstream;
|
|
2032 buf = &u->buffer;
|
|
2033
|
|
2034 buf->pos = buf->last;
|
|
2035 buf->last += bytes;
|
|
2036
|
|
2037 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
2038 ll = &cl->next;
|
|
2039 }
|
|
2040
|
|
2041 for ( ;; ) {
|
|
2042
|
|
2043 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
2044
|
|
2045 if (rc == NGX_OK) {
|
|
2046
|
|
2047 /* a chunk has been parsed successfully */
|
|
2048
|
|
2049 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
2050 if (cl == NULL) {
|
|
2051 return NGX_ERROR;
|
|
2052 }
|
|
2053
|
|
2054 *ll = cl;
|
|
2055 ll = &cl->next;
|
|
2056
|
|
2057 b = cl->buf;
|
|
2058
|
|
2059 b->flush = 1;
|
|
2060 b->memory = 1;
|
|
2061
|
|
2062 b->pos = buf->pos;
|
|
2063 b->tag = u->output.tag;
|
|
2064
|
|
2065 if (buf->last - buf->pos >= ctx->size) {
|
|
2066 buf->pos += ctx->size;
|
|
2067 b->last = buf->pos;
|
|
2068 ctx->size = 0;
|
|
2069
|
|
2070 } else {
|
|
2071 ctx->size -= buf->last - buf->pos;
|
|
2072 buf->pos = buf->last;
|
|
2073 b->last = buf->last;
|
|
2074 }
|
|
2075
|
|
2076 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2077 "http proxy out buf %p %z",
|
|
2078 b->pos, b->last - b->pos);
|
|
2079
|
|
2080 continue;
|
|
2081 }
|
|
2082
|
|
2083 if (rc == NGX_DONE) {
|
|
2084
|
|
2085 /* a whole response has been parsed successfully */
|
|
2086
|
|
2087 u->keepalive = !u->headers_in.connection_close;
|
|
2088 u->length = 0;
|
|
2089
|
|
2090 break;
|
|
2091 }
|
|
2092
|
|
2093 if (rc == NGX_AGAIN) {
|
|
2094 break;
|
|
2095 }
|
|
2096
|
|
2097 /* invalid response */
|
|
2098
|
642
|
2099 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
2100 "upstream sent invalid chunked response");
|
|
2101
|
|
2102 return NGX_ERROR;
|
|
2103 }
|
|
2104
|
|
2105 /* provide continuous buffer for subrequests in memory */
|
|
2106
|
|
2107 if (r->subrequest_in_memory) {
|
|
2108
|
|
2109 cl = u->out_bufs;
|
|
2110
|
|
2111 if (cl) {
|
|
2112 buf->pos = cl->buf->pos;
|
|
2113 }
|
|
2114
|
|
2115 buf->last = buf->pos;
|
|
2116
|
|
2117 for (cl = u->out_bufs; cl; cl = cl->next) {
|
|
2118 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2119 "http proxy in memory %p-%p %uz",
|
|
2120 cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf));
|
|
2121
|
|
2122 if (buf->last == cl->buf->pos) {
|
|
2123 buf->last = cl->buf->last;
|
|
2124 continue;
|
|
2125 }
|
|
2126
|
|
2127 buf->last = ngx_movemem(buf->last, cl->buf->pos,
|
|
2128 cl->buf->last - cl->buf->pos);
|
|
2129
|
|
2130 cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos);
|
|
2131 cl->buf->last = buf->last;
|
|
2132 }
|
|
2133 }
|
|
2134
|
|
2135 return NGX_OK;
|
|
2136 }
|
|
2137
|
|
2138
|
58
|
2139 static void
|
|
2140 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
2141 {
|
|
2142 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2143 "abort http proxy request");
|
126
|
2144
|
58
|
2145 return;
|
|
2146 }
|
|
2147
|
|
2148
|
|
2149 static void
|
|
2150 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
2151 {
|
58
|
2152 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2153 "finalize http proxy request");
|
|
2154
|
|
2155 return;
|
|
2156 }
|
|
2157
|
|
2158
|
122
|
2159 static ngx_int_t
|
|
2160 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
2161 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2162 {
|
348
|
2163 ngx_http_proxy_ctx_t *ctx;
|
|
2164
|
|
2165 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2166
|
|
2167 if (ctx == NULL) {
|
|
2168 v->not_found = 1;
|
|
2169 return NGX_OK;
|
|
2170 }
|
|
2171
|
|
2172 v->len = ctx->vars.host_header.len;
|
126
|
2173 v->valid = 1;
|
340
|
2174 v->no_cacheable = 0;
|
122
|
2175 v->not_found = 0;
|
348
|
2176 v->data = ctx->vars.host_header.data;
|
58
|
2177
|
122
|
2178 return NGX_OK;
|
58
|
2179 }
|
|
2180
|
|
2181
|
122
|
2182 static ngx_int_t
|
|
2183 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
2184 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2185 {
|
348
|
2186 ngx_http_proxy_ctx_t *ctx;
|
|
2187
|
|
2188 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2189
|
|
2190 if (ctx == NULL) {
|
|
2191 v->not_found = 1;
|
|
2192 return NGX_OK;
|
|
2193 }
|
|
2194
|
|
2195 v->len = ctx->vars.port.len;
|
126
|
2196 v->valid = 1;
|
340
|
2197 v->no_cacheable = 0;
|
122
|
2198 v->not_found = 0;
|
348
|
2199 v->data = ctx->vars.port.data;
|
58
|
2200
|
122
|
2201 return NGX_OK;
|
58
|
2202 }
|
|
2203
|
|
2204
|
122
|
2205 static ngx_int_t
|
58
|
2206 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
2207 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2208 {
|
122
|
2209 u_char *p;
|
58
|
2210
|
126
|
2211 v->valid = 1;
|
340
|
2212 v->no_cacheable = 0;
|
122
|
2213 v->not_found = 0;
|
58
|
2214
|
|
2215 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
2216 v->len = r->connection->addr_text.len;
|
|
2217 v->data = r->connection->addr_text.data;
|
|
2218 return NGX_OK;
|
58
|
2219 }
|
|
2220
|
122
|
2221 v->len = r->headers_in.x_forwarded_for->value.len
|
|
2222 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
2223
|
382
|
2224 p = ngx_pnalloc(r->pool, v->len);
|
58
|
2225 if (p == NULL) {
|
122
|
2226 return NGX_ERROR;
|
58
|
2227 }
|
|
2228
|
122
|
2229 v->data = p;
|
58
|
2230
|
122
|
2231 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
2232 r->headers_in.x_forwarded_for->value.len);
|
58
|
2233
|
|
2234 *p++ = ','; *p++ = ' ';
|
|
2235
|
|
2236 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
2237
|
122
|
2238 return NGX_OK;
|
58
|
2239 }
|
|
2240
|
|
2241
|
|
2242 static ngx_int_t
|
126
|
2243 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
2244 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2245 {
|
348
|
2246 ngx_http_proxy_ctx_t *ctx;
|
|
2247
|
|
2248 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2249
|
|
2250 if (ctx == NULL) {
|
126
|
2251 v->not_found = 1;
|
|
2252 return NGX_OK;
|
|
2253 }
|
|
2254
|
|
2255 v->valid = 1;
|
340
|
2256 v->no_cacheable = 0;
|
126
|
2257 v->not_found = 0;
|
|
2258
|
382
|
2259 v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
|
126
|
2260
|
|
2261 if (v->data == NULL) {
|
|
2262 return NGX_ERROR;
|
|
2263 }
|
|
2264
|
348
|
2265 v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
|
126
|
2266
|
|
2267 return NGX_OK;
|
|
2268 }
|
|
2269
|
|
2270
|
|
2271 static ngx_int_t
|
58
|
2272 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2273 size_t prefix)
|
|
2274 {
|
|
2275 ngx_int_t rc;
|
|
2276 ngx_uint_t i;
|
|
2277 ngx_http_proxy_loc_conf_t *plcf;
|
|
2278 ngx_http_proxy_redirect_t *pr;
|
|
2279
|
|
2280 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2281
|
|
2282 pr = plcf->redirects->elts;
|
|
2283
|
|
2284 if (pr == NULL) {
|
|
2285 return NGX_DECLINED;
|
|
2286 }
|
|
2287
|
|
2288 for (i = 0; i < plcf->redirects->nelts; i++) {
|
154
|
2289 rc = pr[i].handler(r, h, prefix, &pr[i]);
|
58
|
2290
|
|
2291 if (rc != NGX_DECLINED) {
|
|
2292 return rc;
|
|
2293 }
|
|
2294 }
|
|
2295
|
|
2296 return NGX_DECLINED;
|
|
2297 }
|
|
2298
|
|
2299
|
|
2300 static ngx_int_t
|
654
|
2301 ngx_http_proxy_rewrite_redirect_complex(ngx_http_request_t *r,
|
|
2302 ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_redirect_t *pr)
|
58
|
2303 {
|
654
|
2304 size_t len;
|
|
2305 u_char *data, *p;
|
|
2306 ngx_str_t redirect, replacement;
|
|
2307
|
|
2308 if (ngx_http_complex_value(r, &pr->redirect.complex, &redirect) != NGX_OK) {
|
|
2309 return NGX_ERROR;
|
|
2310 }
|
|
2311
|
|
2312 if (redirect.len > h->value.len - prefix
|
|
2313 || ngx_rstrncmp(h->value.data + prefix, redirect.data,
|
|
2314 redirect.len) != 0)
|
58
|
2315 {
|
|
2316 return NGX_DECLINED;
|
|
2317 }
|
|
2318
|
654
|
2319 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
|
2320 return NGX_ERROR;
|
|
2321 }
|
|
2322
|
|
2323 len = replacement.len + h->value.len - redirect.len;
|
58
|
2324
|
382
|
2325 data = ngx_pnalloc(r->pool, len);
|
58
|
2326 if (data == NULL) {
|
|
2327 return NGX_ERROR;
|
|
2328 }
|
|
2329
|
578
|
2330 p = ngx_copy(data, h->value.data, prefix);
|
58
|
2331
|
654
|
2332 if (replacement.len) {
|
|
2333 p = ngx_copy(p, replacement.data, replacement.len);
|
202
|
2334 }
|
58
|
2335
|
654
|
2336 ngx_memcpy(p, h->value.data + prefix + redirect.len,
|
|
2337 h->value.len - redirect.len - prefix);
|
58
|
2338
|
|
2339 h->value.len = len;
|
|
2340 h->value.data = data;
|
|
2341
|
|
2342 return NGX_OK;
|
|
2343 }
|
|
2344
|
|
2345
|
654
|
2346 #if (NGX_PCRE)
|
|
2347
|
58
|
2348 static ngx_int_t
|
654
|
2349 ngx_http_proxy_rewrite_redirect_regex(ngx_http_request_t *r, ngx_table_elt_t *h,
|
58
|
2350 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
2351 {
|
658
|
2352 size_t len;
|
|
2353 u_char *data;
|
654
|
2354 ngx_str_t redirect, replacement;
|
|
2355
|
|
2356 redirect.len = h->value.len - prefix;
|
|
2357 redirect.data = h->value.data + prefix;
|
|
2358
|
|
2359 if (ngx_http_regex_exec(r, pr->redirect.regex, &redirect) != NGX_OK) {
|
58
|
2360 return NGX_DECLINED;
|
|
2361 }
|
|
2362
|
654
|
2363 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
58
|
2364 return NGX_ERROR;
|
|
2365 }
|
|
2366
|
658
|
2367 if (!prefix) {
|
|
2368 h->value = replacement;
|
|
2369 return NGX_OK;
|
|
2370 }
|
|
2371
|
|
2372 len = prefix + replacement.len;
|
|
2373
|
|
2374 data = ngx_pnalloc(r->pool, len);
|
|
2375 if (data == NULL) {
|
|
2376 return NGX_ERROR;
|
|
2377 }
|
|
2378
|
|
2379 ngx_memcpy(data, h->value.data, prefix);
|
|
2380 ngx_memcpy(data + prefix, replacement.data, replacement.len);
|
|
2381
|
|
2382 h->value.len = len;
|
|
2383 h->value.data = data;
|
58
|
2384
|
|
2385 return NGX_OK;
|
|
2386 }
|
|
2387
|
654
|
2388 #endif
|
|
2389
|
58
|
2390
|
|
2391 static ngx_int_t
|
|
2392 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
2393 {
|
|
2394 ngx_http_variable_t *var, *v;
|
|
2395
|
|
2396 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
2397 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2398 if (var == NULL) {
|
|
2399 return NGX_ERROR;
|
|
2400 }
|
|
2401
|
186
|
2402 var->get_handler = v->get_handler;
|
58
|
2403 var->data = v->data;
|
|
2404 }
|
|
2405
|
|
2406 return NGX_OK;
|
|
2407 }
|
|
2408
|
|
2409
|
|
2410 static void *
|
|
2411 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
2412 {
|
|
2413 ngx_http_proxy_loc_conf_t *conf;
|
|
2414
|
|
2415 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
2416 if (conf == NULL) {
|
496
|
2417 return NULL;
|
58
|
2418 }
|
|
2419
|
|
2420 /*
|
|
2421 * set by ngx_pcalloc():
|
|
2422 *
|
|
2423 * conf->upstream.bufs.num = 0;
|
484
|
2424 * conf->upstream.ignore_headers = 0;
|
58
|
2425 * conf->upstream.next_upstream = 0;
|
494
|
2426 * conf->upstream.cache_use_stale = 0;
|
|
2427 * conf->upstream.cache_methods = 0;
|
58
|
2428 * conf->upstream.temp_path = NULL;
|
198
|
2429 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
2430 * conf->upstream.uri = { 0, NULL };
|
|
2431 * conf->upstream.location = NULL;
|
320
|
2432 * conf->upstream.store_lengths = NULL;
|
|
2433 * conf->upstream.store_values = NULL;
|
58
|
2434 *
|
110
|
2435 * conf->method = NULL;
|
58
|
2436 * conf->headers_source = NULL;
|
|
2437 * conf->headers_set_len = NULL;
|
|
2438 * conf->headers_set = NULL;
|
|
2439 * conf->headers_set_hash = NULL;
|
126
|
2440 * conf->body_set_len = NULL;
|
|
2441 * conf->body_set = NULL;
|
|
2442 * conf->body_source = { 0, NULL };
|
566
|
2443 * conf->redirects = NULL;
|
58
|
2444 */
|
126
|
2445
|
320
|
2446 conf->upstream.store = NGX_CONF_UNSET;
|
|
2447 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
2448 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
2449 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
2450
|
58
|
2451 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
2452 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
2453 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
2454
|
|
2455 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
2456 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
2457
|
|
2458 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
2459 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
2460 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
2461
|
|
2462 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
2463 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
2464
|
464
|
2465 #if (NGX_HTTP_CACHE)
|
|
2466 conf->upstream.cache = NGX_CONF_UNSET_PTR;
|
|
2467 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
|
588
|
2468 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
|
572
|
2469 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
|
464
|
2470 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
|
656
|
2471 conf->upstream.cache_lock = NGX_CONF_UNSET;
|
|
2472 conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
|
464
|
2473 #endif
|
|
2474
|
356
|
2475 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
2476 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
2477
|
206
|
2478 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
2479 #if (NGX_HTTP_SSL)
|
|
2480 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
2481 #endif
|
58
|
2482
|
|
2483 /* "proxy_cyclic_temp_file" is disabled */
|
|
2484 conf->upstream.cyclic_temp_file = 0;
|
|
2485
|
|
2486 conf->redirect = NGX_CONF_UNSET;
|
198
|
2487 conf->upstream.change_buffering = 1;
|
58
|
2488
|
640
|
2489 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
2490
|
298
|
2491 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
2492 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
2493
|
632
|
2494 ngx_str_set(&conf->upstream.module, "proxy");
|
|
2495
|
58
|
2496 return conf;
|
|
2497 }
|
|
2498
|
|
2499
|
|
2500 static char *
|
|
2501 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2502 {
|
|
2503 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
2504 ngx_http_proxy_loc_conf_t *conf = child;
|
|
2505
|
614
|
2506 u_char *p;
|
464
|
2507 size_t size;
|
|
2508 ngx_hash_init_t hash;
|
592
|
2509 ngx_http_core_loc_conf_t *clcf;
|
464
|
2510 ngx_http_proxy_redirect_t *pr;
|
|
2511 ngx_http_script_compile_t sc;
|
58
|
2512
|
320
|
2513 if (conf->upstream.store != 0) {
|
|
2514 ngx_conf_merge_value(conf->upstream.store,
|
496
|
2515 prev->upstream.store, 0);
|
320
|
2516
|
|
2517 if (conf->upstream.store_lengths == NULL) {
|
|
2518 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
2519 conf->upstream.store_values = prev->upstream.store_values;
|
|
2520 }
|
|
2521 }
|
|
2522
|
|
2523 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
2524 prev->upstream.store_access, 0600);
|
318
|
2525
|
130
|
2526 ngx_conf_merge_value(conf->upstream.buffering,
|
|
2527 prev->upstream.buffering, 1);
|
|
2528
|
178
|
2529 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
2530 prev->upstream.ignore_client_abort, 0);
|
|
2531
|
58
|
2532 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
2533 prev->upstream.connect_timeout, 60000);
|
|
2534
|
|
2535 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
2536 prev->upstream.send_timeout, 60000);
|
|
2537
|
|
2538 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
2539 prev->upstream.read_timeout, 60000);
|
|
2540
|
|
2541 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
2542 prev->upstream.send_lowat, 0);
|
|
2543
|
130
|
2544 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
2545 prev->upstream.buffer_size,
|
58
|
2546 (size_t) ngx_pagesize);
|
|
2547
|
|
2548 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
2549 8, ngx_pagesize);
|
|
2550
|
|
2551 if (conf->upstream.bufs.num < 2) {
|
126
|
2552 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
2553 "there must be at least 2 \"proxy_buffers\"");
|
|
2554 return NGX_CONF_ERROR;
|
|
2555 }
|
126
|
2556
|
58
|
2557
|
130
|
2558 size = conf->upstream.buffer_size;
|
126
|
2559 if (size < conf->upstream.bufs.size) {
|
58
|
2560 size = conf->upstream.bufs.size;
|
|
2561 }
|
|
2562
|
|
2563
|
78
|
2564 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
2565 prev->upstream.busy_buffers_size_conf,
|
58
|
2566 NGX_CONF_UNSET_SIZE);
|
|
2567
|
78
|
2568 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2569 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
2570 } else {
|
|
2571 conf->upstream.busy_buffers_size =
|
|
2572 conf->upstream.busy_buffers_size_conf;
|
|
2573 }
|
126
|
2574
|
78
|
2575 if (conf->upstream.busy_buffers_size < size) {
|
58
|
2576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2577 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
288
|
2578 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2579 "one of the \"proxy_buffers\"");
|
|
2580
|
|
2581 return NGX_CONF_ERROR;
|
78
|
2582 }
|
58
|
2583
|
78
|
2584 if (conf->upstream.busy_buffers_size
|
|
2585 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
2586 {
|
|
2587 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2588 "\"proxy_busy_buffers_size\" must be less than "
|
|
2589 "the size of all \"proxy_buffers\" minus one buffer");
|
|
2590
|
|
2591 return NGX_CONF_ERROR;
|
|
2592 }
|
78
|
2593
|
58
|
2594
|
78
|
2595 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
2596 prev->upstream.temp_file_write_size_conf,
|
58
|
2597 NGX_CONF_UNSET_SIZE);
|
|
2598
|
78
|
2599 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2600 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
2601 } else {
|
|
2602 conf->upstream.temp_file_write_size =
|
|
2603 conf->upstream.temp_file_write_size_conf;
|
|
2604 }
|
126
|
2605
|
78
|
2606 if (conf->upstream.temp_file_write_size < size) {
|
58
|
2607 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2608 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
288
|
2609 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2610 "one of the \"proxy_buffers\"");
|
|
2611
|
|
2612 return NGX_CONF_ERROR;
|
|
2613 }
|
|
2614
|
78
|
2615 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
2616 prev->upstream.max_temp_file_size_conf,
|
58
|
2617 NGX_CONF_UNSET_SIZE);
|
|
2618
|
78
|
2619 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2620 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
2621 } else {
|
|
2622 conf->upstream.max_temp_file_size =
|
|
2623 conf->upstream.max_temp_file_size_conf;
|
|
2624 }
|
58
|
2625
|
78
|
2626 if (conf->upstream.max_temp_file_size != 0
|
|
2627 && conf->upstream.max_temp_file_size < size)
|
58
|
2628 {
|
|
2629 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
2630 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
58
|
2631 "the temporary files usage or must be equal or bigger than "
|
288
|
2632 "maximum of the value of \"proxy_buffer_size\" and "
|
110
|
2633 "one of the \"proxy_buffers\"");
|
58
|
2634
|
|
2635 return NGX_CONF_ERROR;
|
|
2636 }
|
|
2637
|
78
|
2638
|
484
|
2639 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
|
|
2640 prev->upstream.ignore_headers,
|
|
2641 NGX_CONF_BITMASK_SET);
|
|
2642
|
|
2643
|
58
|
2644 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
2645 prev->upstream.next_upstream,
|
|
2646 (NGX_CONF_BITMASK_SET
|
|
2647 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
2648 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
2649
|
214
|
2650 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2651 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
2652 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2653 }
|
|
2654
|
464
|
2655 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
|
58
|
2656 prev->upstream.temp_path,
|
464
|
2657 &ngx_http_proxy_temp_path)
|
|
2658 != NGX_OK)
|
|
2659 {
|
|
2660 return NGX_CONF_ERROR;
|
|
2661 }
|
|
2662
|
|
2663
|
|
2664 #if (NGX_HTTP_CACHE)
|
|
2665
|
|
2666 ngx_conf_merge_ptr_value(conf->upstream.cache,
|
|
2667 prev->upstream.cache, NULL);
|
|
2668
|
|
2669 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
|
|
2670 ngx_shm_zone_t *shm_zone;
|
|
2671
|
|
2672 shm_zone = conf->upstream.cache;
|
|
2673
|
|
2674 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2675 "\"proxy_cache\" zone \"%V\" is unknown",
|
480
|
2676 &shm_zone->shm.name);
|
464
|
2677
|
|
2678 return NGX_CONF_ERROR;
|
|
2679 }
|
|
2680
|
|
2681 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
|
|
2682 prev->upstream.cache_min_uses, 1);
|
|
2683
|
|
2684 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
|
|
2685 prev->upstream.cache_use_stale,
|
|
2686 (NGX_CONF_BITMASK_SET
|
|
2687 |NGX_HTTP_UPSTREAM_FT_OFF));
|
|
2688
|
658
|
2689 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2690 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
|
|
2691 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2692 }
|
|
2693
|
|
2694 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
2695 conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
|
|
2696 }
|
|
2697
|
494
|
2698 if (conf->upstream.cache_methods == 0) {
|
|
2699 conf->upstream.cache_methods = prev->upstream.cache_methods;
|
|
2700 }
|
|
2701
|
|
2702 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
|
|
2703
|
588
|
2704 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
|
|
2705 prev->upstream.cache_bypass, NULL);
|
|
2706
|
572
|
2707 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
|
|
2708 prev->upstream.no_cache, NULL);
|
|
2709
|
588
|
2710 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
|
|
2711 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
2712 "\"proxy_no_cache\" functionality has been changed in 0.8.46, "
|
|
2713 "now it should be used together with \"proxy_cache_bypass\"");
|
|
2714 }
|
|
2715
|
464
|
2716 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
|
|
2717 prev->upstream.cache_valid, NULL);
|
|
2718
|
472
|
2719 if (conf->cache_key.value.data == NULL) {
|
|
2720 conf->cache_key = prev->cache_key;
|
|
2721 }
|
|
2722
|
656
|
2723 ngx_conf_merge_value(conf->upstream.cache_lock,
|
|
2724 prev->upstream.cache_lock, 0);
|
|
2725
|
|
2726 ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
|
|
2727 prev->upstream.cache_lock_timeout, 5000);
|
|
2728
|
464
|
2729 #endif
|
58
|
2730
|
110
|
2731 if (conf->method.len == 0) {
|
|
2732 conf->method = prev->method;
|
|
2733
|
|
2734 } else {
|
|
2735 conf->method.data[conf->method.len] = ' ';
|
|
2736 conf->method.len++;
|
58
|
2737 }
|
|
2738
|
|
2739 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
2740 prev->upstream.pass_request_headers, 1);
|
|
2741 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
2742 prev->upstream.pass_request_body, 1);
|
|
2743
|
206
|
2744 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
2745 prev->upstream.intercept_errors, 0);
|
58
|
2746
|
398
|
2747 #if (NGX_HTTP_SSL)
|
|
2748 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
2749 prev->upstream.ssl_session_reuse, 1);
|
|
2750 #endif
|
|
2751
|
58
|
2752 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
2753
|
|
2754 if (conf->redirect) {
|
|
2755
|
|
2756 if (conf->redirects == NULL) {
|
|
2757 conf->redirects = prev->redirects;
|
|
2758 }
|
|
2759
|
348
|
2760 if (conf->redirects == NULL && conf->url.data) {
|
58
|
2761
|
|
2762 conf->redirects = ngx_array_create(cf->pool, 1,
|
|
2763 sizeof(ngx_http_proxy_redirect_t));
|
|
2764 if (conf->redirects == NULL) {
|
|
2765 return NGX_CONF_ERROR;
|
|
2766 }
|
|
2767
|
|
2768 pr = ngx_array_push(conf->redirects);
|
|
2769 if (pr == NULL) {
|
|
2770 return NGX_CONF_ERROR;
|
|
2771 }
|
|
2772
|
654
|
2773 ngx_memzero(&pr->redirect.complex,
|
|
2774 sizeof(ngx_http_complex_value_t));
|
|
2775
|
|
2776 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
|
2777
|
|
2778 pr->handler = ngx_http_proxy_rewrite_redirect_complex;
|
348
|
2779
|
|
2780 if (conf->vars.uri.len) {
|
654
|
2781 pr->redirect.complex.value = conf->url;
|
|
2782 pr->replacement.value = conf->location;
|
202
|
2783
|
|
2784 } else {
|
654
|
2785 pr->redirect.complex.value.len = conf->url.len
|
|
2786 + sizeof("/") - 1;
|
|
2787
|
|
2788 p = ngx_pnalloc(cf->pool, pr->redirect.complex.value.len);
|
614
|
2789 if (p == NULL) {
|
|
2790 return NGX_CONF_ERROR;
|
|
2791 }
|
|
2792
|
654
|
2793 pr->redirect.complex.value.data = p;
|
614
|
2794
|
|
2795 p = ngx_cpymem(p, conf->url.data, conf->url.len);
|
|
2796 *p = '/';
|
|
2797
|
654
|
2798 ngx_str_set(&pr->replacement.value, "/");
|
202
|
2799 }
|
58
|
2800 }
|
|
2801 }
|
|
2802
|
482
|
2803 #if (NGX_HTTP_SSL)
|
|
2804 if (conf->upstream.ssl == NULL) {
|
|
2805 conf->upstream.ssl = prev->upstream.ssl;
|
|
2806 }
|
|
2807 #endif
|
|
2808
|
640
|
2809 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
|
|
2810 NGX_HTTP_VERSION_10);
|
|
2811
|
298
|
2812 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
2813 prev->headers_hash_max_size, 512);
|
|
2814
|
|
2815 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
2816 prev->headers_hash_bucket_size, 64);
|
|
2817
|
|
2818 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
2819 ngx_cacheline_size);
|
|
2820
|
356
|
2821 hash.max_size = conf->headers_hash_max_size;
|
|
2822 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2823 hash.name = "proxy_headers_hash";
|
|
2824
|
|
2825 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
584
|
2826 &prev->upstream, ngx_http_proxy_hide_headers, &hash)
|
198
|
2827 != NGX_OK)
|
|
2828 {
|
|
2829 return NGX_CONF_ERROR;
|
|
2830 }
|
|
2831
|
260
|
2832 if (conf->upstream.upstream == NULL) {
|
|
2833 conf->upstream.upstream = prev->upstream.upstream;
|
348
|
2834 conf->vars = prev->vars;
|
58
|
2835 }
|
|
2836
|
592
|
2837 if (conf->proxy_lengths == NULL) {
|
|
2838 conf->proxy_lengths = prev->proxy_lengths;
|
|
2839 conf->proxy_values = prev->proxy_values;
|
|
2840 }
|
|
2841
|
|
2842 if (conf->upstream.upstream || conf->proxy_lengths) {
|
|
2843 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2844 if (clcf->handler == NULL && clcf->lmt_excpt) {
|
|
2845 clcf->handler = ngx_http_proxy_handler;
|
|
2846 conf->location = prev->location;
|
|
2847 }
|
|
2848 }
|
126
|
2849
|
|
2850 if (conf->body_source.data == NULL) {
|
|
2851 conf->body_source = prev->body_source;
|
|
2852 conf->body_set_len = prev->body_set_len;
|
|
2853 conf->body_set = prev->body_set;
|
|
2854 }
|
|
2855
|
128
|
2856 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
2857
|
|
2858 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2859
|
|
2860 sc.cf = cf;
|
|
2861 sc.source = &conf->body_source;
|
|
2862 sc.flushes = &conf->flushes;
|
|
2863 sc.lengths = &conf->body_set_len;
|
|
2864 sc.values = &conf->body_set;
|
|
2865 sc.complete_lengths = 1;
|
|
2866 sc.complete_values = 1;
|
|
2867
|
|
2868 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2869 return NGX_CONF_ERROR;
|
|
2870 }
|
|
2871 }
|
|
2872
|
464
|
2873 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
|
|
2874 return NGX_CONF_ERROR;
|
|
2875 }
|
|
2876
|
|
2877 return NGX_CONF_OK;
|
|
2878 }
|
|
2879
|
|
2880
|
|
2881 static ngx_int_t
|
|
2882 ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
|
|
2883 ngx_http_proxy_loc_conf_t *prev)
|
|
2884 {
|
|
2885 u_char *p;
|
|
2886 size_t size;
|
|
2887 uintptr_t *code;
|
|
2888 ngx_uint_t i;
|
648
|
2889 ngx_array_t headers_names, headers_merged;
|
464
|
2890 ngx_keyval_t *src, *s, *h;
|
|
2891 ngx_hash_key_t *hk;
|
|
2892 ngx_hash_init_t hash;
|
|
2893 ngx_http_script_compile_t sc;
|
|
2894 ngx_http_script_copy_code_t *copy;
|
126
|
2895
|
58
|
2896 if (conf->headers_source == NULL) {
|
122
|
2897 conf->flushes = prev->flushes;
|
58
|
2898 conf->headers_set_len = prev->headers_set_len;
|
|
2899 conf->headers_set = prev->headers_set;
|
|
2900 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
2901 conf->headers_source = prev->headers_source;
|
58
|
2902 }
|
|
2903
|
498
|
2904 if (conf->headers_set_hash.buckets
|
648
|
2905 && ((conf->body_source.data == NULL)
|
|
2906 == (prev->body_source.data == NULL))
|
502
|
2907 #if (NGX_HTTP_CACHE)
|
|
2908 && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
|
|
2909 #endif
|
|
2910 )
|
498
|
2911 {
|
464
|
2912 return NGX_OK;
|
58
|
2913 }
|
|
2914
|
|
2915
|
576
|
2916 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
2917 != NGX_OK)
|
|
2918 {
|
464
|
2919 return NGX_ERROR;
|
58
|
2920 }
|
|
2921
|
648
|
2922 if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
|
|
2923 != NGX_OK)
|
|
2924 {
|
|
2925 return NGX_ERROR;
|
|
2926 }
|
|
2927
|
58
|
2928 if (conf->headers_source == NULL) {
|
|
2929 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
2930 sizeof(ngx_keyval_t));
|
58
|
2931 if (conf->headers_source == NULL) {
|
464
|
2932 return NGX_ERROR;
|
58
|
2933 }
|
|
2934 }
|
|
2935
|
|
2936 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
2937 if (conf->headers_set_len == NULL) {
|
464
|
2938 return NGX_ERROR;
|
58
|
2939 }
|
|
2940
|
|
2941 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
2942 if (conf->headers_set == NULL) {
|
464
|
2943 return NGX_ERROR;
|
58
|
2944 }
|
|
2945
|
|
2946
|
480
|
2947 #if (NGX_HTTP_CACHE)
|
|
2948
|
|
2949 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
|
|
2950 ngx_http_proxy_headers;
|
|
2951 #else
|
|
2952
|
|
2953 h = ngx_http_proxy_headers;
|
|
2954
|
|
2955 #endif
|
|
2956
|
648
|
2957 src = conf->headers_source->elts;
|
|
2958 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2959
|
|
2960 s = ngx_array_push(&headers_merged);
|
|
2961 if (s == NULL) {
|
|
2962 return NGX_ERROR;
|
|
2963 }
|
|
2964
|
|
2965 *s = src[i];
|
|
2966 }
|
|
2967
|
480
|
2968 while (h->key.len) {
|
58
|
2969
|
648
|
2970 src = headers_merged.elts;
|
|
2971 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
2972 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
2973 goto next;
|
|
2974 }
|
|
2975 }
|
|
2976
|
648
|
2977 s = ngx_array_push(&headers_merged);
|
58
|
2978 if (s == NULL) {
|
464
|
2979 return NGX_ERROR;
|
58
|
2980 }
|
|
2981
|
|
2982 *s = *h;
|
|
2983
|
|
2984 next:
|
|
2985
|
480
|
2986 h++;
|
58
|
2987 }
|
|
2988
|
648
|
2989 if (conf->body_source.data) {
|
|
2990 s = ngx_array_push(&headers_merged);
|
|
2991 if (s == NULL) {
|
|
2992 return NGX_ERROR;
|
|
2993 }
|
|
2994
|
|
2995 ngx_str_set(&s->key, "Content-Length");
|
|
2996 ngx_str_set(&s->value, "$proxy_internal_body_length");
|
|
2997 }
|
|
2998
|
|
2999
|
|
3000 src = headers_merged.elts;
|
|
3001 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
3002
|
576
|
3003 hk = ngx_array_push(&headers_names);
|
198
|
3004 if (hk == NULL) {
|
464
|
3005 return NGX_ERROR;
|
58
|
3006 }
|
|
3007
|
198
|
3008 hk->key = src[i].key;
|
|
3009 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
3010 hk->value = (void *) 1;
|
58
|
3011
|
68
|
3012 if (src[i].value.len == 0) {
|
|
3013 continue;
|
|
3014 }
|
|
3015
|
58
|
3016 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
3017 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3018 sizeof(ngx_http_script_copy_code_t));
|
|
3019 if (copy == NULL) {
|
464
|
3020 return NGX_ERROR;
|
58
|
3021 }
|
|
3022
|
|
3023 copy->code = (ngx_http_script_code_pt)
|
|
3024 ngx_http_script_copy_len_code;
|
|
3025 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
3026 + src[i].value.len + sizeof(CRLF) - 1;
|
|
3027
|
|
3028
|
|
3029 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3030 + src[i].key.len + sizeof(": ") - 1
|
|
3031 + src[i].value.len + sizeof(CRLF) - 1
|
|
3032 + sizeof(uintptr_t) - 1)
|
|
3033 & ~(sizeof(uintptr_t) - 1);
|
|
3034
|
|
3035 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3036 if (copy == NULL) {
|
464
|
3037 return NGX_ERROR;
|
58
|
3038 }
|
|
3039
|
|
3040 copy->code = ngx_http_script_copy_code;
|
|
3041 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
3042 + src[i].value.len + sizeof(CRLF) - 1;
|
|
3043
|
|
3044 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3045
|
|
3046 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3047 *p++ = ':'; *p++ = ' ';
|
|
3048 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
3049 *p++ = CR; *p = LF;
|
|
3050
|
|
3051 } else {
|
|
3052 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3053 sizeof(ngx_http_script_copy_code_t));
|
|
3054 if (copy == NULL) {
|
464
|
3055 return NGX_ERROR;
|
58
|
3056 }
|
|
3057
|
|
3058 copy->code = (ngx_http_script_code_pt)
|
|
3059 ngx_http_script_copy_len_code;
|
|
3060 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3061
|
|
3062
|
|
3063 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3064 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
3065 & ~(sizeof(uintptr_t) - 1);
|
|
3066
|
|
3067 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3068 if (copy == NULL) {
|
464
|
3069 return NGX_ERROR;
|
58
|
3070 }
|
|
3071
|
|
3072 copy->code = ngx_http_script_copy_code;
|
|
3073 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3074
|
|
3075 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3076 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3077 *p++ = ':'; *p = ' ';
|
|
3078
|
|
3079
|
|
3080 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3081
|
|
3082 sc.cf = cf;
|
|
3083 sc.source = &src[i].value;
|
122
|
3084 sc.flushes = &conf->flushes;
|
58
|
3085 sc.lengths = &conf->headers_set_len;
|
|
3086 sc.values = &conf->headers_set;
|
|
3087
|
|
3088 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
464
|
3089 return NGX_ERROR;
|
58
|
3090 }
|
|
3091
|
|
3092
|
|
3093 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3094 sizeof(ngx_http_script_copy_code_t));
|
|
3095 if (copy == NULL) {
|
464
|
3096 return NGX_ERROR;
|
58
|
3097 }
|
|
3098
|
|
3099 copy->code = (ngx_http_script_code_pt)
|
|
3100 ngx_http_script_copy_len_code;
|
|
3101 copy->len = sizeof(CRLF) - 1;
|
|
3102
|
|
3103
|
|
3104 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3105 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
3106 & ~(sizeof(uintptr_t) - 1);
|
|
3107
|
|
3108 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3109 if (copy == NULL) {
|
464
|
3110 return NGX_ERROR;
|
58
|
3111 }
|
|
3112
|
|
3113 copy->code = ngx_http_script_copy_code;
|
|
3114 copy->len = sizeof(CRLF) - 1;
|
|
3115
|
|
3116 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3117 *p++ = CR; *p = LF;
|
|
3118 }
|
|
3119
|
|
3120 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3121 if (code == NULL) {
|
464
|
3122 return NGX_ERROR;
|
58
|
3123 }
|
|
3124
|
|
3125 *code = (uintptr_t) NULL;
|
|
3126
|
|
3127 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
3128 if (code == NULL) {
|
464
|
3129 return NGX_ERROR;
|
58
|
3130 }
|
|
3131
|
|
3132 *code = (uintptr_t) NULL;
|
|
3133 }
|
|
3134
|
|
3135 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3136 if (code == NULL) {
|
464
|
3137 return NGX_ERROR;
|
58
|
3138 }
|
|
3139
|
|
3140 *code = (uintptr_t) NULL;
|
|
3141
|
|
3142
|
198
|
3143 hash.hash = &conf->headers_set_hash;
|
|
3144 hash.key = ngx_hash_key_lc;
|
298
|
3145 hash.max_size = conf->headers_hash_max_size;
|
|
3146 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
3147 hash.name = "proxy_headers_hash";
|
198
|
3148 hash.pool = cf->pool;
|
|
3149 hash.temp_pool = NULL;
|
|
3150
|
576
|
3151 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
|
58
|
3152 }
|
|
3153
|
|
3154
|
|
3155 static char *
|
|
3156 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3157 {
|
|
3158 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3159
|
348
|
3160 size_t add;
|
|
3161 u_short port;
|
|
3162 ngx_str_t *value, *url;
|
|
3163 ngx_url_t u;
|
|
3164 ngx_uint_t n;
|
|
3165 ngx_http_core_loc_conf_t *clcf;
|
|
3166 ngx_http_script_compile_t sc;
|
58
|
3167
|
430
|
3168 if (plcf->upstream.upstream || plcf->proxy_lengths) {
|
104
|
3169 return "is duplicate";
|
|
3170 }
|
|
3171
|
348
|
3172 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
3173
|
510
|
3174 clcf->handler = ngx_http_proxy_handler;
|
|
3175
|
|
3176 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
3177 clcf->auto_redirect = 1;
|
|
3178 }
|
|
3179
|
58
|
3180 value = cf->args->elts;
|
|
3181
|
|
3182 url = &value[1];
|
|
3183
|
348
|
3184 n = ngx_http_script_variables_count(url);
|
|
3185
|
|
3186 if (n) {
|
|
3187
|
|
3188 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3189
|
|
3190 sc.cf = cf;
|
|
3191 sc.source = url;
|
|
3192 sc.lengths = &plcf->proxy_lengths;
|
|
3193 sc.values = &plcf->proxy_values;
|
|
3194 sc.variables = n;
|
|
3195 sc.complete_lengths = 1;
|
|
3196 sc.complete_values = 1;
|
354
|
3197
|
348
|
3198 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3199 return NGX_CONF_ERROR;
|
|
3200 }
|
|
3201
|
|
3202 #if (NGX_HTTP_SSL)
|
|
3203 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3204 return NGX_CONF_ERROR;
|
|
3205 }
|
|
3206 #endif
|
|
3207
|
|
3208 return NGX_CONF_OK;
|
|
3209 }
|
|
3210
|
286
|
3211 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
3212 add = 7;
|
140
|
3213 port = 80;
|
126
|
3214
|
286
|
3215 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
3216
|
|
3217 #if (NGX_HTTP_SSL)
|
348
|
3218 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3219 return NGX_CONF_ERROR;
|
|
3220 }
|
126
|
3221
|
|
3222 add = 8;
|
140
|
3223 port = 443;
|
126
|
3224 #else
|
|
3225 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3226 "https protocol requires SSL support");
|
|
3227 return NGX_CONF_ERROR;
|
|
3228 #endif
|
|
3229
|
|
3230 } else {
|
58
|
3231 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
3232 return NGX_CONF_ERROR;
|
|
3233 }
|
|
3234
|
200
|
3235 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3236
|
|
3237 u.url.len = url->len - add;
|
|
3238 u.url.data = url->data + add;
|
266
|
3239 u.default_port = port;
|
|
3240 u.uri_part = 1;
|
260
|
3241 u.no_resolve = 1;
|
|
3242
|
|
3243 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
3244 if (plcf->upstream.upstream == NULL) {
|
58
|
3245 return NGX_CONF_ERROR;
|
|
3246 }
|
|
3247
|
430
|
3248 plcf->vars.schema.len = add;
|
|
3249 plcf->vars.schema.data = url->data;
|
464
|
3250 plcf->vars.key_start = plcf->vars.schema;
|
|
3251
|
|
3252 ngx_http_proxy_set_vars(&u, &plcf->vars);
|
58
|
3253
|
464
|
3254 plcf->location = clcf->name;
|
|
3255
|
324
|
3256 if (clcf->named
|
58
|
3257 #if (NGX_PCRE)
|
324
|
3258 || clcf->regex
|
|
3259 #endif
|
|
3260 || clcf->noname)
|
|
3261 {
|
348
|
3262 if (plcf->vars.uri.len) {
|
122
|
3263 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3264 "\"proxy_pass\" may not have URI part in "
|
256
|
3265 "location given by regular expression, "
|
324
|
3266 "or inside named location, "
|
256
|
3267 "or inside the \"if\" statement, "
|
|
3268 "or inside the \"limit_except\" block");
|
122
|
3269 return NGX_CONF_ERROR;
|
|
3270 }
|
|
3271
|
348
|
3272 plcf->location.len = 0;
|
104
|
3273 }
|
|
3274
|
348
|
3275 plcf->url = *url;
|
58
|
3276
|
|
3277 return NGX_CONF_OK;
|
|
3278 }
|
|
3279
|
|
3280
|
|
3281 static char *
|
|
3282 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3283 {
|
|
3284 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3285
|
654
|
3286 u_char *p;
|
|
3287 ngx_str_t *value;
|
|
3288 ngx_http_proxy_redirect_t *pr;
|
|
3289 ngx_http_compile_complex_value_t ccv;
|
58
|
3290
|
|
3291 if (plcf->redirect == 0) {
|
|
3292 return NGX_CONF_OK;
|
|
3293 }
|
|
3294
|
660
|
3295 plcf->redirect = 1;
|
|
3296
|
58
|
3297 value = cf->args->elts;
|
|
3298
|
484
|
3299 if (cf->args->nelts == 2) {
|
|
3300 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3301 plcf->redirect = 0;
|
|
3302 plcf->redirects = NULL;
|
|
3303 return NGX_CONF_OK;
|
|
3304 }
|
|
3305
|
|
3306 if (ngx_strcmp(value[1].data, "false") == 0) {
|
|
3307 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
3308 "invalid parameter \"false\", use \"off\" instead");
|
|
3309 plcf->redirect = 0;
|
|
3310 plcf->redirects = NULL;
|
|
3311 return NGX_CONF_OK;
|
|
3312 }
|
|
3313
|
|
3314 if (ngx_strcmp(value[1].data, "default") != 0) {
|
|
3315 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3316 "invalid parameter \"%V\"", &value[1]);
|
|
3317 return NGX_CONF_ERROR;
|
|
3318 }
|
58
|
3319 }
|
|
3320
|
|
3321 if (plcf->redirects == NULL) {
|
|
3322 plcf->redirects = ngx_array_create(cf->pool, 1,
|
|
3323 sizeof(ngx_http_proxy_redirect_t));
|
|
3324 if (plcf->redirects == NULL) {
|
|
3325 return NGX_CONF_ERROR;
|
|
3326 }
|
|
3327 }
|
|
3328
|
|
3329 pr = ngx_array_push(plcf->redirects);
|
|
3330 if (pr == NULL) {
|
|
3331 return NGX_CONF_ERROR;
|
|
3332 }
|
|
3333
|
484
|
3334 if (ngx_strcmp(value[1].data, "default") == 0) {
|
566
|
3335 if (plcf->proxy_lengths) {
|
|
3336 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3337 "\"proxy_redirect default\" may not be used "
|
|
3338 "with \"proxy_pass\" directive with variables");
|
|
3339 return NGX_CONF_ERROR;
|
|
3340 }
|
|
3341
|
348
|
3342 if (plcf->url.data == NULL) {
|
58
|
3343 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
566
|
3344 "\"proxy_redirect default\" must go "
|
58
|
3345 "after the \"proxy_pass\" directive");
|
|
3346 return NGX_CONF_ERROR;
|
|
3347 }
|
|
3348
|
654
|
3349 pr->handler = ngx_http_proxy_rewrite_redirect_complex;
|
|
3350
|
|
3351 ngx_memzero(&pr->redirect.complex, sizeof(ngx_http_complex_value_t));
|
|
3352
|
|
3353 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
348
|
3354
|
|
3355 if (plcf->vars.uri.len) {
|
654
|
3356 pr->redirect.complex.value = plcf->url;
|
|
3357 pr->replacement.value = plcf->location;
|
202
|
3358
|
|
3359 } else {
|
654
|
3360 pr->redirect.complex.value.len = plcf->url.len + sizeof("/") - 1;
|
|
3361
|
|
3362 p = ngx_pnalloc(cf->pool, pr->redirect.complex.value.len);
|
614
|
3363 if (p == NULL) {
|
|
3364 return NGX_CONF_ERROR;
|
|
3365 }
|
|
3366
|
654
|
3367 pr->redirect.complex.value.data = p;
|
614
|
3368
|
|
3369 p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
|
|
3370 *p = '/';
|
|
3371
|
654
|
3372 ngx_str_set(&pr->replacement.value, "/");
|
202
|
3373 }
|
58
|
3374
|
|
3375 return NGX_CONF_OK;
|
|
3376 }
|
|
3377
|
654
|
3378
|
|
3379 if (value[1].data[0] == '~') {
|
|
3380 #if (NGX_PCRE)
|
|
3381 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
3382 ngx_regex_compile_t rc;
|
|
3383
|
|
3384 value[1].len--;
|
|
3385 value[1].data++;
|
|
3386
|
|
3387 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
3388
|
|
3389 if (value[1].data[0] == '*') {
|
|
3390 value[1].len--;
|
|
3391 value[1].data++;
|
|
3392 rc.options = NGX_REGEX_CASELESS;
|
|
3393 }
|
|
3394
|
|
3395 rc.pattern = value[1];
|
|
3396 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
3397 rc.err.data = errstr;
|
|
3398
|
|
3399 pr->redirect.regex = ngx_http_regex_compile(cf, &rc);
|
|
3400 if (pr->redirect.regex == NULL) {
|
|
3401 return NGX_CONF_ERROR;
|
|
3402 }
|
|
3403
|
|
3404 pr->handler = ngx_http_proxy_rewrite_redirect_regex;
|
|
3405
|
|
3406 #else
|
|
3407 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3408 "using regex \"%V\" requires PCRE library",
|
|
3409 &value[1]);
|
|
3410
|
|
3411 return NGX_CONF_ERROR;
|
|
3412 #endif
|
|
3413 } else {
|
|
3414
|
|
3415 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3416
|
|
3417 ccv.cf = cf;
|
|
3418 ccv.value = &value[1];
|
|
3419 ccv.complex_value = &pr->redirect.complex;
|
|
3420
|
|
3421 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3422 return NGX_CONF_ERROR;
|
|
3423 }
|
|
3424
|
|
3425 pr->handler = ngx_http_proxy_rewrite_redirect_complex;
|
58
|
3426 }
|
|
3427
|
654
|
3428
|
|
3429 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3430
|
|
3431 ccv.cf = cf;
|
|
3432 ccv.value = &value[2];
|
|
3433 ccv.complex_value = &pr->replacement;
|
|
3434
|
|
3435 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
58
|
3436 return NGX_CONF_ERROR;
|
|
3437 }
|
|
3438
|
|
3439 return NGX_CONF_OK;
|
|
3440 }
|
|
3441
|
|
3442
|
|
3443 static char *
|
320
|
3444 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3445 {
|
|
3446 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3447
|
|
3448 ngx_str_t *value;
|
|
3449 ngx_http_script_compile_t sc;
|
|
3450
|
496
|
3451 if (plcf->upstream.store != NGX_CONF_UNSET
|
|
3452 || plcf->upstream.store_lengths)
|
320
|
3453 {
|
|
3454 return "is duplicate";
|
|
3455 }
|
|
3456
|
|
3457 value = cf->args->elts;
|
|
3458
|
496
|
3459 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3460 plcf->upstream.store = 0;
|
320
|
3461 return NGX_CONF_OK;
|
|
3462 }
|
|
3463
|
496
|
3464 #if (NGX_HTTP_CACHE)
|
|
3465
|
|
3466 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
|
|
3467 && plcf->upstream.cache != NULL)
|
|
3468 {
|
|
3469 return "is incompatible with \"proxy_cache\"";
|
|
3470 }
|
|
3471
|
|
3472 #endif
|
|
3473
|
|
3474 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
3475 plcf->upstream.store = 1;
|
320
|
3476 return NGX_CONF_OK;
|
|
3477 }
|
|
3478
|
|
3479 /* include the terminating '\0' into script */
|
|
3480 value[1].len++;
|
|
3481
|
|
3482 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3483
|
|
3484 sc.cf = cf;
|
|
3485 sc.source = &value[1];
|
|
3486 sc.lengths = &plcf->upstream.store_lengths;
|
|
3487 sc.values = &plcf->upstream.store_values;
|
340
|
3488 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
3489 sc.complete_lengths = 1;
|
|
3490 sc.complete_values = 1;
|
|
3491
|
|
3492 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3493 return NGX_CONF_ERROR;
|
|
3494 }
|
|
3495
|
|
3496 return NGX_CONF_OK;
|
|
3497 }
|
|
3498
|
|
3499
|
464
|
3500 #if (NGX_HTTP_CACHE)
|
|
3501
|
|
3502 static char *
|
|
3503 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3504 {
|
|
3505 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3506
|
|
3507 ngx_str_t *value;
|
|
3508
|
|
3509 value = cf->args->elts;
|
|
3510
|
|
3511 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
|
|
3512 return "is duplicate";
|
|
3513 }
|
|
3514
|
|
3515 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3516 plcf->upstream.cache = NULL;
|
|
3517 return NGX_CONF_OK;
|
|
3518 }
|
|
3519
|
496
|
3520 if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
|
|
3521 return "is incompatible with \"proxy_store\"";
|
|
3522 }
|
|
3523
|
464
|
3524 plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
|
|
3525 &ngx_http_proxy_module);
|
|
3526 if (plcf->upstream.cache == NULL) {
|
|
3527 return NGX_CONF_ERROR;
|
|
3528 }
|
|
3529
|
|
3530 return NGX_CONF_OK;
|
|
3531 }
|
|
3532
|
472
|
3533
|
|
3534 static char *
|
|
3535 ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3536 {
|
|
3537 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3538
|
|
3539 ngx_str_t *value;
|
|
3540 ngx_http_compile_complex_value_t ccv;
|
|
3541
|
|
3542 value = cf->args->elts;
|
|
3543
|
|
3544 if (plcf->cache_key.value.len) {
|
|
3545 return "is duplicate";
|
|
3546 }
|
|
3547
|
|
3548 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3549
|
|
3550 ccv.cf = cf;
|
|
3551 ccv.value = &value[1];
|
|
3552 ccv.complex_value = &plcf->cache_key;
|
|
3553
|
|
3554 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3555 return NGX_CONF_ERROR;
|
|
3556 }
|
|
3557
|
|
3558 return NGX_CONF_OK;
|
|
3559 }
|
|
3560
|
464
|
3561 #endif
|
|
3562
|
|
3563
|
320
|
3564 static char *
|
58
|
3565 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
3566 {
|
|
3567 #if (NGX_FREEBSD)
|
|
3568 ssize_t *np = data;
|
|
3569
|
222
|
3570 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
3571 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3572 "\"proxy_send_lowat\" must be less than %d "
|
|
3573 "(sysctl net.inet.tcp.sendspace)",
|
|
3574 ngx_freebsd_net_inet_tcp_sendspace);
|
|
3575
|
|
3576 return NGX_CONF_ERROR;
|
|
3577 }
|
|
3578
|
|
3579 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
3580 ssize_t *np = data;
|
|
3581
|
|
3582 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
3583 "\"proxy_send_lowat\" is not supported, ignored");
|
|
3584
|
|
3585 *np = 0;
|
|
3586
|
|
3587 #endif
|
|
3588
|
|
3589 return NGX_CONF_OK;
|
|
3590 }
|
260
|
3591
|
|
3592
|
348
|
3593 #if (NGX_HTTP_SSL)
|
|
3594
|
|
3595 static ngx_int_t
|
|
3596 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
3597 {
|
|
3598 ngx_pool_cleanup_t *cln;
|
|
3599
|
|
3600 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
3601 if (plcf->upstream.ssl == NULL) {
|
|
3602 return NGX_ERROR;
|
|
3603 }
|
|
3604
|
|
3605 plcf->upstream.ssl->log = cf->log;
|
|
3606
|
|
3607 if (ngx_ssl_create(plcf->upstream.ssl,
|
658
|
3608 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
3609 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2,
|
|
3610 NULL)
|
348
|
3611 != NGX_OK)
|
|
3612 {
|
|
3613 return NGX_ERROR;
|
|
3614 }
|
|
3615
|
|
3616 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
3617 if (cln == NULL) {
|
|
3618 return NGX_ERROR;
|
|
3619 }
|
|
3620
|
|
3621 cln->handler = ngx_ssl_cleanup_ctx;
|
|
3622 cln->data = plcf->upstream.ssl;
|
|
3623
|
|
3624 return NGX_OK;
|
|
3625 }
|
|
3626
|
|
3627 #endif
|
|
3628
|
|
3629
|
464
|
3630 static void
|
|
3631 ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
|
348
|
3632 {
|
400
|
3633 if (u->family != AF_UNIX) {
|
464
|
3634
|
348
|
3635 if (u->no_port || u->port == u->default_port) {
|
464
|
3636
|
348
|
3637 v->host_header = u->host;
|
|
3638
|
|
3639 if (u->default_port == 80) {
|
570
|
3640 ngx_str_set(&v->port, "80");
|
348
|
3641
|
|
3642 } else {
|
570
|
3643 ngx_str_set(&v->port, "443");
|
348
|
3644 }
|
|
3645
|
|
3646 } else {
|
354
|
3647 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
3648 v->host_header.data = u->host.data;
|
|
3649 v->port = u->port_text;
|
348
|
3650 }
|
|
3651
|
464
|
3652 v->key_start.len += v->host_header.len;
|
|
3653
|
348
|
3654 } else {
|
570
|
3655 ngx_str_set(&v->host_header, "localhost");
|
|
3656 ngx_str_null(&v->port);
|
464
|
3657 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
|
348
|
3658 }
|
|
3659
|
|
3660 v->uri = u->uri;
|
|
3661 }
|