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