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