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