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
|
642
|
1562 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
644
|
1563 "upstream sent too much data");
|
640
|
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 ';':
|
642
|
1650 case ' ':
|
|
1651 case '\t':
|
640
|
1652 state = sw_last_chunk_extension;
|
|
1653 break;
|
|
1654 default:
|
|
1655 goto invalid;
|
|
1656 }
|
|
1657
|
|
1658 break;
|
|
1659 }
|
|
1660
|
|
1661 switch (ch) {
|
|
1662 case CR:
|
|
1663 state = sw_chunk_extension_almost_done;
|
|
1664 break;
|
|
1665 case LF:
|
|
1666 state = sw_chunk_data;
|
|
1667 break;
|
|
1668 case ';':
|
642
|
1669 case ' ':
|
|
1670 case '\t':
|
640
|
1671 state = sw_chunk_extension;
|
|
1672 break;
|
|
1673 default:
|
|
1674 goto invalid;
|
|
1675 }
|
|
1676
|
|
1677 break;
|
|
1678
|
|
1679 case sw_chunk_extension:
|
|
1680 switch (ch) {
|
|
1681 case CR:
|
|
1682 state = sw_chunk_extension_almost_done;
|
|
1683 break;
|
|
1684 case LF:
|
|
1685 state = sw_chunk_data;
|
|
1686 }
|
|
1687 break;
|
|
1688
|
|
1689 case sw_chunk_extension_almost_done:
|
|
1690 if (ch == LF) {
|
|
1691 state = sw_chunk_data;
|
|
1692 break;
|
|
1693 }
|
|
1694 goto invalid;
|
|
1695
|
|
1696 case sw_chunk_data:
|
|
1697 rc = NGX_OK;
|
|
1698 goto data;
|
|
1699
|
|
1700 case sw_after_data:
|
|
1701 switch (ch) {
|
|
1702 case CR:
|
|
1703 state = sw_after_data_almost_done;
|
|
1704 break;
|
|
1705 case LF:
|
|
1706 state = sw_chunk_start;
|
|
1707 }
|
|
1708 break;
|
|
1709
|
|
1710 case sw_after_data_almost_done:
|
|
1711 if (ch == LF) {
|
|
1712 state = sw_chunk_start;
|
|
1713 break;
|
|
1714 }
|
|
1715 goto invalid;
|
|
1716
|
|
1717 case sw_last_chunk_extension:
|
|
1718 switch (ch) {
|
|
1719 case CR:
|
|
1720 state = sw_last_chunk_extension_almost_done;
|
|
1721 break;
|
|
1722 case LF:
|
|
1723 state = sw_trailer;
|
|
1724 }
|
|
1725 break;
|
|
1726
|
|
1727 case sw_last_chunk_extension_almost_done:
|
|
1728 if (ch == LF) {
|
|
1729 state = sw_trailer;
|
|
1730 break;
|
|
1731 }
|
|
1732 goto invalid;
|
|
1733
|
|
1734 case sw_trailer:
|
|
1735 switch (ch) {
|
|
1736 case CR:
|
|
1737 state = sw_trailer_almost_done;
|
|
1738 break;
|
|
1739 case LF:
|
|
1740 goto done;
|
|
1741 default:
|
|
1742 state = sw_trailer_header;
|
|
1743 }
|
|
1744 break;
|
|
1745
|
|
1746 case sw_trailer_almost_done:
|
|
1747 if (ch == LF) {
|
|
1748 goto done;
|
|
1749 }
|
|
1750 goto invalid;
|
|
1751
|
|
1752 case sw_trailer_header:
|
|
1753 switch (ch) {
|
|
1754 case CR:
|
|
1755 state = sw_trailer_header_almost_done;
|
|
1756 break;
|
|
1757 case LF:
|
|
1758 state = sw_trailer;
|
|
1759 }
|
|
1760 break;
|
|
1761
|
|
1762 case sw_trailer_header_almost_done:
|
|
1763 if (ch == LF) {
|
|
1764 state = sw_trailer;
|
|
1765 break;
|
|
1766 }
|
|
1767 goto invalid;
|
|
1768
|
|
1769 }
|
|
1770 }
|
|
1771
|
|
1772 data:
|
|
1773
|
|
1774 ctx->state = state;
|
|
1775 buf->pos = pos;
|
|
1776
|
|
1777 switch (state) {
|
|
1778
|
|
1779 case sw_chunk_start:
|
|
1780 ctx->length = 3 /* "0" LF LF */;
|
|
1781 break;
|
|
1782 case sw_chunk_size:
|
|
1783 ctx->length = 2 /* LF LF */
|
|
1784 + (ctx->size ? ctx->size + 4 /* LF "0" LF LF */ : 0);
|
|
1785 break;
|
|
1786 case sw_chunk_extension:
|
|
1787 case sw_chunk_extension_almost_done:
|
|
1788 ctx->length = 1 /* LF */ + ctx->size + 4 /* LF "0" LF LF */;
|
|
1789 break;
|
|
1790 case sw_chunk_data:
|
|
1791 ctx->length = ctx->size + 4 /* LF "0" LF LF */;
|
|
1792 break;
|
|
1793 case sw_after_data:
|
|
1794 case sw_after_data_almost_done:
|
|
1795 ctx->length = 4 /* LF "0" LF LF */;
|
|
1796 break;
|
|
1797 case sw_last_chunk_extension:
|
|
1798 case sw_last_chunk_extension_almost_done:
|
|
1799 ctx->length = 2 /* LF LF */;
|
|
1800 break;
|
|
1801 case sw_trailer:
|
|
1802 case sw_trailer_almost_done:
|
|
1803 ctx->length = 1 /* LF */;
|
|
1804 break;
|
|
1805 case sw_trailer_header:
|
|
1806 case sw_trailer_header_almost_done:
|
|
1807 ctx->length = 2 /* LF LF */;
|
|
1808 break;
|
|
1809
|
|
1810 }
|
|
1811
|
|
1812 return rc;
|
|
1813
|
|
1814 done:
|
|
1815
|
|
1816 return NGX_DONE;
|
|
1817
|
|
1818 invalid:
|
|
1819
|
|
1820 return NGX_ERROR;
|
|
1821 }
|
|
1822
|
|
1823
|
|
1824 static ngx_int_t
|
|
1825 ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1826 {
|
|
1827 ngx_int_t rc;
|
|
1828 ngx_buf_t *b, **prev;
|
|
1829 ngx_chain_t *cl;
|
|
1830 ngx_http_request_t *r;
|
|
1831 ngx_http_proxy_ctx_t *ctx;
|
|
1832
|
|
1833 if (buf->pos == buf->last) {
|
|
1834 return NGX_OK;
|
|
1835 }
|
|
1836
|
|
1837 r = p->input_ctx;
|
|
1838 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1839
|
|
1840 b = NULL;
|
|
1841 prev = &buf->shadow;
|
|
1842
|
|
1843 for ( ;; ) {
|
|
1844
|
|
1845 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
1846
|
|
1847 if (rc == NGX_OK) {
|
|
1848
|
|
1849 /* a chunk has been parsed successfully */
|
|
1850
|
|
1851 if (p->free) {
|
|
1852 cl = p->free;
|
|
1853 b = cl->buf;
|
|
1854 p->free = cl->next;
|
|
1855 ngx_free_chain(p->pool, cl);
|
|
1856
|
|
1857 } else {
|
|
1858 b = ngx_alloc_buf(p->pool);
|
|
1859 if (b == NULL) {
|
|
1860 return NGX_ERROR;
|
|
1861 }
|
|
1862 }
|
|
1863
|
|
1864 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
1865
|
|
1866 b->pos = buf->pos;
|
|
1867 b->start = buf->start;
|
|
1868 b->end = buf->end;
|
|
1869 b->tag = p->tag;
|
|
1870 b->temporary = 1;
|
|
1871 b->recycled = 1;
|
|
1872
|
|
1873 *prev = b;
|
|
1874 prev = &b->shadow;
|
|
1875
|
|
1876 cl = ngx_alloc_chain_link(p->pool);
|
|
1877 if (cl == NULL) {
|
|
1878 return NGX_ERROR;
|
|
1879 }
|
|
1880
|
|
1881 cl->buf = b;
|
|
1882 cl->next = NULL;
|
|
1883
|
|
1884 if (p->in) {
|
|
1885 *p->last_in = cl;
|
|
1886 } else {
|
|
1887 p->in = cl;
|
|
1888 }
|
|
1889 p->last_in = &cl->next;
|
|
1890
|
|
1891 /* STUB */ b->num = buf->num;
|
|
1892
|
|
1893 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1894 "input buf #%d %p", b->num, b->pos);
|
|
1895
|
|
1896 if (buf->last - buf->pos >= ctx->size) {
|
|
1897
|
|
1898 buf->pos += ctx->size;
|
|
1899 b->last = buf->pos;
|
|
1900 ctx->size = 0;
|
|
1901
|
|
1902 continue;
|
|
1903 }
|
|
1904
|
|
1905 ctx->size -= buf->last - buf->pos;
|
|
1906 buf->pos = buf->last;
|
|
1907 b->last = buf->last;
|
|
1908
|
|
1909 continue;
|
|
1910 }
|
|
1911
|
|
1912 if (rc == NGX_DONE) {
|
|
1913
|
|
1914 /* a whole response has been parsed successfully */
|
|
1915
|
|
1916 p->upstream_done = 1;
|
|
1917 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1918
|
|
1919 break;
|
|
1920 }
|
|
1921
|
|
1922 if (rc == NGX_AGAIN) {
|
|
1923
|
|
1924 /* set p->length, minimal amount of data we want to see */
|
|
1925
|
|
1926 p->length = ctx->length;
|
|
1927
|
|
1928 break;
|
|
1929 }
|
|
1930
|
|
1931 /* invalid response */
|
|
1932
|
642
|
1933 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
1934 "upstream sent invalid chunked response");
|
|
1935
|
|
1936 return NGX_ERROR;
|
|
1937 }
|
|
1938
|
|
1939 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1940 "http proxy chunked state %d, length %d",
|
|
1941 ctx->state, p->length);
|
|
1942
|
|
1943 if (b) {
|
|
1944 b->shadow = buf;
|
|
1945 b->last_shadow = 1;
|
|
1946
|
|
1947 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1948 "input buf %p %z", b->pos, b->last - b->pos);
|
|
1949
|
|
1950 return NGX_OK;
|
|
1951 }
|
|
1952
|
|
1953 /* there is no data record in the buf, add it to free chain */
|
|
1954
|
|
1955 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
|
|
1956 return NGX_ERROR;
|
|
1957 }
|
|
1958
|
|
1959 return NGX_OK;
|
|
1960 }
|
|
1961
|
|
1962
|
|
1963 static ngx_int_t
|
|
1964 ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
|
|
1965 {
|
|
1966 ngx_http_request_t *r = data;
|
|
1967
|
|
1968 ngx_buf_t *b;
|
|
1969 ngx_chain_t *cl, **ll;
|
|
1970 ngx_http_upstream_t *u;
|
|
1971
|
|
1972 u = r->upstream;
|
|
1973
|
|
1974 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
1975 ll = &cl->next;
|
|
1976 }
|
|
1977
|
|
1978 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
1979 if (cl == NULL) {
|
|
1980 return NGX_ERROR;
|
|
1981 }
|
|
1982
|
|
1983 *ll = cl;
|
|
1984
|
|
1985 cl->buf->flush = 1;
|
|
1986 cl->buf->memory = 1;
|
|
1987
|
|
1988 b = &u->buffer;
|
|
1989
|
|
1990 cl->buf->pos = b->last;
|
|
1991 b->last += bytes;
|
|
1992 cl->buf->last = b->last;
|
|
1993 cl->buf->tag = u->output.tag;
|
|
1994
|
|
1995 if (u->length == -1) {
|
|
1996 return NGX_OK;
|
|
1997 }
|
|
1998
|
|
1999 u->length -= bytes;
|
|
2000
|
|
2001 if (u->length == 0) {
|
|
2002 u->keepalive = !u->headers_in.connection_close;
|
|
2003 }
|
|
2004
|
|
2005 return NGX_OK;
|
|
2006 }
|
|
2007
|
|
2008
|
|
2009 static ngx_int_t
|
|
2010 ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
|
|
2011 {
|
|
2012 ngx_http_request_t *r = data;
|
|
2013
|
|
2014 ngx_int_t rc;
|
|
2015 ngx_buf_t *b, *buf;
|
|
2016 ngx_chain_t *cl, **ll;
|
|
2017 ngx_http_upstream_t *u;
|
|
2018 ngx_http_proxy_ctx_t *ctx;
|
|
2019
|
|
2020 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2021 u = r->upstream;
|
|
2022 buf = &u->buffer;
|
|
2023
|
|
2024 buf->pos = buf->last;
|
|
2025 buf->last += bytes;
|
|
2026
|
|
2027 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
2028 ll = &cl->next;
|
|
2029 }
|
|
2030
|
|
2031 for ( ;; ) {
|
|
2032
|
|
2033 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
2034
|
|
2035 if (rc == NGX_OK) {
|
|
2036
|
|
2037 /* a chunk has been parsed successfully */
|
|
2038
|
|
2039 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
2040 if (cl == NULL) {
|
|
2041 return NGX_ERROR;
|
|
2042 }
|
|
2043
|
|
2044 *ll = cl;
|
|
2045 ll = &cl->next;
|
|
2046
|
|
2047 b = cl->buf;
|
|
2048
|
|
2049 b->flush = 1;
|
|
2050 b->memory = 1;
|
|
2051
|
|
2052 b->pos = buf->pos;
|
|
2053 b->tag = u->output.tag;
|
|
2054
|
|
2055 if (buf->last - buf->pos >= ctx->size) {
|
|
2056 buf->pos += ctx->size;
|
|
2057 b->last = buf->pos;
|
|
2058 ctx->size = 0;
|
|
2059
|
|
2060 } else {
|
|
2061 ctx->size -= buf->last - buf->pos;
|
|
2062 buf->pos = buf->last;
|
|
2063 b->last = buf->last;
|
|
2064 }
|
|
2065
|
|
2066 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2067 "http proxy out buf %p %z",
|
|
2068 b->pos, b->last - b->pos);
|
|
2069
|
|
2070 continue;
|
|
2071 }
|
|
2072
|
|
2073 if (rc == NGX_DONE) {
|
|
2074
|
|
2075 /* a whole response has been parsed successfully */
|
|
2076
|
|
2077 u->keepalive = !u->headers_in.connection_close;
|
|
2078 u->length = 0;
|
|
2079
|
|
2080 break;
|
|
2081 }
|
|
2082
|
|
2083 if (rc == NGX_AGAIN) {
|
|
2084 break;
|
|
2085 }
|
|
2086
|
|
2087 /* invalid response */
|
|
2088
|
642
|
2089 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
2090 "upstream sent invalid chunked response");
|
|
2091
|
|
2092 return NGX_ERROR;
|
|
2093 }
|
|
2094
|
|
2095 /* provide continuous buffer for subrequests in memory */
|
|
2096
|
|
2097 if (r->subrequest_in_memory) {
|
|
2098
|
|
2099 cl = u->out_bufs;
|
|
2100
|
|
2101 if (cl) {
|
|
2102 buf->pos = cl->buf->pos;
|
|
2103 }
|
|
2104
|
|
2105 buf->last = buf->pos;
|
|
2106
|
|
2107 for (cl = u->out_bufs; cl; cl = cl->next) {
|
|
2108 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2109 "http proxy in memory %p-%p %uz",
|
|
2110 cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf));
|
|
2111
|
|
2112 if (buf->last == cl->buf->pos) {
|
|
2113 buf->last = cl->buf->last;
|
|
2114 continue;
|
|
2115 }
|
|
2116
|
|
2117 buf->last = ngx_movemem(buf->last, cl->buf->pos,
|
|
2118 cl->buf->last - cl->buf->pos);
|
|
2119
|
|
2120 cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos);
|
|
2121 cl->buf->last = buf->last;
|
|
2122 }
|
|
2123 }
|
|
2124
|
|
2125 return NGX_OK;
|
|
2126 }
|
|
2127
|
|
2128
|
58
|
2129 static void
|
|
2130 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
2131 {
|
|
2132 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2133 "abort http proxy request");
|
126
|
2134
|
58
|
2135 return;
|
|
2136 }
|
|
2137
|
|
2138
|
|
2139 static void
|
|
2140 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
2141 {
|
58
|
2142 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2143 "finalize http proxy request");
|
|
2144
|
|
2145 return;
|
|
2146 }
|
|
2147
|
|
2148
|
122
|
2149 static ngx_int_t
|
|
2150 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
2151 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2152 {
|
348
|
2153 ngx_http_proxy_ctx_t *ctx;
|
|
2154
|
|
2155 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2156
|
|
2157 if (ctx == NULL) {
|
|
2158 v->not_found = 1;
|
|
2159 return NGX_OK;
|
|
2160 }
|
|
2161
|
|
2162 v->len = ctx->vars.host_header.len;
|
126
|
2163 v->valid = 1;
|
340
|
2164 v->no_cacheable = 0;
|
122
|
2165 v->not_found = 0;
|
348
|
2166 v->data = ctx->vars.host_header.data;
|
58
|
2167
|
122
|
2168 return NGX_OK;
|
58
|
2169 }
|
|
2170
|
|
2171
|
122
|
2172 static ngx_int_t
|
|
2173 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
2174 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2175 {
|
348
|
2176 ngx_http_proxy_ctx_t *ctx;
|
|
2177
|
|
2178 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2179
|
|
2180 if (ctx == NULL) {
|
|
2181 v->not_found = 1;
|
|
2182 return NGX_OK;
|
|
2183 }
|
|
2184
|
|
2185 v->len = ctx->vars.port.len;
|
126
|
2186 v->valid = 1;
|
340
|
2187 v->no_cacheable = 0;
|
122
|
2188 v->not_found = 0;
|
348
|
2189 v->data = ctx->vars.port.data;
|
58
|
2190
|
122
|
2191 return NGX_OK;
|
58
|
2192 }
|
|
2193
|
|
2194
|
122
|
2195 static ngx_int_t
|
58
|
2196 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
2197 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2198 {
|
122
|
2199 u_char *p;
|
58
|
2200
|
126
|
2201 v->valid = 1;
|
340
|
2202 v->no_cacheable = 0;
|
122
|
2203 v->not_found = 0;
|
58
|
2204
|
|
2205 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
2206 v->len = r->connection->addr_text.len;
|
|
2207 v->data = r->connection->addr_text.data;
|
|
2208 return NGX_OK;
|
58
|
2209 }
|
|
2210
|
122
|
2211 v->len = r->headers_in.x_forwarded_for->value.len
|
|
2212 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
2213
|
382
|
2214 p = ngx_pnalloc(r->pool, v->len);
|
58
|
2215 if (p == NULL) {
|
122
|
2216 return NGX_ERROR;
|
58
|
2217 }
|
|
2218
|
122
|
2219 v->data = p;
|
58
|
2220
|
122
|
2221 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
2222 r->headers_in.x_forwarded_for->value.len);
|
58
|
2223
|
|
2224 *p++ = ','; *p++ = ' ';
|
|
2225
|
|
2226 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
2227
|
122
|
2228 return NGX_OK;
|
58
|
2229 }
|
|
2230
|
|
2231
|
|
2232 static ngx_int_t
|
126
|
2233 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
2234 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2235 {
|
348
|
2236 ngx_http_proxy_ctx_t *ctx;
|
|
2237
|
|
2238 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2239
|
|
2240 if (ctx == NULL) {
|
126
|
2241 v->not_found = 1;
|
|
2242 return NGX_OK;
|
|
2243 }
|
|
2244
|
|
2245 v->valid = 1;
|
340
|
2246 v->no_cacheable = 0;
|
126
|
2247 v->not_found = 0;
|
|
2248
|
382
|
2249 v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
|
126
|
2250
|
|
2251 if (v->data == NULL) {
|
|
2252 return NGX_ERROR;
|
|
2253 }
|
|
2254
|
348
|
2255 v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
|
126
|
2256
|
|
2257 return NGX_OK;
|
|
2258 }
|
|
2259
|
|
2260
|
|
2261 static ngx_int_t
|
58
|
2262 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2263 size_t prefix)
|
|
2264 {
|
|
2265 ngx_int_t rc;
|
|
2266 ngx_uint_t i;
|
|
2267 ngx_http_proxy_loc_conf_t *plcf;
|
|
2268 ngx_http_proxy_redirect_t *pr;
|
|
2269
|
|
2270 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2271
|
|
2272 pr = plcf->redirects->elts;
|
|
2273
|
|
2274 if (pr == NULL) {
|
|
2275 return NGX_DECLINED;
|
|
2276 }
|
|
2277
|
|
2278 for (i = 0; i < plcf->redirects->nelts; i++) {
|
154
|
2279 rc = pr[i].handler(r, h, prefix, &pr[i]);
|
58
|
2280
|
|
2281 if (rc != NGX_DECLINED) {
|
|
2282 return rc;
|
|
2283 }
|
|
2284 }
|
|
2285
|
|
2286 return NGX_DECLINED;
|
|
2287 }
|
|
2288
|
|
2289
|
|
2290 static ngx_int_t
|
|
2291 ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2292 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
2293 {
|
|
2294 size_t len;
|
|
2295 u_char *data, *p;
|
|
2296
|
|
2297 if (pr->redirect.len > h->value.len - prefix
|
|
2298 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
2299 pr->redirect.len) != 0)
|
|
2300 {
|
|
2301 return NGX_DECLINED;
|
|
2302 }
|
|
2303
|
578
|
2304 len = pr->replacement.text.len + h->value.len - pr->redirect.len;
|
58
|
2305
|
382
|
2306 data = ngx_pnalloc(r->pool, len);
|
58
|
2307 if (data == NULL) {
|
|
2308 return NGX_ERROR;
|
|
2309 }
|
|
2310
|
578
|
2311 p = ngx_copy(data, h->value.data, prefix);
|
58
|
2312
|
202
|
2313 if (pr->replacement.text.len) {
|
|
2314 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len);
|
|
2315 }
|
58
|
2316
|
|
2317 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len,
|
|
2318 h->value.len - pr->redirect.len - prefix);
|
|
2319
|
|
2320 h->value.len = len;
|
|
2321 h->value.data = data;
|
|
2322
|
|
2323 return NGX_OK;
|
|
2324 }
|
|
2325
|
|
2326
|
|
2327 static ngx_int_t
|
|
2328 ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2329 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
2330 {
|
|
2331 size_t len;
|
|
2332 u_char *data, *p;
|
|
2333 ngx_http_script_code_pt code;
|
|
2334 ngx_http_script_engine_t e;
|
|
2335 ngx_http_script_len_code_pt lcode;
|
|
2336
|
|
2337 if (pr->redirect.len > h->value.len - prefix
|
|
2338 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
2339 pr->redirect.len) != 0)
|
|
2340 {
|
|
2341 return NGX_DECLINED;
|
|
2342 }
|
|
2343
|
|
2344 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
|
2345
|
|
2346 e.ip = pr->replacement.vars.lengths;
|
|
2347 e.request = r;
|
|
2348
|
578
|
2349 len = h->value.len - pr->redirect.len;
|
238
|
2350
|
|
2351 while (*(uintptr_t *) e.ip) {
|
58
|
2352 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
238
|
2353 len += lcode(&e);
|
58
|
2354 }
|
|
2355
|
382
|
2356 data = ngx_pnalloc(r->pool, len);
|
58
|
2357 if (data == NULL) {
|
|
2358 return NGX_ERROR;
|
|
2359 }
|
|
2360
|
578
|
2361 p = ngx_copy(data, h->value.data, prefix);
|
58
|
2362
|
|
2363 e.ip = pr->replacement.vars.values;
|
|
2364 e.pos = p;
|
|
2365
|
|
2366 while (*(uintptr_t *) e.ip) {
|
|
2367 code = *(ngx_http_script_code_pt *) e.ip;
|
|
2368 code(&e);
|
|
2369 }
|
|
2370
|
238
|
2371 ngx_memcpy(e.pos, h->value.data + prefix + pr->redirect.len,
|
|
2372 h->value.len - pr->redirect.len - prefix);
|
|
2373
|
58
|
2374 h->value.len = len;
|
|
2375 h->value.data = data;
|
|
2376
|
|
2377 return NGX_OK;
|
|
2378 }
|
|
2379
|
|
2380
|
|
2381 static ngx_int_t
|
|
2382 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
2383 {
|
|
2384 ngx_http_variable_t *var, *v;
|
|
2385
|
|
2386 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
2387 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2388 if (var == NULL) {
|
|
2389 return NGX_ERROR;
|
|
2390 }
|
|
2391
|
186
|
2392 var->get_handler = v->get_handler;
|
58
|
2393 var->data = v->data;
|
|
2394 }
|
|
2395
|
|
2396 return NGX_OK;
|
|
2397 }
|
|
2398
|
|
2399
|
|
2400 static void *
|
|
2401 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
2402 {
|
|
2403 ngx_http_proxy_loc_conf_t *conf;
|
|
2404
|
|
2405 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
2406 if (conf == NULL) {
|
496
|
2407 return NULL;
|
58
|
2408 }
|
|
2409
|
|
2410 /*
|
|
2411 * set by ngx_pcalloc():
|
|
2412 *
|
|
2413 * conf->upstream.bufs.num = 0;
|
484
|
2414 * conf->upstream.ignore_headers = 0;
|
58
|
2415 * conf->upstream.next_upstream = 0;
|
494
|
2416 * conf->upstream.cache_use_stale = 0;
|
|
2417 * conf->upstream.cache_methods = 0;
|
58
|
2418 * conf->upstream.temp_path = NULL;
|
198
|
2419 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
2420 * conf->upstream.uri = { 0, NULL };
|
|
2421 * conf->upstream.location = NULL;
|
320
|
2422 * conf->upstream.store_lengths = NULL;
|
|
2423 * conf->upstream.store_values = NULL;
|
58
|
2424 *
|
110
|
2425 * conf->method = NULL;
|
58
|
2426 * conf->headers_source = NULL;
|
|
2427 * conf->headers_set_len = NULL;
|
|
2428 * conf->headers_set = NULL;
|
|
2429 * conf->headers_set_hash = NULL;
|
126
|
2430 * conf->body_set_len = NULL;
|
|
2431 * conf->body_set = NULL;
|
|
2432 * conf->body_source = { 0, NULL };
|
566
|
2433 * conf->redirects = NULL;
|
58
|
2434 */
|
126
|
2435
|
320
|
2436 conf->upstream.store = NGX_CONF_UNSET;
|
|
2437 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
2438 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
2439 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
2440
|
58
|
2441 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
2442 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
2443 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
2444
|
|
2445 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
2446 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
2447
|
|
2448 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
2449 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
2450 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
2451
|
|
2452 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
2453 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
2454
|
464
|
2455 #if (NGX_HTTP_CACHE)
|
|
2456 conf->upstream.cache = NGX_CONF_UNSET_PTR;
|
|
2457 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
|
588
|
2458 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
|
572
|
2459 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
|
464
|
2460 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
|
|
2461 #endif
|
|
2462
|
356
|
2463 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
2464 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
2465
|
206
|
2466 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
2467 #if (NGX_HTTP_SSL)
|
|
2468 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
2469 #endif
|
58
|
2470
|
|
2471 /* "proxy_cyclic_temp_file" is disabled */
|
|
2472 conf->upstream.cyclic_temp_file = 0;
|
|
2473
|
|
2474 conf->redirect = NGX_CONF_UNSET;
|
198
|
2475 conf->upstream.change_buffering = 1;
|
58
|
2476
|
640
|
2477 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
2478
|
298
|
2479 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
2480 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
2481
|
632
|
2482 ngx_str_set(&conf->upstream.module, "proxy");
|
|
2483
|
58
|
2484 return conf;
|
|
2485 }
|
|
2486
|
|
2487
|
|
2488 static char *
|
|
2489 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2490 {
|
|
2491 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
2492 ngx_http_proxy_loc_conf_t *conf = child;
|
|
2493
|
614
|
2494 u_char *p;
|
464
|
2495 size_t size;
|
|
2496 ngx_keyval_t *s;
|
|
2497 ngx_hash_init_t hash;
|
592
|
2498 ngx_http_core_loc_conf_t *clcf;
|
464
|
2499 ngx_http_proxy_redirect_t *pr;
|
|
2500 ngx_http_script_compile_t sc;
|
58
|
2501
|
320
|
2502 if (conf->upstream.store != 0) {
|
|
2503 ngx_conf_merge_value(conf->upstream.store,
|
496
|
2504 prev->upstream.store, 0);
|
320
|
2505
|
|
2506 if (conf->upstream.store_lengths == NULL) {
|
|
2507 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
2508 conf->upstream.store_values = prev->upstream.store_values;
|
|
2509 }
|
|
2510 }
|
|
2511
|
|
2512 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
2513 prev->upstream.store_access, 0600);
|
318
|
2514
|
130
|
2515 ngx_conf_merge_value(conf->upstream.buffering,
|
|
2516 prev->upstream.buffering, 1);
|
|
2517
|
178
|
2518 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
2519 prev->upstream.ignore_client_abort, 0);
|
|
2520
|
58
|
2521 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
2522 prev->upstream.connect_timeout, 60000);
|
|
2523
|
|
2524 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
2525 prev->upstream.send_timeout, 60000);
|
|
2526
|
|
2527 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
2528 prev->upstream.read_timeout, 60000);
|
|
2529
|
|
2530 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
2531 prev->upstream.send_lowat, 0);
|
|
2532
|
130
|
2533 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
2534 prev->upstream.buffer_size,
|
58
|
2535 (size_t) ngx_pagesize);
|
|
2536
|
|
2537 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
2538 8, ngx_pagesize);
|
|
2539
|
|
2540 if (conf->upstream.bufs.num < 2) {
|
126
|
2541 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
2542 "there must be at least 2 \"proxy_buffers\"");
|
|
2543 return NGX_CONF_ERROR;
|
|
2544 }
|
126
|
2545
|
58
|
2546
|
130
|
2547 size = conf->upstream.buffer_size;
|
126
|
2548 if (size < conf->upstream.bufs.size) {
|
58
|
2549 size = conf->upstream.bufs.size;
|
|
2550 }
|
|
2551
|
|
2552
|
78
|
2553 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
2554 prev->upstream.busy_buffers_size_conf,
|
58
|
2555 NGX_CONF_UNSET_SIZE);
|
|
2556
|
78
|
2557 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2558 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
2559 } else {
|
|
2560 conf->upstream.busy_buffers_size =
|
|
2561 conf->upstream.busy_buffers_size_conf;
|
|
2562 }
|
126
|
2563
|
78
|
2564 if (conf->upstream.busy_buffers_size < size) {
|
58
|
2565 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2566 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
288
|
2567 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2568 "one of the \"proxy_buffers\"");
|
|
2569
|
|
2570 return NGX_CONF_ERROR;
|
78
|
2571 }
|
58
|
2572
|
78
|
2573 if (conf->upstream.busy_buffers_size
|
|
2574 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
2575 {
|
|
2576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2577 "\"proxy_busy_buffers_size\" must be less than "
|
|
2578 "the size of all \"proxy_buffers\" minus one buffer");
|
|
2579
|
|
2580 return NGX_CONF_ERROR;
|
|
2581 }
|
78
|
2582
|
58
|
2583
|
78
|
2584 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
2585 prev->upstream.temp_file_write_size_conf,
|
58
|
2586 NGX_CONF_UNSET_SIZE);
|
|
2587
|
78
|
2588 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2589 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
2590 } else {
|
|
2591 conf->upstream.temp_file_write_size =
|
|
2592 conf->upstream.temp_file_write_size_conf;
|
|
2593 }
|
126
|
2594
|
78
|
2595 if (conf->upstream.temp_file_write_size < size) {
|
58
|
2596 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2597 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
288
|
2598 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2599 "one of the \"proxy_buffers\"");
|
|
2600
|
|
2601 return NGX_CONF_ERROR;
|
|
2602 }
|
|
2603
|
78
|
2604 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
2605 prev->upstream.max_temp_file_size_conf,
|
58
|
2606 NGX_CONF_UNSET_SIZE);
|
|
2607
|
78
|
2608 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2609 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
2610 } else {
|
|
2611 conf->upstream.max_temp_file_size =
|
|
2612 conf->upstream.max_temp_file_size_conf;
|
|
2613 }
|
58
|
2614
|
78
|
2615 if (conf->upstream.max_temp_file_size != 0
|
|
2616 && conf->upstream.max_temp_file_size < size)
|
58
|
2617 {
|
|
2618 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
2619 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
58
|
2620 "the temporary files usage or must be equal or bigger than "
|
288
|
2621 "maximum of the value of \"proxy_buffer_size\" and "
|
110
|
2622 "one of the \"proxy_buffers\"");
|
58
|
2623
|
|
2624 return NGX_CONF_ERROR;
|
|
2625 }
|
|
2626
|
78
|
2627
|
484
|
2628 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
|
|
2629 prev->upstream.ignore_headers,
|
|
2630 NGX_CONF_BITMASK_SET);
|
|
2631
|
|
2632
|
58
|
2633 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
2634 prev->upstream.next_upstream,
|
|
2635 (NGX_CONF_BITMASK_SET
|
|
2636 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
2637 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
2638
|
214
|
2639 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2640 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
2641 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2642 }
|
|
2643
|
464
|
2644 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
|
58
|
2645 prev->upstream.temp_path,
|
464
|
2646 &ngx_http_proxy_temp_path)
|
|
2647 != NGX_OK)
|
|
2648 {
|
|
2649 return NGX_CONF_ERROR;
|
|
2650 }
|
|
2651
|
|
2652
|
|
2653 #if (NGX_HTTP_CACHE)
|
|
2654
|
|
2655 ngx_conf_merge_ptr_value(conf->upstream.cache,
|
|
2656 prev->upstream.cache, NULL);
|
|
2657
|
|
2658 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
|
|
2659 ngx_shm_zone_t *shm_zone;
|
|
2660
|
|
2661 shm_zone = conf->upstream.cache;
|
|
2662
|
|
2663 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2664 "\"proxy_cache\" zone \"%V\" is unknown",
|
480
|
2665 &shm_zone->shm.name);
|
464
|
2666
|
|
2667 return NGX_CONF_ERROR;
|
|
2668 }
|
|
2669
|
|
2670 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
|
|
2671 prev->upstream.cache_min_uses, 1);
|
|
2672
|
|
2673 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
|
|
2674 prev->upstream.cache_use_stale,
|
|
2675 (NGX_CONF_BITMASK_SET
|
|
2676 |NGX_HTTP_UPSTREAM_FT_OFF));
|
|
2677
|
494
|
2678 if (conf->upstream.cache_methods == 0) {
|
|
2679 conf->upstream.cache_methods = prev->upstream.cache_methods;
|
|
2680 }
|
|
2681
|
|
2682 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
|
|
2683
|
464
|
2684 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2685 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
|
|
2686 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2687 }
|
|
2688
|
588
|
2689 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
|
|
2690 prev->upstream.cache_bypass, NULL);
|
|
2691
|
572
|
2692 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
|
|
2693 prev->upstream.no_cache, NULL);
|
|
2694
|
588
|
2695 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
|
|
2696 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
2697 "\"proxy_no_cache\" functionality has been changed in 0.8.46, "
|
|
2698 "now it should be used together with \"proxy_cache_bypass\"");
|
|
2699 }
|
|
2700
|
464
|
2701 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
|
|
2702 prev->upstream.cache_valid, NULL);
|
|
2703
|
472
|
2704 if (conf->cache_key.value.data == NULL) {
|
|
2705 conf->cache_key = prev->cache_key;
|
|
2706 }
|
|
2707
|
464
|
2708 #endif
|
58
|
2709
|
110
|
2710 if (conf->method.len == 0) {
|
|
2711 conf->method = prev->method;
|
|
2712
|
|
2713 } else {
|
|
2714 conf->method.data[conf->method.len] = ' ';
|
|
2715 conf->method.len++;
|
58
|
2716 }
|
|
2717
|
|
2718 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
2719 prev->upstream.pass_request_headers, 1);
|
|
2720 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
2721 prev->upstream.pass_request_body, 1);
|
|
2722
|
206
|
2723 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
2724 prev->upstream.intercept_errors, 0);
|
58
|
2725
|
398
|
2726 #if (NGX_HTTP_SSL)
|
|
2727 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
2728 prev->upstream.ssl_session_reuse, 1);
|
|
2729 #endif
|
|
2730
|
58
|
2731 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
2732
|
|
2733 if (conf->redirect) {
|
|
2734
|
|
2735 if (conf->redirects == NULL) {
|
|
2736 conf->redirects = prev->redirects;
|
|
2737 }
|
|
2738
|
348
|
2739 if (conf->redirects == NULL && conf->url.data) {
|
58
|
2740
|
|
2741 conf->redirects = ngx_array_create(cf->pool, 1,
|
|
2742 sizeof(ngx_http_proxy_redirect_t));
|
|
2743 if (conf->redirects == NULL) {
|
|
2744 return NGX_CONF_ERROR;
|
|
2745 }
|
|
2746
|
|
2747 pr = ngx_array_push(conf->redirects);
|
|
2748 if (pr == NULL) {
|
|
2749 return NGX_CONF_ERROR;
|
|
2750 }
|
|
2751
|
|
2752 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
2753
|
|
2754 if (conf->vars.uri.len) {
|
614
|
2755 pr->redirect = conf->url;
|
348
|
2756 pr->replacement.text = conf->location;
|
202
|
2757
|
|
2758 } else {
|
614
|
2759 pr->redirect.len = conf->url.len + sizeof("/") - 1;
|
|
2760
|
|
2761 p = ngx_pnalloc(cf->pool, pr->redirect.len);
|
|
2762 if (p == NULL) {
|
|
2763 return NGX_CONF_ERROR;
|
|
2764 }
|
|
2765
|
|
2766 pr->redirect.data = p;
|
|
2767
|
|
2768 p = ngx_cpymem(p, conf->url.data, conf->url.len);
|
|
2769 *p = '/';
|
|
2770
|
|
2771 ngx_str_set(&pr->replacement.text, "/");
|
202
|
2772 }
|
58
|
2773 }
|
|
2774 }
|
|
2775
|
482
|
2776 #if (NGX_HTTP_SSL)
|
|
2777 if (conf->upstream.ssl == NULL) {
|
|
2778 conf->upstream.ssl = prev->upstream.ssl;
|
|
2779 }
|
|
2780 #endif
|
|
2781
|
640
|
2782 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
|
|
2783 NGX_HTTP_VERSION_10);
|
|
2784
|
298
|
2785 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
2786 prev->headers_hash_max_size, 512);
|
|
2787
|
|
2788 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
2789 prev->headers_hash_bucket_size, 64);
|
|
2790
|
|
2791 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
2792 ngx_cacheline_size);
|
|
2793
|
356
|
2794 hash.max_size = conf->headers_hash_max_size;
|
|
2795 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2796 hash.name = "proxy_headers_hash";
|
|
2797
|
|
2798 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
584
|
2799 &prev->upstream, ngx_http_proxy_hide_headers, &hash)
|
198
|
2800 != NGX_OK)
|
|
2801 {
|
|
2802 return NGX_CONF_ERROR;
|
|
2803 }
|
|
2804
|
260
|
2805 if (conf->upstream.upstream == NULL) {
|
|
2806 conf->upstream.upstream = prev->upstream.upstream;
|
348
|
2807 conf->vars = prev->vars;
|
58
|
2808 }
|
|
2809
|
592
|
2810 if (conf->proxy_lengths == NULL) {
|
|
2811 conf->proxy_lengths = prev->proxy_lengths;
|
|
2812 conf->proxy_values = prev->proxy_values;
|
|
2813 }
|
|
2814
|
|
2815 if (conf->upstream.upstream || conf->proxy_lengths) {
|
|
2816 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2817 if (clcf->handler == NULL && clcf->lmt_excpt) {
|
|
2818 clcf->handler = ngx_http_proxy_handler;
|
|
2819 conf->location = prev->location;
|
|
2820 }
|
|
2821 }
|
126
|
2822
|
|
2823 if (conf->body_source.data == NULL) {
|
|
2824 conf->body_source = prev->body_source;
|
|
2825 conf->body_set_len = prev->body_set_len;
|
|
2826 conf->body_set = prev->body_set;
|
|
2827 }
|
|
2828
|
128
|
2829 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
2830
|
|
2831 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2832
|
|
2833 sc.cf = cf;
|
|
2834 sc.source = &conf->body_source;
|
|
2835 sc.flushes = &conf->flushes;
|
|
2836 sc.lengths = &conf->body_set_len;
|
|
2837 sc.values = &conf->body_set;
|
|
2838 sc.complete_lengths = 1;
|
|
2839 sc.complete_values = 1;
|
|
2840
|
|
2841 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2842 return NGX_CONF_ERROR;
|
|
2843 }
|
|
2844
|
|
2845 if (conf->headers_source == NULL) {
|
|
2846 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
2847 sizeof(ngx_keyval_t));
|
126
|
2848 if (conf->headers_source == NULL) {
|
|
2849 return NGX_CONF_ERROR;
|
|
2850 }
|
|
2851 }
|
|
2852
|
|
2853 s = ngx_array_push(conf->headers_source);
|
|
2854 if (s == NULL) {
|
|
2855 return NGX_CONF_ERROR;
|
|
2856 }
|
|
2857
|
570
|
2858 ngx_str_set(&s->key, "Content-Length");
|
|
2859 ngx_str_set(&s->value, "$proxy_internal_body_length");
|
126
|
2860 }
|
|
2861
|
464
|
2862 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
|
|
2863 return NGX_CONF_ERROR;
|
|
2864 }
|
|
2865
|
|
2866 return NGX_CONF_OK;
|
|
2867 }
|
|
2868
|
|
2869
|
|
2870 static ngx_int_t
|
|
2871 ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
|
|
2872 ngx_http_proxy_loc_conf_t *prev)
|
|
2873 {
|
|
2874 u_char *p;
|
|
2875 size_t size;
|
|
2876 uintptr_t *code;
|
|
2877 ngx_uint_t i;
|
576
|
2878 ngx_array_t headers_names;
|
464
|
2879 ngx_keyval_t *src, *s, *h;
|
|
2880 ngx_hash_key_t *hk;
|
|
2881 ngx_hash_init_t hash;
|
|
2882 ngx_http_script_compile_t sc;
|
|
2883 ngx_http_script_copy_code_t *copy;
|
126
|
2884
|
58
|
2885 if (conf->headers_source == NULL) {
|
122
|
2886 conf->flushes = prev->flushes;
|
58
|
2887 conf->headers_set_len = prev->headers_set_len;
|
|
2888 conf->headers_set = prev->headers_set;
|
|
2889 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
2890 conf->headers_source = prev->headers_source;
|
58
|
2891 }
|
|
2892
|
498
|
2893 if (conf->headers_set_hash.buckets
|
502
|
2894 #if (NGX_HTTP_CACHE)
|
|
2895 && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
|
|
2896 #endif
|
|
2897 )
|
498
|
2898 {
|
464
|
2899 return NGX_OK;
|
58
|
2900 }
|
|
2901
|
|
2902
|
576
|
2903 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
2904 != NGX_OK)
|
|
2905 {
|
464
|
2906 return NGX_ERROR;
|
58
|
2907 }
|
|
2908
|
|
2909 if (conf->headers_source == NULL) {
|
|
2910 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
2911 sizeof(ngx_keyval_t));
|
58
|
2912 if (conf->headers_source == NULL) {
|
464
|
2913 return NGX_ERROR;
|
58
|
2914 }
|
|
2915 }
|
|
2916
|
|
2917 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
2918 if (conf->headers_set_len == NULL) {
|
464
|
2919 return NGX_ERROR;
|
58
|
2920 }
|
|
2921
|
|
2922 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
2923 if (conf->headers_set == NULL) {
|
464
|
2924 return NGX_ERROR;
|
58
|
2925 }
|
|
2926
|
|
2927
|
|
2928 src = conf->headers_source->elts;
|
|
2929
|
480
|
2930 #if (NGX_HTTP_CACHE)
|
|
2931
|
|
2932 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
|
|
2933 ngx_http_proxy_headers;
|
|
2934 #else
|
|
2935
|
|
2936 h = ngx_http_proxy_headers;
|
|
2937
|
|
2938 #endif
|
|
2939
|
|
2940 while (h->key.len) {
|
58
|
2941
|
|
2942 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2943 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
2944 goto next;
|
|
2945 }
|
|
2946 }
|
|
2947
|
|
2948 s = ngx_array_push(conf->headers_source);
|
|
2949 if (s == NULL) {
|
464
|
2950 return NGX_ERROR;
|
58
|
2951 }
|
|
2952
|
|
2953 *s = *h;
|
|
2954
|
66
|
2955 src = conf->headers_source->elts;
|
|
2956
|
58
|
2957 next:
|
|
2958
|
480
|
2959 h++;
|
58
|
2960 }
|
|
2961
|
126
|
2962
|
|
2963 src = conf->headers_source->elts;
|
58
|
2964 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2965
|
576
|
2966 hk = ngx_array_push(&headers_names);
|
198
|
2967 if (hk == NULL) {
|
464
|
2968 return NGX_ERROR;
|
58
|
2969 }
|
|
2970
|
198
|
2971 hk->key = src[i].key;
|
|
2972 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
2973 hk->value = (void *) 1;
|
58
|
2974
|
68
|
2975 if (src[i].value.len == 0) {
|
|
2976 continue;
|
|
2977 }
|
|
2978
|
58
|
2979 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
2980 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2981 sizeof(ngx_http_script_copy_code_t));
|
|
2982 if (copy == NULL) {
|
464
|
2983 return NGX_ERROR;
|
58
|
2984 }
|
|
2985
|
|
2986 copy->code = (ngx_http_script_code_pt)
|
|
2987 ngx_http_script_copy_len_code;
|
|
2988 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2989 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2990
|
|
2991
|
|
2992 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2993 + src[i].key.len + sizeof(": ") - 1
|
|
2994 + src[i].value.len + sizeof(CRLF) - 1
|
|
2995 + sizeof(uintptr_t) - 1)
|
|
2996 & ~(sizeof(uintptr_t) - 1);
|
|
2997
|
|
2998 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2999 if (copy == NULL) {
|
464
|
3000 return NGX_ERROR;
|
58
|
3001 }
|
|
3002
|
|
3003 copy->code = ngx_http_script_copy_code;
|
|
3004 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
3005 + src[i].value.len + sizeof(CRLF) - 1;
|
|
3006
|
|
3007 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3008
|
|
3009 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3010 *p++ = ':'; *p++ = ' ';
|
|
3011 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
3012 *p++ = CR; *p = LF;
|
|
3013
|
|
3014 } else {
|
|
3015 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3016 sizeof(ngx_http_script_copy_code_t));
|
|
3017 if (copy == NULL) {
|
464
|
3018 return NGX_ERROR;
|
58
|
3019 }
|
|
3020
|
|
3021 copy->code = (ngx_http_script_code_pt)
|
|
3022 ngx_http_script_copy_len_code;
|
|
3023 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3024
|
|
3025
|
|
3026 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3027 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
3028 & ~(sizeof(uintptr_t) - 1);
|
|
3029
|
|
3030 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3031 if (copy == NULL) {
|
464
|
3032 return NGX_ERROR;
|
58
|
3033 }
|
|
3034
|
|
3035 copy->code = ngx_http_script_copy_code;
|
|
3036 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3037
|
|
3038 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3039 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3040 *p++ = ':'; *p = ' ';
|
|
3041
|
|
3042
|
|
3043 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3044
|
|
3045 sc.cf = cf;
|
|
3046 sc.source = &src[i].value;
|
122
|
3047 sc.flushes = &conf->flushes;
|
58
|
3048 sc.lengths = &conf->headers_set_len;
|
|
3049 sc.values = &conf->headers_set;
|
|
3050
|
|
3051 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
464
|
3052 return NGX_ERROR;
|
58
|
3053 }
|
|
3054
|
|
3055
|
|
3056 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3057 sizeof(ngx_http_script_copy_code_t));
|
|
3058 if (copy == NULL) {
|
464
|
3059 return NGX_ERROR;
|
58
|
3060 }
|
|
3061
|
|
3062 copy->code = (ngx_http_script_code_pt)
|
|
3063 ngx_http_script_copy_len_code;
|
|
3064 copy->len = sizeof(CRLF) - 1;
|
|
3065
|
|
3066
|
|
3067 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3068 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
3069 & ~(sizeof(uintptr_t) - 1);
|
|
3070
|
|
3071 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3072 if (copy == NULL) {
|
464
|
3073 return NGX_ERROR;
|
58
|
3074 }
|
|
3075
|
|
3076 copy->code = ngx_http_script_copy_code;
|
|
3077 copy->len = sizeof(CRLF) - 1;
|
|
3078
|
|
3079 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3080 *p++ = CR; *p = LF;
|
|
3081 }
|
|
3082
|
|
3083 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3084 if (code == NULL) {
|
464
|
3085 return NGX_ERROR;
|
58
|
3086 }
|
|
3087
|
|
3088 *code = (uintptr_t) NULL;
|
|
3089
|
|
3090 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
3091 if (code == NULL) {
|
464
|
3092 return NGX_ERROR;
|
58
|
3093 }
|
|
3094
|
|
3095 *code = (uintptr_t) NULL;
|
|
3096 }
|
|
3097
|
|
3098 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3099 if (code == NULL) {
|
464
|
3100 return NGX_ERROR;
|
58
|
3101 }
|
|
3102
|
|
3103 *code = (uintptr_t) NULL;
|
|
3104
|
|
3105
|
198
|
3106 hash.hash = &conf->headers_set_hash;
|
|
3107 hash.key = ngx_hash_key_lc;
|
298
|
3108 hash.max_size = conf->headers_hash_max_size;
|
|
3109 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
3110 hash.name = "proxy_headers_hash";
|
198
|
3111 hash.pool = cf->pool;
|
|
3112 hash.temp_pool = NULL;
|
|
3113
|
576
|
3114 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
|
58
|
3115 }
|
|
3116
|
|
3117
|
|
3118 static char *
|
|
3119 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3120 {
|
|
3121 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3122
|
348
|
3123 size_t add;
|
|
3124 u_short port;
|
|
3125 ngx_str_t *value, *url;
|
|
3126 ngx_url_t u;
|
|
3127 ngx_uint_t n;
|
|
3128 ngx_http_core_loc_conf_t *clcf;
|
|
3129 ngx_http_script_compile_t sc;
|
58
|
3130
|
430
|
3131 if (plcf->upstream.upstream || plcf->proxy_lengths) {
|
104
|
3132 return "is duplicate";
|
|
3133 }
|
|
3134
|
348
|
3135 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
3136
|
510
|
3137 clcf->handler = ngx_http_proxy_handler;
|
|
3138
|
|
3139 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
3140 clcf->auto_redirect = 1;
|
|
3141 }
|
|
3142
|
58
|
3143 value = cf->args->elts;
|
|
3144
|
|
3145 url = &value[1];
|
|
3146
|
348
|
3147 n = ngx_http_script_variables_count(url);
|
|
3148
|
|
3149 if (n) {
|
|
3150
|
|
3151 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3152
|
|
3153 sc.cf = cf;
|
|
3154 sc.source = url;
|
|
3155 sc.lengths = &plcf->proxy_lengths;
|
|
3156 sc.values = &plcf->proxy_values;
|
|
3157 sc.variables = n;
|
|
3158 sc.complete_lengths = 1;
|
|
3159 sc.complete_values = 1;
|
354
|
3160
|
348
|
3161 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3162 return NGX_CONF_ERROR;
|
|
3163 }
|
|
3164
|
|
3165 #if (NGX_HTTP_SSL)
|
|
3166 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3167 return NGX_CONF_ERROR;
|
|
3168 }
|
|
3169 #endif
|
|
3170
|
|
3171 return NGX_CONF_OK;
|
|
3172 }
|
|
3173
|
286
|
3174 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
3175 add = 7;
|
140
|
3176 port = 80;
|
126
|
3177
|
286
|
3178 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
3179
|
|
3180 #if (NGX_HTTP_SSL)
|
348
|
3181 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3182 return NGX_CONF_ERROR;
|
|
3183 }
|
126
|
3184
|
|
3185 add = 8;
|
140
|
3186 port = 443;
|
126
|
3187 #else
|
|
3188 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3189 "https protocol requires SSL support");
|
|
3190 return NGX_CONF_ERROR;
|
|
3191 #endif
|
|
3192
|
|
3193 } else {
|
58
|
3194 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
3195 return NGX_CONF_ERROR;
|
|
3196 }
|
|
3197
|
200
|
3198 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3199
|
|
3200 u.url.len = url->len - add;
|
|
3201 u.url.data = url->data + add;
|
266
|
3202 u.default_port = port;
|
|
3203 u.uri_part = 1;
|
260
|
3204 u.no_resolve = 1;
|
|
3205
|
|
3206 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
3207 if (plcf->upstream.upstream == NULL) {
|
58
|
3208 return NGX_CONF_ERROR;
|
|
3209 }
|
|
3210
|
430
|
3211 plcf->vars.schema.len = add;
|
|
3212 plcf->vars.schema.data = url->data;
|
464
|
3213 plcf->vars.key_start = plcf->vars.schema;
|
|
3214
|
|
3215 ngx_http_proxy_set_vars(&u, &plcf->vars);
|
58
|
3216
|
464
|
3217 plcf->location = clcf->name;
|
|
3218
|
324
|
3219 if (clcf->named
|
58
|
3220 #if (NGX_PCRE)
|
324
|
3221 || clcf->regex
|
|
3222 #endif
|
|
3223 || clcf->noname)
|
|
3224 {
|
348
|
3225 if (plcf->vars.uri.len) {
|
122
|
3226 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3227 "\"proxy_pass\" may not have URI part in "
|
256
|
3228 "location given by regular expression, "
|
324
|
3229 "or inside named location, "
|
256
|
3230 "or inside the \"if\" statement, "
|
|
3231 "or inside the \"limit_except\" block");
|
122
|
3232 return NGX_CONF_ERROR;
|
|
3233 }
|
|
3234
|
348
|
3235 plcf->location.len = 0;
|
104
|
3236 }
|
|
3237
|
348
|
3238 plcf->url = *url;
|
58
|
3239
|
|
3240 return NGX_CONF_OK;
|
|
3241 }
|
|
3242
|
|
3243
|
|
3244 static char *
|
|
3245 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3246 {
|
|
3247 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3248
|
614
|
3249 u_char *p;
|
58
|
3250 ngx_str_t *value;
|
|
3251 ngx_array_t *vars_lengths, *vars_values;
|
|
3252 ngx_http_script_compile_t sc;
|
|
3253 ngx_http_proxy_redirect_t *pr;
|
|
3254
|
|
3255 if (plcf->redirect == 0) {
|
|
3256 return NGX_CONF_OK;
|
|
3257 }
|
|
3258
|
|
3259 value = cf->args->elts;
|
|
3260
|
484
|
3261 if (cf->args->nelts == 2) {
|
|
3262 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3263 plcf->redirect = 0;
|
|
3264 plcf->redirects = NULL;
|
|
3265 return NGX_CONF_OK;
|
|
3266 }
|
|
3267
|
|
3268 if (ngx_strcmp(value[1].data, "false") == 0) {
|
|
3269 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
3270 "invalid parameter \"false\", use \"off\" instead");
|
|
3271 plcf->redirect = 0;
|
|
3272 plcf->redirects = NULL;
|
|
3273 return NGX_CONF_OK;
|
|
3274 }
|
|
3275
|
|
3276 if (ngx_strcmp(value[1].data, "default") != 0) {
|
|
3277 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3278 "invalid parameter \"%V\"", &value[1]);
|
|
3279 return NGX_CONF_ERROR;
|
|
3280 }
|
58
|
3281 }
|
|
3282
|
|
3283 if (plcf->redirects == NULL) {
|
|
3284 plcf->redirects = ngx_array_create(cf->pool, 1,
|
|
3285 sizeof(ngx_http_proxy_redirect_t));
|
|
3286 if (plcf->redirects == NULL) {
|
|
3287 return NGX_CONF_ERROR;
|
|
3288 }
|
|
3289 }
|
|
3290
|
|
3291 pr = ngx_array_push(plcf->redirects);
|
|
3292 if (pr == NULL) {
|
|
3293 return NGX_CONF_ERROR;
|
|
3294 }
|
|
3295
|
484
|
3296 if (ngx_strcmp(value[1].data, "default") == 0) {
|
566
|
3297 if (plcf->proxy_lengths) {
|
|
3298 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3299 "\"proxy_redirect default\" may not be used "
|
|
3300 "with \"proxy_pass\" directive with variables");
|
|
3301 return NGX_CONF_ERROR;
|
|
3302 }
|
|
3303
|
348
|
3304 if (plcf->url.data == NULL) {
|
58
|
3305 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
566
|
3306 "\"proxy_redirect default\" must go "
|
58
|
3307 "after the \"proxy_pass\" directive");
|
|
3308 return NGX_CONF_ERROR;
|
|
3309 }
|
|
3310
|
|
3311 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
3312
|
|
3313 if (plcf->vars.uri.len) {
|
614
|
3314 pr->redirect = plcf->url;
|
348
|
3315 pr->replacement.text = plcf->location;
|
202
|
3316
|
|
3317 } else {
|
614
|
3318 pr->redirect.len = plcf->url.len + sizeof("/") - 1;
|
|
3319
|
|
3320 p = ngx_pnalloc(cf->pool, pr->redirect.len);
|
|
3321 if (p == NULL) {
|
|
3322 return NGX_CONF_ERROR;
|
|
3323 }
|
|
3324
|
|
3325 pr->redirect.data = p;
|
|
3326
|
|
3327 p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
|
|
3328 *p = '/';
|
|
3329
|
|
3330 ngx_str_set(&pr->replacement.text, "/");
|
202
|
3331 }
|
58
|
3332
|
|
3333 return NGX_CONF_OK;
|
|
3334 }
|
|
3335
|
|
3336 if (ngx_http_script_variables_count(&value[2]) == 0) {
|
|
3337 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
|
3338 pr->redirect = value[1];
|
|
3339 pr->replacement.text = value[2];
|
|
3340
|
|
3341 return NGX_CONF_OK;
|
|
3342 }
|
|
3343
|
|
3344 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3345
|
|
3346 vars_lengths = NULL;
|
|
3347 vars_values = NULL;
|
|
3348
|
|
3349 sc.cf = cf;
|
|
3350 sc.source = &value[2];
|
|
3351 sc.lengths = &vars_lengths;
|
|
3352 sc.values = &vars_values;
|
|
3353 sc.complete_lengths = 1;
|
|
3354 sc.complete_values = 1;
|
|
3355
|
|
3356 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3357 return NGX_CONF_ERROR;
|
|
3358 }
|
|
3359
|
|
3360 pr->handler = ngx_http_proxy_rewrite_redirect_vars;
|
|
3361 pr->redirect = value[1];
|
|
3362 pr->replacement.vars.lengths = vars_lengths->elts;
|
|
3363 pr->replacement.vars.values = vars_values->elts;
|
|
3364
|
|
3365 return NGX_CONF_OK;
|
|
3366 }
|
|
3367
|
|
3368
|
|
3369 static char *
|
320
|
3370 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3371 {
|
|
3372 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3373
|
|
3374 ngx_str_t *value;
|
|
3375 ngx_http_script_compile_t sc;
|
|
3376
|
496
|
3377 if (plcf->upstream.store != NGX_CONF_UNSET
|
|
3378 || plcf->upstream.store_lengths)
|
320
|
3379 {
|
|
3380 return "is duplicate";
|
|
3381 }
|
|
3382
|
|
3383 value = cf->args->elts;
|
|
3384
|
496
|
3385 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3386 plcf->upstream.store = 0;
|
320
|
3387 return NGX_CONF_OK;
|
|
3388 }
|
|
3389
|
496
|
3390 #if (NGX_HTTP_CACHE)
|
|
3391
|
|
3392 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
|
|
3393 && plcf->upstream.cache != NULL)
|
|
3394 {
|
|
3395 return "is incompatible with \"proxy_cache\"";
|
|
3396 }
|
|
3397
|
|
3398 #endif
|
|
3399
|
|
3400 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
3401 plcf->upstream.store = 1;
|
320
|
3402 return NGX_CONF_OK;
|
|
3403 }
|
|
3404
|
|
3405 /* include the terminating '\0' into script */
|
|
3406 value[1].len++;
|
|
3407
|
|
3408 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3409
|
|
3410 sc.cf = cf;
|
|
3411 sc.source = &value[1];
|
|
3412 sc.lengths = &plcf->upstream.store_lengths;
|
|
3413 sc.values = &plcf->upstream.store_values;
|
340
|
3414 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
3415 sc.complete_lengths = 1;
|
|
3416 sc.complete_values = 1;
|
|
3417
|
|
3418 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3419 return NGX_CONF_ERROR;
|
|
3420 }
|
|
3421
|
|
3422 return NGX_CONF_OK;
|
|
3423 }
|
|
3424
|
|
3425
|
464
|
3426 #if (NGX_HTTP_CACHE)
|
|
3427
|
|
3428 static char *
|
|
3429 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3430 {
|
|
3431 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3432
|
|
3433 ngx_str_t *value;
|
|
3434
|
|
3435 value = cf->args->elts;
|
|
3436
|
|
3437 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
|
|
3438 return "is duplicate";
|
|
3439 }
|
|
3440
|
|
3441 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3442 plcf->upstream.cache = NULL;
|
|
3443 return NGX_CONF_OK;
|
|
3444 }
|
|
3445
|
496
|
3446 if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
|
|
3447 return "is incompatible with \"proxy_store\"";
|
|
3448 }
|
|
3449
|
464
|
3450 plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
|
|
3451 &ngx_http_proxy_module);
|
|
3452 if (plcf->upstream.cache == NULL) {
|
|
3453 return NGX_CONF_ERROR;
|
|
3454 }
|
|
3455
|
|
3456 return NGX_CONF_OK;
|
|
3457 }
|
|
3458
|
472
|
3459
|
|
3460 static char *
|
|
3461 ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3462 {
|
|
3463 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3464
|
|
3465 ngx_str_t *value;
|
|
3466 ngx_http_compile_complex_value_t ccv;
|
|
3467
|
|
3468 value = cf->args->elts;
|
|
3469
|
|
3470 if (plcf->cache_key.value.len) {
|
|
3471 return "is duplicate";
|
|
3472 }
|
|
3473
|
|
3474 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3475
|
|
3476 ccv.cf = cf;
|
|
3477 ccv.value = &value[1];
|
|
3478 ccv.complex_value = &plcf->cache_key;
|
|
3479
|
|
3480 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3481 return NGX_CONF_ERROR;
|
|
3482 }
|
|
3483
|
|
3484 return NGX_CONF_OK;
|
|
3485 }
|
|
3486
|
464
|
3487 #endif
|
|
3488
|
|
3489
|
320
|
3490 static char *
|
58
|
3491 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
3492 {
|
|
3493 #if (NGX_FREEBSD)
|
|
3494 ssize_t *np = data;
|
|
3495
|
222
|
3496 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
3497 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3498 "\"proxy_send_lowat\" must be less than %d "
|
|
3499 "(sysctl net.inet.tcp.sendspace)",
|
|
3500 ngx_freebsd_net_inet_tcp_sendspace);
|
|
3501
|
|
3502 return NGX_CONF_ERROR;
|
|
3503 }
|
|
3504
|
|
3505 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
3506 ssize_t *np = data;
|
|
3507
|
|
3508 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
3509 "\"proxy_send_lowat\" is not supported, ignored");
|
|
3510
|
|
3511 *np = 0;
|
|
3512
|
|
3513 #endif
|
|
3514
|
|
3515 return NGX_CONF_OK;
|
|
3516 }
|
260
|
3517
|
|
3518
|
348
|
3519 #if (NGX_HTTP_SSL)
|
|
3520
|
|
3521 static ngx_int_t
|
|
3522 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
3523 {
|
|
3524 ngx_pool_cleanup_t *cln;
|
|
3525
|
|
3526 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
3527 if (plcf->upstream.ssl == NULL) {
|
|
3528 return NGX_ERROR;
|
|
3529 }
|
|
3530
|
|
3531 plcf->upstream.ssl->log = cf->log;
|
|
3532
|
|
3533 if (ngx_ssl_create(plcf->upstream.ssl,
|
|
3534 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL)
|
|
3535 != NGX_OK)
|
|
3536 {
|
|
3537 return NGX_ERROR;
|
|
3538 }
|
|
3539
|
|
3540 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
3541 if (cln == NULL) {
|
|
3542 return NGX_ERROR;
|
|
3543 }
|
|
3544
|
|
3545 cln->handler = ngx_ssl_cleanup_ctx;
|
|
3546 cln->data = plcf->upstream.ssl;
|
|
3547
|
|
3548 return NGX_OK;
|
|
3549 }
|
|
3550
|
|
3551 #endif
|
|
3552
|
|
3553
|
464
|
3554 static void
|
|
3555 ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
|
348
|
3556 {
|
400
|
3557 if (u->family != AF_UNIX) {
|
464
|
3558
|
348
|
3559 if (u->no_port || u->port == u->default_port) {
|
464
|
3560
|
348
|
3561 v->host_header = u->host;
|
|
3562
|
|
3563 if (u->default_port == 80) {
|
570
|
3564 ngx_str_set(&v->port, "80");
|
348
|
3565
|
|
3566 } else {
|
570
|
3567 ngx_str_set(&v->port, "443");
|
348
|
3568 }
|
|
3569
|
|
3570 } else {
|
354
|
3571 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
3572 v->host_header.data = u->host.data;
|
|
3573 v->port = u->port_text;
|
348
|
3574 }
|
|
3575
|
464
|
3576 v->key_start.len += v->host_header.len;
|
|
3577
|
348
|
3578 } else {
|
570
|
3579 ngx_str_set(&v->host_header, "localhost");
|
|
3580 ngx_str_null(&v->port);
|
464
|
3581 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
|
348
|
3582 }
|
|
3583
|
|
3584 v->uri = u->uri;
|
|
3585 }
|