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
|
666
|
1384 ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
|
|
1385 h->key.data[h->key.len] = '\0';
|
|
1386 ngx_memcpy(h->value.data, r->header_start, h->value.len);
|
|
1387 h->value.data[h->value.len] = '\0';
|
58
|
1388
|
198
|
1389 if (h->key.len == r->lowcase_index) {
|
|
1390 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1391
|
|
1392 } else {
|
392
|
1393 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
58
|
1394 }
|
|
1395
|
198
|
1396 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
|
|
1397 h->lowcase_key, h->key.len);
|
|
1398
|
|
1399 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
356
|
1400 return NGX_ERROR;
|
198
|
1401 }
|
|
1402
|
58
|
1403 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1404 "http proxy header: \"%V: %V\"",
|
|
1405 &h->key, &h->value);
|
|
1406
|
|
1407 continue;
|
|
1408 }
|
|
1409
|
|
1410 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1411
|
|
1412 /* a whole header has been parsed successfully */
|
|
1413
|
|
1414 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1415 "http proxy header done");
|
|
1416
|
198
|
1417 /*
|
|
1418 * if no "Server" and "Date" in header line,
|
|
1419 * then add the special empty headers
|
|
1420 */
|
|
1421
|
|
1422 if (r->upstream->headers_in.server == NULL) {
|
|
1423 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1424 if (h == NULL) {
|
356
|
1425 return NGX_ERROR;
|
198
|
1426 }
|
|
1427
|
|
1428 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
|
|
1429 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
|
|
1430
|
570
|
1431 ngx_str_set(&h->key, "Server");
|
|
1432 ngx_str_null(&h->value);
|
198
|
1433 h->lowcase_key = (u_char *) "server";
|
|
1434 }
|
|
1435
|
|
1436 if (r->upstream->headers_in.date == NULL) {
|
|
1437 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1438 if (h == NULL) {
|
356
|
1439 return NGX_ERROR;
|
198
|
1440 }
|
|
1441
|
|
1442 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
|
|
1443
|
570
|
1444 ngx_str_set(&h->key, "Date");
|
|
1445 ngx_str_null(&h->value);
|
198
|
1446 h->lowcase_key = (u_char *) "date";
|
|
1447 }
|
|
1448
|
640
|
1449 /* clear content length if response is chunked */
|
|
1450
|
|
1451 u = r->upstream;
|
|
1452
|
|
1453 if (u->headers_in.chunked) {
|
|
1454 u->headers_in.content_length_n = -1;
|
|
1455 }
|
|
1456
|
|
1457 /*
|
|
1458 * set u->keepalive if response has no body; this allows to keep
|
|
1459 * connections alive in case of r->header_only or X-Accel-Redirect
|
|
1460 */
|
|
1461
|
|
1462 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1463
|
|
1464 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1465 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1466 || ctx->head
|
|
1467 || (!u->headers_in.chunked
|
|
1468 && u->headers_in.content_length_n == 0))
|
|
1469 {
|
|
1470 u->keepalive = !u->headers_in.connection_close;
|
|
1471 }
|
|
1472
|
58
|
1473 return NGX_OK;
|
|
1474 }
|
|
1475
|
70
|
1476 if (rc == NGX_AGAIN) {
|
|
1477 return NGX_AGAIN;
|
|
1478 }
|
|
1479
|
58
|
1480 /* there was error while a header line parsing */
|
|
1481
|
|
1482 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
168
|
1483 "upstream sent invalid header");
|
58
|
1484
|
|
1485 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1486 }
|
|
1487 }
|
|
1488
|
|
1489
|
640
|
1490 static ngx_int_t
|
|
1491 ngx_http_proxy_input_filter_init(void *data)
|
|
1492 {
|
|
1493 ngx_http_request_t *r = data;
|
|
1494 ngx_http_upstream_t *u;
|
|
1495 ngx_http_proxy_ctx_t *ctx;
|
|
1496
|
|
1497 u = r->upstream;
|
|
1498 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1499
|
|
1500 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1501 "http proxy filter init s:%d h:%d c:%d l:%O",
|
|
1502 u->headers_in.status_n, ctx->head, u->headers_in.chunked,
|
|
1503 u->headers_in.content_length_n);
|
|
1504
|
|
1505 /* as per RFC2616, 4.4 Message Length */
|
|
1506
|
|
1507 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1508 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1509 || ctx->head)
|
|
1510 {
|
|
1511 /* 1xx, 204, and 304 and replies to HEAD requests */
|
|
1512 /* no 1xx since we don't send Expect and Upgrade */
|
|
1513
|
|
1514 u->pipe->length = 0;
|
|
1515 u->length = 0;
|
|
1516 u->keepalive = !u->headers_in.connection_close;
|
|
1517
|
|
1518 } else if (u->headers_in.chunked) {
|
|
1519 /* chunked */
|
|
1520
|
|
1521 u->pipe->input_filter = ngx_http_proxy_chunked_filter;
|
|
1522 u->pipe->length = 3; /* "0" LF LF */
|
|
1523
|
|
1524 u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
|
|
1525 u->length = -1;
|
|
1526
|
|
1527 } else if (u->headers_in.content_length_n == 0) {
|
|
1528 /* empty body: special case as filter won't be called */
|
|
1529
|
|
1530 u->pipe->length = 0;
|
|
1531 u->length = 0;
|
|
1532 u->keepalive = !u->headers_in.connection_close;
|
|
1533
|
|
1534 } else {
|
|
1535 /* content length or connection close */
|
|
1536
|
|
1537 u->pipe->length = u->headers_in.content_length_n;
|
|
1538 u->length = u->headers_in.content_length_n;
|
|
1539 }
|
|
1540
|
|
1541 return NGX_OK;
|
|
1542 }
|
|
1543
|
|
1544
|
|
1545 static ngx_int_t
|
|
1546 ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1547 {
|
|
1548 ngx_buf_t *b;
|
|
1549 ngx_chain_t *cl;
|
|
1550 ngx_http_request_t *r;
|
|
1551
|
|
1552 if (buf->pos == buf->last) {
|
|
1553 return NGX_OK;
|
|
1554 }
|
|
1555
|
|
1556 if (p->free) {
|
|
1557 cl = p->free;
|
|
1558 b = cl->buf;
|
|
1559 p->free = cl->next;
|
|
1560 ngx_free_chain(p->pool, cl);
|
|
1561
|
|
1562 } else {
|
|
1563 b = ngx_alloc_buf(p->pool);
|
|
1564 if (b == NULL) {
|
|
1565 return NGX_ERROR;
|
|
1566 }
|
|
1567 }
|
|
1568
|
|
1569 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
1570 b->shadow = buf;
|
|
1571 b->tag = p->tag;
|
|
1572 b->last_shadow = 1;
|
|
1573 b->recycled = 1;
|
|
1574 buf->shadow = b;
|
|
1575
|
|
1576 cl = ngx_alloc_chain_link(p->pool);
|
|
1577 if (cl == NULL) {
|
|
1578 return NGX_ERROR;
|
|
1579 }
|
|
1580
|
|
1581 cl->buf = b;
|
|
1582 cl->next = NULL;
|
|
1583
|
|
1584 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
|
|
1585
|
|
1586 if (p->in) {
|
|
1587 *p->last_in = cl;
|
|
1588 } else {
|
|
1589 p->in = cl;
|
|
1590 }
|
|
1591 p->last_in = &cl->next;
|
|
1592
|
|
1593 if (p->length == -1) {
|
|
1594 return NGX_OK;
|
|
1595 }
|
|
1596
|
|
1597 p->length -= b->last - b->pos;
|
|
1598
|
|
1599 if (p->length == 0) {
|
|
1600 r = p->input_ctx;
|
|
1601 p->upstream_done = 1;
|
|
1602 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1603
|
|
1604 } else if (p->length < 0) {
|
|
1605 r = p->input_ctx;
|
|
1606 p->upstream_done = 1;
|
|
1607
|
642
|
1608 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
644
|
1609 "upstream sent too much data");
|
640
|
1610 }
|
|
1611
|
|
1612 return NGX_OK;
|
|
1613 }
|
|
1614
|
|
1615
|
|
1616 static ngx_inline ngx_int_t
|
|
1617 ngx_http_proxy_parse_chunked(ngx_http_request_t *r, ngx_buf_t *buf)
|
|
1618 {
|
|
1619 u_char *pos, ch, c;
|
|
1620 ngx_int_t rc;
|
|
1621 ngx_http_proxy_ctx_t *ctx;
|
|
1622 enum {
|
|
1623 sw_chunk_start = 0,
|
|
1624 sw_chunk_size,
|
|
1625 sw_chunk_extension,
|
|
1626 sw_chunk_extension_almost_done,
|
|
1627 sw_chunk_data,
|
|
1628 sw_after_data,
|
|
1629 sw_after_data_almost_done,
|
|
1630 sw_last_chunk_extension,
|
|
1631 sw_last_chunk_extension_almost_done,
|
|
1632 sw_trailer,
|
|
1633 sw_trailer_almost_done,
|
|
1634 sw_trailer_header,
|
|
1635 sw_trailer_header_almost_done
|
|
1636 } state;
|
|
1637
|
|
1638 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1639 state = ctx->state;
|
|
1640
|
|
1641 if (state == sw_chunk_data && ctx->size == 0) {
|
|
1642 state = sw_after_data;
|
|
1643 }
|
|
1644
|
|
1645 rc = NGX_AGAIN;
|
|
1646
|
|
1647 for (pos = buf->pos; pos < buf->last; pos++) {
|
|
1648
|
|
1649 ch = *pos;
|
|
1650
|
|
1651 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1652 "http proxy chunked byte: %02Xd s:%d", ch, state);
|
|
1653
|
|
1654 switch (state) {
|
|
1655
|
|
1656 case sw_chunk_start:
|
|
1657 if (ch >= '0' && ch <= '9') {
|
|
1658 state = sw_chunk_size;
|
|
1659 ctx->size = ch - '0';
|
|
1660 break;
|
|
1661 }
|
|
1662
|
|
1663 c = (u_char) (ch | 0x20);
|
|
1664
|
|
1665 if (c >= 'a' && c <= 'f') {
|
|
1666 state = sw_chunk_size;
|
|
1667 ctx->size = c - 'a' + 10;
|
|
1668 break;
|
|
1669 }
|
|
1670
|
|
1671 goto invalid;
|
|
1672
|
|
1673 case sw_chunk_size:
|
|
1674 if (ch >= '0' && ch <= '9') {
|
|
1675 ctx->size = ctx->size * 16 + (ch - '0');
|
|
1676 break;
|
|
1677 }
|
|
1678
|
|
1679 c = (u_char) (ch | 0x20);
|
|
1680
|
|
1681 if (c >= 'a' && c <= 'f') {
|
|
1682 ctx->size = ctx->size * 16 + (c - 'a' + 10);
|
|
1683 break;
|
|
1684 }
|
|
1685
|
|
1686 if (ctx->size == 0) {
|
|
1687
|
|
1688 switch (ch) {
|
|
1689 case CR:
|
|
1690 state = sw_last_chunk_extension_almost_done;
|
|
1691 break;
|
|
1692 case LF:
|
|
1693 state = sw_trailer;
|
|
1694 break;
|
|
1695 case ';':
|
642
|
1696 case ' ':
|
|
1697 case '\t':
|
640
|
1698 state = sw_last_chunk_extension;
|
|
1699 break;
|
|
1700 default:
|
|
1701 goto invalid;
|
|
1702 }
|
|
1703
|
|
1704 break;
|
|
1705 }
|
|
1706
|
|
1707 switch (ch) {
|
|
1708 case CR:
|
|
1709 state = sw_chunk_extension_almost_done;
|
|
1710 break;
|
|
1711 case LF:
|
|
1712 state = sw_chunk_data;
|
|
1713 break;
|
|
1714 case ';':
|
642
|
1715 case ' ':
|
|
1716 case '\t':
|
640
|
1717 state = sw_chunk_extension;
|
|
1718 break;
|
|
1719 default:
|
|
1720 goto invalid;
|
|
1721 }
|
|
1722
|
|
1723 break;
|
|
1724
|
|
1725 case sw_chunk_extension:
|
|
1726 switch (ch) {
|
|
1727 case CR:
|
|
1728 state = sw_chunk_extension_almost_done;
|
|
1729 break;
|
|
1730 case LF:
|
|
1731 state = sw_chunk_data;
|
|
1732 }
|
|
1733 break;
|
|
1734
|
|
1735 case sw_chunk_extension_almost_done:
|
|
1736 if (ch == LF) {
|
|
1737 state = sw_chunk_data;
|
|
1738 break;
|
|
1739 }
|
|
1740 goto invalid;
|
|
1741
|
|
1742 case sw_chunk_data:
|
|
1743 rc = NGX_OK;
|
|
1744 goto data;
|
|
1745
|
|
1746 case sw_after_data:
|
|
1747 switch (ch) {
|
|
1748 case CR:
|
|
1749 state = sw_after_data_almost_done;
|
|
1750 break;
|
|
1751 case LF:
|
|
1752 state = sw_chunk_start;
|
|
1753 }
|
|
1754 break;
|
|
1755
|
|
1756 case sw_after_data_almost_done:
|
|
1757 if (ch == LF) {
|
|
1758 state = sw_chunk_start;
|
|
1759 break;
|
|
1760 }
|
|
1761 goto invalid;
|
|
1762
|
|
1763 case sw_last_chunk_extension:
|
|
1764 switch (ch) {
|
|
1765 case CR:
|
|
1766 state = sw_last_chunk_extension_almost_done;
|
|
1767 break;
|
|
1768 case LF:
|
|
1769 state = sw_trailer;
|
|
1770 }
|
|
1771 break;
|
|
1772
|
|
1773 case sw_last_chunk_extension_almost_done:
|
|
1774 if (ch == LF) {
|
|
1775 state = sw_trailer;
|
|
1776 break;
|
|
1777 }
|
|
1778 goto invalid;
|
|
1779
|
|
1780 case sw_trailer:
|
|
1781 switch (ch) {
|
|
1782 case CR:
|
|
1783 state = sw_trailer_almost_done;
|
|
1784 break;
|
|
1785 case LF:
|
|
1786 goto done;
|
|
1787 default:
|
|
1788 state = sw_trailer_header;
|
|
1789 }
|
|
1790 break;
|
|
1791
|
|
1792 case sw_trailer_almost_done:
|
|
1793 if (ch == LF) {
|
|
1794 goto done;
|
|
1795 }
|
|
1796 goto invalid;
|
|
1797
|
|
1798 case sw_trailer_header:
|
|
1799 switch (ch) {
|
|
1800 case CR:
|
|
1801 state = sw_trailer_header_almost_done;
|
|
1802 break;
|
|
1803 case LF:
|
|
1804 state = sw_trailer;
|
|
1805 }
|
|
1806 break;
|
|
1807
|
|
1808 case sw_trailer_header_almost_done:
|
|
1809 if (ch == LF) {
|
|
1810 state = sw_trailer;
|
|
1811 break;
|
|
1812 }
|
|
1813 goto invalid;
|
|
1814
|
|
1815 }
|
|
1816 }
|
|
1817
|
|
1818 data:
|
|
1819
|
|
1820 ctx->state = state;
|
|
1821 buf->pos = pos;
|
|
1822
|
|
1823 switch (state) {
|
|
1824
|
|
1825 case sw_chunk_start:
|
|
1826 ctx->length = 3 /* "0" LF LF */;
|
|
1827 break;
|
|
1828 case sw_chunk_size:
|
|
1829 ctx->length = 2 /* LF LF */
|
|
1830 + (ctx->size ? ctx->size + 4 /* LF "0" LF LF */ : 0);
|
|
1831 break;
|
|
1832 case sw_chunk_extension:
|
|
1833 case sw_chunk_extension_almost_done:
|
|
1834 ctx->length = 1 /* LF */ + ctx->size + 4 /* LF "0" LF LF */;
|
|
1835 break;
|
|
1836 case sw_chunk_data:
|
|
1837 ctx->length = ctx->size + 4 /* LF "0" LF LF */;
|
|
1838 break;
|
|
1839 case sw_after_data:
|
|
1840 case sw_after_data_almost_done:
|
|
1841 ctx->length = 4 /* LF "0" LF LF */;
|
|
1842 break;
|
|
1843 case sw_last_chunk_extension:
|
|
1844 case sw_last_chunk_extension_almost_done:
|
|
1845 ctx->length = 2 /* LF LF */;
|
|
1846 break;
|
|
1847 case sw_trailer:
|
|
1848 case sw_trailer_almost_done:
|
|
1849 ctx->length = 1 /* LF */;
|
|
1850 break;
|
|
1851 case sw_trailer_header:
|
|
1852 case sw_trailer_header_almost_done:
|
|
1853 ctx->length = 2 /* LF LF */;
|
|
1854 break;
|
|
1855
|
|
1856 }
|
|
1857
|
|
1858 return rc;
|
|
1859
|
|
1860 done:
|
|
1861
|
|
1862 return NGX_DONE;
|
|
1863
|
|
1864 invalid:
|
|
1865
|
|
1866 return NGX_ERROR;
|
|
1867 }
|
|
1868
|
|
1869
|
|
1870 static ngx_int_t
|
|
1871 ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1872 {
|
|
1873 ngx_int_t rc;
|
|
1874 ngx_buf_t *b, **prev;
|
|
1875 ngx_chain_t *cl;
|
|
1876 ngx_http_request_t *r;
|
|
1877 ngx_http_proxy_ctx_t *ctx;
|
|
1878
|
|
1879 if (buf->pos == buf->last) {
|
|
1880 return NGX_OK;
|
|
1881 }
|
|
1882
|
|
1883 r = p->input_ctx;
|
|
1884 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1885
|
|
1886 b = NULL;
|
|
1887 prev = &buf->shadow;
|
|
1888
|
|
1889 for ( ;; ) {
|
|
1890
|
|
1891 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
1892
|
|
1893 if (rc == NGX_OK) {
|
|
1894
|
|
1895 /* a chunk has been parsed successfully */
|
|
1896
|
|
1897 if (p->free) {
|
|
1898 cl = p->free;
|
|
1899 b = cl->buf;
|
|
1900 p->free = cl->next;
|
|
1901 ngx_free_chain(p->pool, cl);
|
|
1902
|
|
1903 } else {
|
|
1904 b = ngx_alloc_buf(p->pool);
|
|
1905 if (b == NULL) {
|
|
1906 return NGX_ERROR;
|
|
1907 }
|
|
1908 }
|
|
1909
|
|
1910 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
1911
|
|
1912 b->pos = buf->pos;
|
|
1913 b->start = buf->start;
|
|
1914 b->end = buf->end;
|
|
1915 b->tag = p->tag;
|
|
1916 b->temporary = 1;
|
|
1917 b->recycled = 1;
|
|
1918
|
|
1919 *prev = b;
|
|
1920 prev = &b->shadow;
|
|
1921
|
|
1922 cl = ngx_alloc_chain_link(p->pool);
|
|
1923 if (cl == NULL) {
|
|
1924 return NGX_ERROR;
|
|
1925 }
|
|
1926
|
|
1927 cl->buf = b;
|
|
1928 cl->next = NULL;
|
|
1929
|
|
1930 if (p->in) {
|
|
1931 *p->last_in = cl;
|
|
1932 } else {
|
|
1933 p->in = cl;
|
|
1934 }
|
|
1935 p->last_in = &cl->next;
|
|
1936
|
|
1937 /* STUB */ b->num = buf->num;
|
|
1938
|
|
1939 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1940 "input buf #%d %p", b->num, b->pos);
|
|
1941
|
|
1942 if (buf->last - buf->pos >= ctx->size) {
|
|
1943
|
|
1944 buf->pos += ctx->size;
|
|
1945 b->last = buf->pos;
|
|
1946 ctx->size = 0;
|
|
1947
|
|
1948 continue;
|
|
1949 }
|
|
1950
|
|
1951 ctx->size -= buf->last - buf->pos;
|
|
1952 buf->pos = buf->last;
|
|
1953 b->last = buf->last;
|
|
1954
|
|
1955 continue;
|
|
1956 }
|
|
1957
|
|
1958 if (rc == NGX_DONE) {
|
|
1959
|
|
1960 /* a whole response has been parsed successfully */
|
|
1961
|
|
1962 p->upstream_done = 1;
|
|
1963 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1964
|
|
1965 break;
|
|
1966 }
|
|
1967
|
|
1968 if (rc == NGX_AGAIN) {
|
|
1969
|
|
1970 /* set p->length, minimal amount of data we want to see */
|
|
1971
|
|
1972 p->length = ctx->length;
|
|
1973
|
|
1974 break;
|
|
1975 }
|
|
1976
|
|
1977 /* invalid response */
|
|
1978
|
642
|
1979 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
1980 "upstream sent invalid chunked response");
|
|
1981
|
|
1982 return NGX_ERROR;
|
|
1983 }
|
|
1984
|
|
1985 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1986 "http proxy chunked state %d, length %d",
|
|
1987 ctx->state, p->length);
|
|
1988
|
|
1989 if (b) {
|
|
1990 b->shadow = buf;
|
|
1991 b->last_shadow = 1;
|
|
1992
|
|
1993 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1994 "input buf %p %z", b->pos, b->last - b->pos);
|
|
1995
|
|
1996 return NGX_OK;
|
|
1997 }
|
|
1998
|
|
1999 /* there is no data record in the buf, add it to free chain */
|
|
2000
|
|
2001 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
|
|
2002 return NGX_ERROR;
|
|
2003 }
|
|
2004
|
|
2005 return NGX_OK;
|
|
2006 }
|
|
2007
|
|
2008
|
|
2009 static ngx_int_t
|
|
2010 ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
|
|
2011 {
|
|
2012 ngx_http_request_t *r = data;
|
|
2013
|
|
2014 ngx_buf_t *b;
|
|
2015 ngx_chain_t *cl, **ll;
|
|
2016 ngx_http_upstream_t *u;
|
|
2017
|
|
2018 u = r->upstream;
|
|
2019
|
|
2020 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
2021 ll = &cl->next;
|
|
2022 }
|
|
2023
|
|
2024 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
2025 if (cl == NULL) {
|
|
2026 return NGX_ERROR;
|
|
2027 }
|
|
2028
|
|
2029 *ll = cl;
|
|
2030
|
|
2031 cl->buf->flush = 1;
|
|
2032 cl->buf->memory = 1;
|
|
2033
|
|
2034 b = &u->buffer;
|
|
2035
|
|
2036 cl->buf->pos = b->last;
|
|
2037 b->last += bytes;
|
|
2038 cl->buf->last = b->last;
|
|
2039 cl->buf->tag = u->output.tag;
|
|
2040
|
|
2041 if (u->length == -1) {
|
|
2042 return NGX_OK;
|
|
2043 }
|
|
2044
|
|
2045 u->length -= bytes;
|
|
2046
|
|
2047 if (u->length == 0) {
|
|
2048 u->keepalive = !u->headers_in.connection_close;
|
|
2049 }
|
|
2050
|
|
2051 return NGX_OK;
|
|
2052 }
|
|
2053
|
|
2054
|
|
2055 static ngx_int_t
|
|
2056 ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
|
|
2057 {
|
|
2058 ngx_http_request_t *r = data;
|
|
2059
|
|
2060 ngx_int_t rc;
|
|
2061 ngx_buf_t *b, *buf;
|
|
2062 ngx_chain_t *cl, **ll;
|
|
2063 ngx_http_upstream_t *u;
|
|
2064 ngx_http_proxy_ctx_t *ctx;
|
|
2065
|
|
2066 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2067 u = r->upstream;
|
|
2068 buf = &u->buffer;
|
|
2069
|
|
2070 buf->pos = buf->last;
|
|
2071 buf->last += bytes;
|
|
2072
|
|
2073 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
2074 ll = &cl->next;
|
|
2075 }
|
|
2076
|
|
2077 for ( ;; ) {
|
|
2078
|
|
2079 rc = ngx_http_proxy_parse_chunked(r, buf);
|
|
2080
|
|
2081 if (rc == NGX_OK) {
|
|
2082
|
|
2083 /* a chunk has been parsed successfully */
|
|
2084
|
|
2085 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
2086 if (cl == NULL) {
|
|
2087 return NGX_ERROR;
|
|
2088 }
|
|
2089
|
|
2090 *ll = cl;
|
|
2091 ll = &cl->next;
|
|
2092
|
|
2093 b = cl->buf;
|
|
2094
|
|
2095 b->flush = 1;
|
|
2096 b->memory = 1;
|
|
2097
|
|
2098 b->pos = buf->pos;
|
|
2099 b->tag = u->output.tag;
|
|
2100
|
|
2101 if (buf->last - buf->pos >= ctx->size) {
|
|
2102 buf->pos += ctx->size;
|
|
2103 b->last = buf->pos;
|
|
2104 ctx->size = 0;
|
|
2105
|
|
2106 } else {
|
|
2107 ctx->size -= buf->last - buf->pos;
|
|
2108 buf->pos = buf->last;
|
|
2109 b->last = buf->last;
|
|
2110 }
|
|
2111
|
|
2112 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2113 "http proxy out buf %p %z",
|
|
2114 b->pos, b->last - b->pos);
|
|
2115
|
|
2116 continue;
|
|
2117 }
|
|
2118
|
|
2119 if (rc == NGX_DONE) {
|
|
2120
|
|
2121 /* a whole response has been parsed successfully */
|
|
2122
|
|
2123 u->keepalive = !u->headers_in.connection_close;
|
|
2124 u->length = 0;
|
|
2125
|
|
2126 break;
|
|
2127 }
|
|
2128
|
|
2129 if (rc == NGX_AGAIN) {
|
|
2130 break;
|
|
2131 }
|
|
2132
|
|
2133 /* invalid response */
|
|
2134
|
642
|
2135 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
2136 "upstream sent invalid chunked response");
|
|
2137
|
|
2138 return NGX_ERROR;
|
|
2139 }
|
|
2140
|
|
2141 /* provide continuous buffer for subrequests in memory */
|
|
2142
|
|
2143 if (r->subrequest_in_memory) {
|
|
2144
|
|
2145 cl = u->out_bufs;
|
|
2146
|
|
2147 if (cl) {
|
|
2148 buf->pos = cl->buf->pos;
|
|
2149 }
|
|
2150
|
|
2151 buf->last = buf->pos;
|
|
2152
|
|
2153 for (cl = u->out_bufs; cl; cl = cl->next) {
|
|
2154 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2155 "http proxy in memory %p-%p %uz",
|
|
2156 cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf));
|
|
2157
|
|
2158 if (buf->last == cl->buf->pos) {
|
|
2159 buf->last = cl->buf->last;
|
|
2160 continue;
|
|
2161 }
|
|
2162
|
|
2163 buf->last = ngx_movemem(buf->last, cl->buf->pos,
|
|
2164 cl->buf->last - cl->buf->pos);
|
|
2165
|
|
2166 cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos);
|
|
2167 cl->buf->last = buf->last;
|
|
2168 }
|
|
2169 }
|
|
2170
|
|
2171 return NGX_OK;
|
|
2172 }
|
|
2173
|
|
2174
|
58
|
2175 static void
|
|
2176 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
2177 {
|
|
2178 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2179 "abort http proxy request");
|
126
|
2180
|
58
|
2181 return;
|
|
2182 }
|
|
2183
|
|
2184
|
|
2185 static void
|
|
2186 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
2187 {
|
58
|
2188 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2189 "finalize http proxy request");
|
|
2190
|
|
2191 return;
|
|
2192 }
|
|
2193
|
|
2194
|
122
|
2195 static ngx_int_t
|
|
2196 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
2197 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2198 {
|
348
|
2199 ngx_http_proxy_ctx_t *ctx;
|
|
2200
|
|
2201 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2202
|
|
2203 if (ctx == NULL) {
|
|
2204 v->not_found = 1;
|
|
2205 return NGX_OK;
|
|
2206 }
|
|
2207
|
|
2208 v->len = ctx->vars.host_header.len;
|
126
|
2209 v->valid = 1;
|
340
|
2210 v->no_cacheable = 0;
|
122
|
2211 v->not_found = 0;
|
348
|
2212 v->data = ctx->vars.host_header.data;
|
58
|
2213
|
122
|
2214 return NGX_OK;
|
58
|
2215 }
|
|
2216
|
|
2217
|
122
|
2218 static ngx_int_t
|
|
2219 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
2220 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2221 {
|
348
|
2222 ngx_http_proxy_ctx_t *ctx;
|
|
2223
|
|
2224 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2225
|
|
2226 if (ctx == NULL) {
|
|
2227 v->not_found = 1;
|
|
2228 return NGX_OK;
|
|
2229 }
|
|
2230
|
|
2231 v->len = ctx->vars.port.len;
|
126
|
2232 v->valid = 1;
|
340
|
2233 v->no_cacheable = 0;
|
122
|
2234 v->not_found = 0;
|
348
|
2235 v->data = ctx->vars.port.data;
|
58
|
2236
|
122
|
2237 return NGX_OK;
|
58
|
2238 }
|
|
2239
|
|
2240
|
122
|
2241 static ngx_int_t
|
58
|
2242 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
2243 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2244 {
|
122
|
2245 u_char *p;
|
58
|
2246
|
126
|
2247 v->valid = 1;
|
340
|
2248 v->no_cacheable = 0;
|
122
|
2249 v->not_found = 0;
|
58
|
2250
|
|
2251 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
2252 v->len = r->connection->addr_text.len;
|
|
2253 v->data = r->connection->addr_text.data;
|
|
2254 return NGX_OK;
|
58
|
2255 }
|
|
2256
|
122
|
2257 v->len = r->headers_in.x_forwarded_for->value.len
|
|
2258 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
2259
|
382
|
2260 p = ngx_pnalloc(r->pool, v->len);
|
58
|
2261 if (p == NULL) {
|
122
|
2262 return NGX_ERROR;
|
58
|
2263 }
|
|
2264
|
122
|
2265 v->data = p;
|
58
|
2266
|
122
|
2267 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
2268 r->headers_in.x_forwarded_for->value.len);
|
58
|
2269
|
|
2270 *p++ = ','; *p++ = ' ';
|
|
2271
|
|
2272 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
2273
|
122
|
2274 return NGX_OK;
|
58
|
2275 }
|
|
2276
|
|
2277
|
|
2278 static ngx_int_t
|
126
|
2279 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
2280 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2281 {
|
348
|
2282 ngx_http_proxy_ctx_t *ctx;
|
|
2283
|
|
2284 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2285
|
|
2286 if (ctx == NULL) {
|
126
|
2287 v->not_found = 1;
|
|
2288 return NGX_OK;
|
|
2289 }
|
|
2290
|
|
2291 v->valid = 1;
|
340
|
2292 v->no_cacheable = 0;
|
126
|
2293 v->not_found = 0;
|
|
2294
|
382
|
2295 v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
|
126
|
2296
|
|
2297 if (v->data == NULL) {
|
|
2298 return NGX_ERROR;
|
|
2299 }
|
|
2300
|
348
|
2301 v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
|
126
|
2302
|
|
2303 return NGX_OK;
|
|
2304 }
|
|
2305
|
|
2306
|
|
2307 static ngx_int_t
|
58
|
2308 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2309 size_t prefix)
|
|
2310 {
|
662
|
2311 size_t len;
|
58
|
2312 ngx_int_t rc;
|
|
2313 ngx_uint_t i;
|
662
|
2314 ngx_http_proxy_rewrite_t *pr;
|
58
|
2315 ngx_http_proxy_loc_conf_t *plcf;
|
|
2316
|
|
2317 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2318
|
|
2319 pr = plcf->redirects->elts;
|
|
2320
|
|
2321 if (pr == NULL) {
|
|
2322 return NGX_DECLINED;
|
|
2323 }
|
|
2324
|
662
|
2325 len = h->value.len - prefix;
|
|
2326
|
58
|
2327 for (i = 0; i < plcf->redirects->nelts; i++) {
|
662
|
2328 rc = pr[i].handler(r, h, prefix, len, &pr[i]);
|
|
2329
|
|
2330 if (rc != NGX_DECLINED) {
|
|
2331 return rc;
|
|
2332 }
|
|
2333 }
|
|
2334
|
|
2335 return NGX_DECLINED;
|
|
2336 }
|
|
2337
|
|
2338
|
|
2339 static ngx_int_t
|
|
2340 ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
|
|
2341 {
|
|
2342 size_t prefix;
|
|
2343 u_char *p;
|
|
2344 ngx_int_t rc, rv;
|
|
2345 ngx_http_proxy_loc_conf_t *plcf;
|
|
2346
|
|
2347 p = (u_char *) ngx_strchr(h->value.data, ';');
|
|
2348 if (p == NULL) {
|
|
2349 return NGX_DECLINED;
|
|
2350 }
|
|
2351
|
|
2352 prefix = p + 1 - h->value.data;
|
|
2353
|
|
2354 rv = NGX_DECLINED;
|
|
2355
|
|
2356 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2357
|
|
2358 if (plcf->cookie_domains) {
|
|
2359 p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1);
|
|
2360
|
|
2361 if (p) {
|
|
2362 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7,
|
|
2363 plcf->cookie_domains);
|
|
2364 if (rc == NGX_ERROR) {
|
|
2365 return NGX_ERROR;
|
|
2366 }
|
|
2367
|
|
2368 if (rc != NGX_DECLINED) {
|
|
2369 rv = rc;
|
|
2370 }
|
|
2371 }
|
|
2372 }
|
|
2373
|
|
2374 if (plcf->cookie_paths) {
|
|
2375 p = ngx_strcasestrn(h->value.data + prefix, "path=", 5 - 1);
|
|
2376
|
|
2377 if (p) {
|
|
2378 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 5,
|
|
2379 plcf->cookie_paths);
|
|
2380 if (rc == NGX_ERROR) {
|
|
2381 return NGX_ERROR;
|
|
2382 }
|
|
2383
|
|
2384 if (rc != NGX_DECLINED) {
|
|
2385 rv = rc;
|
|
2386 }
|
|
2387 }
|
|
2388 }
|
|
2389
|
|
2390 return rv;
|
|
2391 }
|
|
2392
|
|
2393
|
|
2394 static ngx_int_t
|
|
2395 ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2396 u_char *value, ngx_array_t *rewrites)
|
|
2397 {
|
|
2398 size_t len, prefix;
|
|
2399 u_char *p;
|
|
2400 ngx_int_t rc;
|
|
2401 ngx_uint_t i;
|
|
2402 ngx_http_proxy_rewrite_t *pr;
|
|
2403
|
|
2404 prefix = value - h->value.data;
|
|
2405
|
|
2406 p = (u_char *) ngx_strchr(value, ';');
|
|
2407
|
|
2408 len = p ? (size_t) (p - value) : (h->value.len - prefix);
|
|
2409
|
|
2410 pr = rewrites->elts;
|
|
2411
|
|
2412 for (i = 0; i < rewrites->nelts; i++) {
|
|
2413 rc = pr[i].handler(r, h, prefix, len, &pr[i]);
|
58
|
2414
|
|
2415 if (rc != NGX_DECLINED) {
|
|
2416 return rc;
|
|
2417 }
|
|
2418 }
|
|
2419
|
|
2420 return NGX_DECLINED;
|
|
2421 }
|
|
2422
|
|
2423
|
|
2424 static ngx_int_t
|
662
|
2425 ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r,
|
|
2426 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
58
|
2427 {
|
662
|
2428 ngx_str_t pattern, replacement;
|
|
2429
|
|
2430 if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
|
654
|
2431 return NGX_ERROR;
|
|
2432 }
|
|
2433
|
662
|
2434 if (pattern.len > len
|
|
2435 || ngx_rstrncmp(h->value.data + prefix, pattern.data,
|
|
2436 pattern.len) != 0)
|
58
|
2437 {
|
|
2438 return NGX_DECLINED;
|
|
2439 }
|
|
2440
|
654
|
2441 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
|
2442 return NGX_ERROR;
|
|
2443 }
|
|
2444
|
662
|
2445 return ngx_http_proxy_rewrite(r, h, prefix, pattern.len, &replacement);
|
58
|
2446 }
|
|
2447
|
|
2448
|
654
|
2449 #if (NGX_PCRE)
|
|
2450
|
58
|
2451 static ngx_int_t
|
662
|
2452 ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2453 size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
58
|
2454 {
|
662
|
2455 ngx_str_t pattern, replacement;
|
|
2456
|
|
2457 pattern.len = len;
|
|
2458 pattern.data = h->value.data + prefix;
|
|
2459
|
|
2460 if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
|
58
|
2461 return NGX_DECLINED;
|
|
2462 }
|
|
2463
|
654
|
2464 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
58
|
2465 return NGX_ERROR;
|
|
2466 }
|
|
2467
|
662
|
2468 if (prefix == 0 && h->value.len == len) {
|
658
|
2469 h->value = replacement;
|
|
2470 return NGX_OK;
|
|
2471 }
|
|
2472
|
662
|
2473 return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
|
|
2474 }
|
|
2475
|
|
2476 #endif
|
|
2477
|
|
2478
|
|
2479 static ngx_int_t
|
|
2480 ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r,
|
|
2481 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
|
2482 {
|
|
2483 u_char *p;
|
|
2484 ngx_str_t pattern, replacement;
|
|
2485
|
|
2486 if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
|
|
2487 return NGX_ERROR;
|
|
2488 }
|
|
2489
|
|
2490 p = h->value.data + prefix;
|
|
2491
|
|
2492 if (p[0] == '.') {
|
|
2493 p++;
|
|
2494 prefix++;
|
|
2495 len--;
|
|
2496 }
|
|
2497
|
|
2498 if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) {
|
|
2499 return NGX_DECLINED;
|
|
2500 }
|
|
2501
|
|
2502 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
658
|
2503 return NGX_ERROR;
|
|
2504 }
|
|
2505
|
662
|
2506 return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
|
|
2507 }
|
|
2508
|
|
2509
|
|
2510 static ngx_int_t
|
|
2511 ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_table_elt_t *h, size_t prefix,
|
|
2512 size_t len, ngx_str_t *replacement)
|
|
2513 {
|
|
2514 u_char *p, *data;
|
|
2515 size_t new_len;
|
|
2516
|
|
2517 new_len = replacement->len + h->value.len - len;
|
|
2518
|
|
2519 if (replacement->len > len) {
|
|
2520
|
|
2521 data = ngx_pnalloc(r->pool, new_len);
|
|
2522 if (data == NULL) {
|
|
2523 return NGX_ERROR;
|
|
2524 }
|
|
2525
|
|
2526 p = ngx_copy(data, h->value.data, prefix);
|
|
2527 p = ngx_copy(p, replacement->data, replacement->len);
|
|
2528
|
|
2529 ngx_memcpy(p, h->value.data + prefix + len,
|
|
2530 h->value.len - len - prefix);
|
|
2531
|
|
2532 h->value.data = data;
|
|
2533
|
|
2534 } else {
|
|
2535 p = ngx_copy(h->value.data + prefix, replacement->data,
|
|
2536 replacement->len);
|
|
2537
|
|
2538 ngx_memmove(p, h->value.data + prefix + len,
|
|
2539 h->value.len - len - prefix);
|
|
2540 }
|
|
2541
|
|
2542 h->value.len = new_len;
|
58
|
2543
|
|
2544 return NGX_OK;
|
|
2545 }
|
|
2546
|
|
2547
|
|
2548 static ngx_int_t
|
|
2549 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
2550 {
|
|
2551 ngx_http_variable_t *var, *v;
|
|
2552
|
|
2553 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
2554 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2555 if (var == NULL) {
|
|
2556 return NGX_ERROR;
|
|
2557 }
|
|
2558
|
186
|
2559 var->get_handler = v->get_handler;
|
58
|
2560 var->data = v->data;
|
|
2561 }
|
|
2562
|
|
2563 return NGX_OK;
|
|
2564 }
|
|
2565
|
|
2566
|
|
2567 static void *
|
|
2568 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
2569 {
|
|
2570 ngx_http_proxy_loc_conf_t *conf;
|
|
2571
|
|
2572 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
2573 if (conf == NULL) {
|
496
|
2574 return NULL;
|
58
|
2575 }
|
|
2576
|
|
2577 /*
|
|
2578 * set by ngx_pcalloc():
|
|
2579 *
|
|
2580 * conf->upstream.bufs.num = 0;
|
484
|
2581 * conf->upstream.ignore_headers = 0;
|
58
|
2582 * conf->upstream.next_upstream = 0;
|
494
|
2583 * conf->upstream.cache_use_stale = 0;
|
|
2584 * conf->upstream.cache_methods = 0;
|
58
|
2585 * conf->upstream.temp_path = NULL;
|
198
|
2586 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
2587 * conf->upstream.uri = { 0, NULL };
|
|
2588 * conf->upstream.location = NULL;
|
320
|
2589 * conf->upstream.store_lengths = NULL;
|
|
2590 * conf->upstream.store_values = NULL;
|
58
|
2591 *
|
110
|
2592 * conf->method = NULL;
|
58
|
2593 * conf->headers_source = NULL;
|
|
2594 * conf->headers_set_len = NULL;
|
|
2595 * conf->headers_set = NULL;
|
|
2596 * conf->headers_set_hash = NULL;
|
126
|
2597 * conf->body_set_len = NULL;
|
|
2598 * conf->body_set = NULL;
|
|
2599 * conf->body_source = { 0, NULL };
|
566
|
2600 * conf->redirects = NULL;
|
58
|
2601 */
|
126
|
2602
|
320
|
2603 conf->upstream.store = NGX_CONF_UNSET;
|
|
2604 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
2605 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
2606 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
2607
|
58
|
2608 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
2609 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
2610 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
2611
|
|
2612 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
2613 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
2614
|
|
2615 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
2616 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
2617 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
2618
|
|
2619 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
2620 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
2621
|
464
|
2622 #if (NGX_HTTP_CACHE)
|
|
2623 conf->upstream.cache = NGX_CONF_UNSET_PTR;
|
|
2624 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
|
588
|
2625 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
|
572
|
2626 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
|
464
|
2627 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
|
656
|
2628 conf->upstream.cache_lock = NGX_CONF_UNSET;
|
|
2629 conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
|
464
|
2630 #endif
|
|
2631
|
356
|
2632 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
2633 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
2634
|
206
|
2635 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
2636 #if (NGX_HTTP_SSL)
|
|
2637 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
2638 #endif
|
58
|
2639
|
|
2640 /* "proxy_cyclic_temp_file" is disabled */
|
|
2641 conf->upstream.cyclic_temp_file = 0;
|
|
2642
|
|
2643 conf->redirect = NGX_CONF_UNSET;
|
198
|
2644 conf->upstream.change_buffering = 1;
|
58
|
2645
|
662
|
2646 conf->cookie_domains = NGX_CONF_UNSET_PTR;
|
|
2647 conf->cookie_paths = NGX_CONF_UNSET_PTR;
|
|
2648
|
640
|
2649 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
2650
|
298
|
2651 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
2652 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
2653
|
632
|
2654 ngx_str_set(&conf->upstream.module, "proxy");
|
|
2655
|
58
|
2656 return conf;
|
|
2657 }
|
|
2658
|
|
2659
|
|
2660 static char *
|
|
2661 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2662 {
|
|
2663 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
2664 ngx_http_proxy_loc_conf_t *conf = child;
|
|
2665
|
614
|
2666 u_char *p;
|
464
|
2667 size_t size;
|
|
2668 ngx_hash_init_t hash;
|
592
|
2669 ngx_http_core_loc_conf_t *clcf;
|
662
|
2670 ngx_http_proxy_rewrite_t *pr;
|
464
|
2671 ngx_http_script_compile_t sc;
|
58
|
2672
|
320
|
2673 if (conf->upstream.store != 0) {
|
|
2674 ngx_conf_merge_value(conf->upstream.store,
|
496
|
2675 prev->upstream.store, 0);
|
320
|
2676
|
|
2677 if (conf->upstream.store_lengths == NULL) {
|
|
2678 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
2679 conf->upstream.store_values = prev->upstream.store_values;
|
|
2680 }
|
|
2681 }
|
|
2682
|
|
2683 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
2684 prev->upstream.store_access, 0600);
|
318
|
2685
|
130
|
2686 ngx_conf_merge_value(conf->upstream.buffering,
|
|
2687 prev->upstream.buffering, 1);
|
|
2688
|
178
|
2689 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
2690 prev->upstream.ignore_client_abort, 0);
|
|
2691
|
58
|
2692 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
2693 prev->upstream.connect_timeout, 60000);
|
|
2694
|
|
2695 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
2696 prev->upstream.send_timeout, 60000);
|
|
2697
|
|
2698 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
2699 prev->upstream.read_timeout, 60000);
|
|
2700
|
|
2701 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
2702 prev->upstream.send_lowat, 0);
|
|
2703
|
130
|
2704 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
2705 prev->upstream.buffer_size,
|
58
|
2706 (size_t) ngx_pagesize);
|
|
2707
|
|
2708 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
2709 8, ngx_pagesize);
|
|
2710
|
|
2711 if (conf->upstream.bufs.num < 2) {
|
126
|
2712 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
2713 "there must be at least 2 \"proxy_buffers\"");
|
|
2714 return NGX_CONF_ERROR;
|
|
2715 }
|
126
|
2716
|
58
|
2717
|
130
|
2718 size = conf->upstream.buffer_size;
|
126
|
2719 if (size < conf->upstream.bufs.size) {
|
58
|
2720 size = conf->upstream.bufs.size;
|
|
2721 }
|
|
2722
|
|
2723
|
78
|
2724 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
2725 prev->upstream.busy_buffers_size_conf,
|
58
|
2726 NGX_CONF_UNSET_SIZE);
|
|
2727
|
78
|
2728 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2729 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
2730 } else {
|
|
2731 conf->upstream.busy_buffers_size =
|
|
2732 conf->upstream.busy_buffers_size_conf;
|
|
2733 }
|
126
|
2734
|
78
|
2735 if (conf->upstream.busy_buffers_size < size) {
|
58
|
2736 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2737 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
288
|
2738 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2739 "one of the \"proxy_buffers\"");
|
|
2740
|
|
2741 return NGX_CONF_ERROR;
|
78
|
2742 }
|
58
|
2743
|
78
|
2744 if (conf->upstream.busy_buffers_size
|
|
2745 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
2746 {
|
|
2747 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2748 "\"proxy_busy_buffers_size\" must be less than "
|
|
2749 "the size of all \"proxy_buffers\" minus one buffer");
|
|
2750
|
|
2751 return NGX_CONF_ERROR;
|
|
2752 }
|
78
|
2753
|
58
|
2754
|
78
|
2755 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
2756 prev->upstream.temp_file_write_size_conf,
|
58
|
2757 NGX_CONF_UNSET_SIZE);
|
|
2758
|
78
|
2759 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2760 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
2761 } else {
|
|
2762 conf->upstream.temp_file_write_size =
|
|
2763 conf->upstream.temp_file_write_size_conf;
|
|
2764 }
|
126
|
2765
|
78
|
2766 if (conf->upstream.temp_file_write_size < size) {
|
58
|
2767 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2768 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
288
|
2769 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2770 "one of the \"proxy_buffers\"");
|
|
2771
|
|
2772 return NGX_CONF_ERROR;
|
|
2773 }
|
|
2774
|
78
|
2775 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
2776 prev->upstream.max_temp_file_size_conf,
|
58
|
2777 NGX_CONF_UNSET_SIZE);
|
|
2778
|
78
|
2779 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2780 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
2781 } else {
|
|
2782 conf->upstream.max_temp_file_size =
|
|
2783 conf->upstream.max_temp_file_size_conf;
|
|
2784 }
|
58
|
2785
|
78
|
2786 if (conf->upstream.max_temp_file_size != 0
|
|
2787 && conf->upstream.max_temp_file_size < size)
|
58
|
2788 {
|
|
2789 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
2790 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
58
|
2791 "the temporary files usage or must be equal or bigger than "
|
288
|
2792 "maximum of the value of \"proxy_buffer_size\" and "
|
110
|
2793 "one of the \"proxy_buffers\"");
|
58
|
2794
|
|
2795 return NGX_CONF_ERROR;
|
|
2796 }
|
|
2797
|
78
|
2798
|
484
|
2799 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
|
|
2800 prev->upstream.ignore_headers,
|
|
2801 NGX_CONF_BITMASK_SET);
|
|
2802
|
|
2803
|
58
|
2804 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
2805 prev->upstream.next_upstream,
|
|
2806 (NGX_CONF_BITMASK_SET
|
|
2807 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
2808 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
2809
|
214
|
2810 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2811 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
2812 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2813 }
|
|
2814
|
464
|
2815 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
|
58
|
2816 prev->upstream.temp_path,
|
464
|
2817 &ngx_http_proxy_temp_path)
|
|
2818 != NGX_OK)
|
|
2819 {
|
|
2820 return NGX_CONF_ERROR;
|
|
2821 }
|
|
2822
|
|
2823
|
|
2824 #if (NGX_HTTP_CACHE)
|
|
2825
|
|
2826 ngx_conf_merge_ptr_value(conf->upstream.cache,
|
|
2827 prev->upstream.cache, NULL);
|
|
2828
|
|
2829 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
|
|
2830 ngx_shm_zone_t *shm_zone;
|
|
2831
|
|
2832 shm_zone = conf->upstream.cache;
|
|
2833
|
|
2834 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2835 "\"proxy_cache\" zone \"%V\" is unknown",
|
480
|
2836 &shm_zone->shm.name);
|
464
|
2837
|
|
2838 return NGX_CONF_ERROR;
|
|
2839 }
|
|
2840
|
|
2841 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
|
|
2842 prev->upstream.cache_min_uses, 1);
|
|
2843
|
|
2844 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
|
|
2845 prev->upstream.cache_use_stale,
|
|
2846 (NGX_CONF_BITMASK_SET
|
|
2847 |NGX_HTTP_UPSTREAM_FT_OFF));
|
|
2848
|
658
|
2849 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2850 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
|
|
2851 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2852 }
|
|
2853
|
|
2854 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
2855 conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
|
|
2856 }
|
|
2857
|
494
|
2858 if (conf->upstream.cache_methods == 0) {
|
|
2859 conf->upstream.cache_methods = prev->upstream.cache_methods;
|
|
2860 }
|
|
2861
|
|
2862 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
|
|
2863
|
588
|
2864 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
|
|
2865 prev->upstream.cache_bypass, NULL);
|
|
2866
|
572
|
2867 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
|
|
2868 prev->upstream.no_cache, NULL);
|
|
2869
|
588
|
2870 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
|
|
2871 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
2872 "\"proxy_no_cache\" functionality has been changed in 0.8.46, "
|
|
2873 "now it should be used together with \"proxy_cache_bypass\"");
|
|
2874 }
|
|
2875
|
464
|
2876 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
|
|
2877 prev->upstream.cache_valid, NULL);
|
|
2878
|
472
|
2879 if (conf->cache_key.value.data == NULL) {
|
|
2880 conf->cache_key = prev->cache_key;
|
|
2881 }
|
|
2882
|
656
|
2883 ngx_conf_merge_value(conf->upstream.cache_lock,
|
|
2884 prev->upstream.cache_lock, 0);
|
|
2885
|
|
2886 ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
|
|
2887 prev->upstream.cache_lock_timeout, 5000);
|
|
2888
|
464
|
2889 #endif
|
58
|
2890
|
110
|
2891 if (conf->method.len == 0) {
|
|
2892 conf->method = prev->method;
|
|
2893
|
|
2894 } else {
|
|
2895 conf->method.data[conf->method.len] = ' ';
|
|
2896 conf->method.len++;
|
58
|
2897 }
|
|
2898
|
|
2899 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
2900 prev->upstream.pass_request_headers, 1);
|
|
2901 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
2902 prev->upstream.pass_request_body, 1);
|
|
2903
|
206
|
2904 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
2905 prev->upstream.intercept_errors, 0);
|
58
|
2906
|
398
|
2907 #if (NGX_HTTP_SSL)
|
|
2908 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
2909 prev->upstream.ssl_session_reuse, 1);
|
|
2910 #endif
|
|
2911
|
58
|
2912 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
2913
|
|
2914 if (conf->redirect) {
|
|
2915
|
|
2916 if (conf->redirects == NULL) {
|
|
2917 conf->redirects = prev->redirects;
|
|
2918 }
|
|
2919
|
348
|
2920 if (conf->redirects == NULL && conf->url.data) {
|
58
|
2921
|
|
2922 conf->redirects = ngx_array_create(cf->pool, 1,
|
662
|
2923 sizeof(ngx_http_proxy_rewrite_t));
|
58
|
2924 if (conf->redirects == NULL) {
|
|
2925 return NGX_CONF_ERROR;
|
|
2926 }
|
|
2927
|
|
2928 pr = ngx_array_push(conf->redirects);
|
|
2929 if (pr == NULL) {
|
|
2930 return NGX_CONF_ERROR;
|
|
2931 }
|
|
2932
|
662
|
2933 ngx_memzero(&pr->pattern.complex,
|
654
|
2934 sizeof(ngx_http_complex_value_t));
|
|
2935
|
|
2936 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
|
2937
|
662
|
2938 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
348
|
2939
|
|
2940 if (conf->vars.uri.len) {
|
662
|
2941 pr->pattern.complex.value = conf->url;
|
654
|
2942 pr->replacement.value = conf->location;
|
202
|
2943
|
|
2944 } else {
|
662
|
2945 pr->pattern.complex.value.len = conf->url.len
|
|
2946 + sizeof("/") - 1;
|
|
2947
|
|
2948 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
|
614
|
2949 if (p == NULL) {
|
|
2950 return NGX_CONF_ERROR;
|
|
2951 }
|
|
2952
|
662
|
2953 pr->pattern.complex.value.data = p;
|
614
|
2954
|
|
2955 p = ngx_cpymem(p, conf->url.data, conf->url.len);
|
|
2956 *p = '/';
|
|
2957
|
654
|
2958 ngx_str_set(&pr->replacement.value, "/");
|
202
|
2959 }
|
58
|
2960 }
|
|
2961 }
|
|
2962
|
662
|
2963 ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL);
|
|
2964
|
|
2965 ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL);
|
|
2966
|
482
|
2967 #if (NGX_HTTP_SSL)
|
|
2968 if (conf->upstream.ssl == NULL) {
|
|
2969 conf->upstream.ssl = prev->upstream.ssl;
|
|
2970 }
|
|
2971 #endif
|
|
2972
|
640
|
2973 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
|
|
2974 NGX_HTTP_VERSION_10);
|
|
2975
|
298
|
2976 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
2977 prev->headers_hash_max_size, 512);
|
|
2978
|
|
2979 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
2980 prev->headers_hash_bucket_size, 64);
|
|
2981
|
|
2982 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
2983 ngx_cacheline_size);
|
|
2984
|
356
|
2985 hash.max_size = conf->headers_hash_max_size;
|
|
2986 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2987 hash.name = "proxy_headers_hash";
|
|
2988
|
|
2989 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
584
|
2990 &prev->upstream, ngx_http_proxy_hide_headers, &hash)
|
198
|
2991 != NGX_OK)
|
|
2992 {
|
|
2993 return NGX_CONF_ERROR;
|
|
2994 }
|
|
2995
|
260
|
2996 if (conf->upstream.upstream == NULL) {
|
|
2997 conf->upstream.upstream = prev->upstream.upstream;
|
348
|
2998 conf->vars = prev->vars;
|
58
|
2999 }
|
|
3000
|
592
|
3001 if (conf->proxy_lengths == NULL) {
|
|
3002 conf->proxy_lengths = prev->proxy_lengths;
|
|
3003 conf->proxy_values = prev->proxy_values;
|
|
3004 }
|
|
3005
|
|
3006 if (conf->upstream.upstream || conf->proxy_lengths) {
|
|
3007 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
3008 if (clcf->handler == NULL && clcf->lmt_excpt) {
|
|
3009 clcf->handler = ngx_http_proxy_handler;
|
|
3010 conf->location = prev->location;
|
|
3011 }
|
|
3012 }
|
126
|
3013
|
|
3014 if (conf->body_source.data == NULL) {
|
|
3015 conf->body_source = prev->body_source;
|
|
3016 conf->body_set_len = prev->body_set_len;
|
|
3017 conf->body_set = prev->body_set;
|
|
3018 }
|
|
3019
|
128
|
3020 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
3021
|
|
3022 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3023
|
|
3024 sc.cf = cf;
|
|
3025 sc.source = &conf->body_source;
|
|
3026 sc.flushes = &conf->flushes;
|
|
3027 sc.lengths = &conf->body_set_len;
|
|
3028 sc.values = &conf->body_set;
|
|
3029 sc.complete_lengths = 1;
|
|
3030 sc.complete_values = 1;
|
|
3031
|
|
3032 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3033 return NGX_CONF_ERROR;
|
|
3034 }
|
|
3035 }
|
|
3036
|
464
|
3037 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
|
|
3038 return NGX_CONF_ERROR;
|
|
3039 }
|
|
3040
|
|
3041 return NGX_CONF_OK;
|
|
3042 }
|
|
3043
|
|
3044
|
|
3045 static ngx_int_t
|
|
3046 ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
|
|
3047 ngx_http_proxy_loc_conf_t *prev)
|
|
3048 {
|
|
3049 u_char *p;
|
|
3050 size_t size;
|
|
3051 uintptr_t *code;
|
|
3052 ngx_uint_t i;
|
648
|
3053 ngx_array_t headers_names, headers_merged;
|
464
|
3054 ngx_keyval_t *src, *s, *h;
|
|
3055 ngx_hash_key_t *hk;
|
|
3056 ngx_hash_init_t hash;
|
|
3057 ngx_http_script_compile_t sc;
|
|
3058 ngx_http_script_copy_code_t *copy;
|
126
|
3059
|
58
|
3060 if (conf->headers_source == NULL) {
|
122
|
3061 conf->flushes = prev->flushes;
|
58
|
3062 conf->headers_set_len = prev->headers_set_len;
|
|
3063 conf->headers_set = prev->headers_set;
|
|
3064 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
3065 conf->headers_source = prev->headers_source;
|
58
|
3066 }
|
|
3067
|
498
|
3068 if (conf->headers_set_hash.buckets
|
648
|
3069 && ((conf->body_source.data == NULL)
|
|
3070 == (prev->body_source.data == NULL))
|
502
|
3071 #if (NGX_HTTP_CACHE)
|
|
3072 && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
|
|
3073 #endif
|
|
3074 )
|
498
|
3075 {
|
464
|
3076 return NGX_OK;
|
58
|
3077 }
|
|
3078
|
|
3079
|
576
|
3080 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
3081 != NGX_OK)
|
|
3082 {
|
464
|
3083 return NGX_ERROR;
|
58
|
3084 }
|
|
3085
|
648
|
3086 if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
|
|
3087 != NGX_OK)
|
|
3088 {
|
|
3089 return NGX_ERROR;
|
|
3090 }
|
|
3091
|
58
|
3092 if (conf->headers_source == NULL) {
|
|
3093 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
3094 sizeof(ngx_keyval_t));
|
58
|
3095 if (conf->headers_source == NULL) {
|
464
|
3096 return NGX_ERROR;
|
58
|
3097 }
|
|
3098 }
|
|
3099
|
|
3100 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
3101 if (conf->headers_set_len == NULL) {
|
464
|
3102 return NGX_ERROR;
|
58
|
3103 }
|
|
3104
|
|
3105 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
3106 if (conf->headers_set == NULL) {
|
464
|
3107 return NGX_ERROR;
|
58
|
3108 }
|
|
3109
|
|
3110
|
480
|
3111 #if (NGX_HTTP_CACHE)
|
|
3112
|
|
3113 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
|
|
3114 ngx_http_proxy_headers;
|
|
3115 #else
|
|
3116
|
|
3117 h = ngx_http_proxy_headers;
|
|
3118
|
|
3119 #endif
|
|
3120
|
648
|
3121 src = conf->headers_source->elts;
|
|
3122 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
3123
|
|
3124 s = ngx_array_push(&headers_merged);
|
|
3125 if (s == NULL) {
|
|
3126 return NGX_ERROR;
|
|
3127 }
|
|
3128
|
|
3129 *s = src[i];
|
|
3130 }
|
|
3131
|
480
|
3132 while (h->key.len) {
|
58
|
3133
|
648
|
3134 src = headers_merged.elts;
|
|
3135 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
3136 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
3137 goto next;
|
|
3138 }
|
|
3139 }
|
|
3140
|
648
|
3141 s = ngx_array_push(&headers_merged);
|
58
|
3142 if (s == NULL) {
|
464
|
3143 return NGX_ERROR;
|
58
|
3144 }
|
|
3145
|
|
3146 *s = *h;
|
|
3147
|
|
3148 next:
|
|
3149
|
480
|
3150 h++;
|
58
|
3151 }
|
|
3152
|
648
|
3153 if (conf->body_source.data) {
|
|
3154 s = ngx_array_push(&headers_merged);
|
|
3155 if (s == NULL) {
|
|
3156 return NGX_ERROR;
|
|
3157 }
|
|
3158
|
|
3159 ngx_str_set(&s->key, "Content-Length");
|
|
3160 ngx_str_set(&s->value, "$proxy_internal_body_length");
|
|
3161 }
|
|
3162
|
|
3163
|
|
3164 src = headers_merged.elts;
|
|
3165 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
3166
|
576
|
3167 hk = ngx_array_push(&headers_names);
|
198
|
3168 if (hk == NULL) {
|
464
|
3169 return NGX_ERROR;
|
58
|
3170 }
|
|
3171
|
198
|
3172 hk->key = src[i].key;
|
|
3173 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
3174 hk->value = (void *) 1;
|
58
|
3175
|
68
|
3176 if (src[i].value.len == 0) {
|
|
3177 continue;
|
|
3178 }
|
|
3179
|
58
|
3180 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
3181 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3182 sizeof(ngx_http_script_copy_code_t));
|
|
3183 if (copy == NULL) {
|
464
|
3184 return NGX_ERROR;
|
58
|
3185 }
|
|
3186
|
|
3187 copy->code = (ngx_http_script_code_pt)
|
|
3188 ngx_http_script_copy_len_code;
|
|
3189 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
3190 + src[i].value.len + sizeof(CRLF) - 1;
|
|
3191
|
|
3192
|
|
3193 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3194 + src[i].key.len + sizeof(": ") - 1
|
|
3195 + src[i].value.len + sizeof(CRLF) - 1
|
|
3196 + sizeof(uintptr_t) - 1)
|
|
3197 & ~(sizeof(uintptr_t) - 1);
|
|
3198
|
|
3199 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3200 if (copy == NULL) {
|
464
|
3201 return NGX_ERROR;
|
58
|
3202 }
|
|
3203
|
|
3204 copy->code = ngx_http_script_copy_code;
|
|
3205 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
3206 + src[i].value.len + sizeof(CRLF) - 1;
|
|
3207
|
|
3208 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3209
|
|
3210 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3211 *p++ = ':'; *p++ = ' ';
|
|
3212 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
3213 *p++ = CR; *p = LF;
|
|
3214
|
|
3215 } else {
|
|
3216 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3217 sizeof(ngx_http_script_copy_code_t));
|
|
3218 if (copy == NULL) {
|
464
|
3219 return NGX_ERROR;
|
58
|
3220 }
|
|
3221
|
|
3222 copy->code = (ngx_http_script_code_pt)
|
|
3223 ngx_http_script_copy_len_code;
|
|
3224 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3225
|
|
3226
|
|
3227 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3228 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
3229 & ~(sizeof(uintptr_t) - 1);
|
|
3230
|
|
3231 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3232 if (copy == NULL) {
|
464
|
3233 return NGX_ERROR;
|
58
|
3234 }
|
|
3235
|
|
3236 copy->code = ngx_http_script_copy_code;
|
|
3237 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
3238
|
|
3239 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3240 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
3241 *p++ = ':'; *p = ' ';
|
|
3242
|
|
3243
|
|
3244 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3245
|
|
3246 sc.cf = cf;
|
|
3247 sc.source = &src[i].value;
|
122
|
3248 sc.flushes = &conf->flushes;
|
58
|
3249 sc.lengths = &conf->headers_set_len;
|
|
3250 sc.values = &conf->headers_set;
|
|
3251
|
|
3252 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
464
|
3253 return NGX_ERROR;
|
58
|
3254 }
|
|
3255
|
|
3256
|
|
3257 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3258 sizeof(ngx_http_script_copy_code_t));
|
|
3259 if (copy == NULL) {
|
464
|
3260 return NGX_ERROR;
|
58
|
3261 }
|
|
3262
|
|
3263 copy->code = (ngx_http_script_code_pt)
|
|
3264 ngx_http_script_copy_len_code;
|
|
3265 copy->len = sizeof(CRLF) - 1;
|
|
3266
|
|
3267
|
|
3268 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3269 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
3270 & ~(sizeof(uintptr_t) - 1);
|
|
3271
|
|
3272 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3273 if (copy == NULL) {
|
464
|
3274 return NGX_ERROR;
|
58
|
3275 }
|
|
3276
|
|
3277 copy->code = ngx_http_script_copy_code;
|
|
3278 copy->len = sizeof(CRLF) - 1;
|
|
3279
|
|
3280 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3281 *p++ = CR; *p = LF;
|
|
3282 }
|
|
3283
|
|
3284 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3285 if (code == NULL) {
|
464
|
3286 return NGX_ERROR;
|
58
|
3287 }
|
|
3288
|
|
3289 *code = (uintptr_t) NULL;
|
|
3290
|
|
3291 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
3292 if (code == NULL) {
|
464
|
3293 return NGX_ERROR;
|
58
|
3294 }
|
|
3295
|
|
3296 *code = (uintptr_t) NULL;
|
|
3297 }
|
|
3298
|
|
3299 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3300 if (code == NULL) {
|
464
|
3301 return NGX_ERROR;
|
58
|
3302 }
|
|
3303
|
|
3304 *code = (uintptr_t) NULL;
|
|
3305
|
|
3306
|
198
|
3307 hash.hash = &conf->headers_set_hash;
|
|
3308 hash.key = ngx_hash_key_lc;
|
298
|
3309 hash.max_size = conf->headers_hash_max_size;
|
|
3310 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
3311 hash.name = "proxy_headers_hash";
|
198
|
3312 hash.pool = cf->pool;
|
|
3313 hash.temp_pool = NULL;
|
|
3314
|
576
|
3315 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
|
58
|
3316 }
|
|
3317
|
|
3318
|
|
3319 static char *
|
|
3320 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3321 {
|
|
3322 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3323
|
348
|
3324 size_t add;
|
|
3325 u_short port;
|
|
3326 ngx_str_t *value, *url;
|
|
3327 ngx_url_t u;
|
|
3328 ngx_uint_t n;
|
|
3329 ngx_http_core_loc_conf_t *clcf;
|
|
3330 ngx_http_script_compile_t sc;
|
58
|
3331
|
430
|
3332 if (plcf->upstream.upstream || plcf->proxy_lengths) {
|
104
|
3333 return "is duplicate";
|
|
3334 }
|
|
3335
|
348
|
3336 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
3337
|
510
|
3338 clcf->handler = ngx_http_proxy_handler;
|
|
3339
|
|
3340 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
3341 clcf->auto_redirect = 1;
|
|
3342 }
|
|
3343
|
58
|
3344 value = cf->args->elts;
|
|
3345
|
|
3346 url = &value[1];
|
|
3347
|
348
|
3348 n = ngx_http_script_variables_count(url);
|
|
3349
|
|
3350 if (n) {
|
|
3351
|
|
3352 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3353
|
|
3354 sc.cf = cf;
|
|
3355 sc.source = url;
|
|
3356 sc.lengths = &plcf->proxy_lengths;
|
|
3357 sc.values = &plcf->proxy_values;
|
|
3358 sc.variables = n;
|
|
3359 sc.complete_lengths = 1;
|
|
3360 sc.complete_values = 1;
|
354
|
3361
|
348
|
3362 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3363 return NGX_CONF_ERROR;
|
|
3364 }
|
|
3365
|
|
3366 #if (NGX_HTTP_SSL)
|
|
3367 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3368 return NGX_CONF_ERROR;
|
|
3369 }
|
|
3370 #endif
|
|
3371
|
|
3372 return NGX_CONF_OK;
|
|
3373 }
|
|
3374
|
286
|
3375 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
3376 add = 7;
|
140
|
3377 port = 80;
|
126
|
3378
|
286
|
3379 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
3380
|
|
3381 #if (NGX_HTTP_SSL)
|
348
|
3382 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3383 return NGX_CONF_ERROR;
|
|
3384 }
|
126
|
3385
|
|
3386 add = 8;
|
140
|
3387 port = 443;
|
126
|
3388 #else
|
|
3389 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3390 "https protocol requires SSL support");
|
|
3391 return NGX_CONF_ERROR;
|
|
3392 #endif
|
|
3393
|
|
3394 } else {
|
58
|
3395 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
3396 return NGX_CONF_ERROR;
|
|
3397 }
|
|
3398
|
200
|
3399 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3400
|
|
3401 u.url.len = url->len - add;
|
|
3402 u.url.data = url->data + add;
|
266
|
3403 u.default_port = port;
|
|
3404 u.uri_part = 1;
|
260
|
3405 u.no_resolve = 1;
|
|
3406
|
|
3407 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
3408 if (plcf->upstream.upstream == NULL) {
|
58
|
3409 return NGX_CONF_ERROR;
|
|
3410 }
|
|
3411
|
430
|
3412 plcf->vars.schema.len = add;
|
|
3413 plcf->vars.schema.data = url->data;
|
464
|
3414 plcf->vars.key_start = plcf->vars.schema;
|
|
3415
|
|
3416 ngx_http_proxy_set_vars(&u, &plcf->vars);
|
58
|
3417
|
464
|
3418 plcf->location = clcf->name;
|
|
3419
|
324
|
3420 if (clcf->named
|
58
|
3421 #if (NGX_PCRE)
|
324
|
3422 || clcf->regex
|
|
3423 #endif
|
|
3424 || clcf->noname)
|
|
3425 {
|
348
|
3426 if (plcf->vars.uri.len) {
|
122
|
3427 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3428 "\"proxy_pass\" may not have URI part in "
|
256
|
3429 "location given by regular expression, "
|
324
|
3430 "or inside named location, "
|
256
|
3431 "or inside the \"if\" statement, "
|
|
3432 "or inside the \"limit_except\" block");
|
122
|
3433 return NGX_CONF_ERROR;
|
|
3434 }
|
|
3435
|
348
|
3436 plcf->location.len = 0;
|
104
|
3437 }
|
|
3438
|
348
|
3439 plcf->url = *url;
|
58
|
3440
|
|
3441 return NGX_CONF_OK;
|
|
3442 }
|
|
3443
|
|
3444
|
|
3445 static char *
|
|
3446 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3447 {
|
|
3448 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3449
|
654
|
3450 u_char *p;
|
|
3451 ngx_str_t *value;
|
662
|
3452 ngx_http_proxy_rewrite_t *pr;
|
654
|
3453 ngx_http_compile_complex_value_t ccv;
|
58
|
3454
|
|
3455 if (plcf->redirect == 0) {
|
|
3456 return NGX_CONF_OK;
|
|
3457 }
|
|
3458
|
660
|
3459 plcf->redirect = 1;
|
|
3460
|
58
|
3461 value = cf->args->elts;
|
|
3462
|
484
|
3463 if (cf->args->nelts == 2) {
|
|
3464 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3465 plcf->redirect = 0;
|
|
3466 plcf->redirects = NULL;
|
|
3467 return NGX_CONF_OK;
|
|
3468 }
|
|
3469
|
|
3470 if (ngx_strcmp(value[1].data, "false") == 0) {
|
|
3471 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
3472 "invalid parameter \"false\", use \"off\" instead");
|
|
3473 plcf->redirect = 0;
|
|
3474 plcf->redirects = NULL;
|
|
3475 return NGX_CONF_OK;
|
|
3476 }
|
|
3477
|
|
3478 if (ngx_strcmp(value[1].data, "default") != 0) {
|
|
3479 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3480 "invalid parameter \"%V\"", &value[1]);
|
|
3481 return NGX_CONF_ERROR;
|
|
3482 }
|
58
|
3483 }
|
|
3484
|
|
3485 if (plcf->redirects == NULL) {
|
|
3486 plcf->redirects = ngx_array_create(cf->pool, 1,
|
662
|
3487 sizeof(ngx_http_proxy_rewrite_t));
|
58
|
3488 if (plcf->redirects == NULL) {
|
|
3489 return NGX_CONF_ERROR;
|
|
3490 }
|
|
3491 }
|
|
3492
|
|
3493 pr = ngx_array_push(plcf->redirects);
|
|
3494 if (pr == NULL) {
|
|
3495 return NGX_CONF_ERROR;
|
|
3496 }
|
|
3497
|
484
|
3498 if (ngx_strcmp(value[1].data, "default") == 0) {
|
566
|
3499 if (plcf->proxy_lengths) {
|
|
3500 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3501 "\"proxy_redirect default\" may not be used "
|
|
3502 "with \"proxy_pass\" directive with variables");
|
|
3503 return NGX_CONF_ERROR;
|
|
3504 }
|
|
3505
|
348
|
3506 if (plcf->url.data == NULL) {
|
58
|
3507 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
566
|
3508 "\"proxy_redirect default\" must go "
|
58
|
3509 "after the \"proxy_pass\" directive");
|
|
3510 return NGX_CONF_ERROR;
|
|
3511 }
|
|
3512
|
662
|
3513 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
|
3514
|
|
3515 ngx_memzero(&pr->pattern.complex, sizeof(ngx_http_complex_value_t));
|
654
|
3516
|
|
3517 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
348
|
3518
|
|
3519 if (plcf->vars.uri.len) {
|
662
|
3520 pr->pattern.complex.value = plcf->url;
|
654
|
3521 pr->replacement.value = plcf->location;
|
202
|
3522
|
|
3523 } else {
|
662
|
3524 pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1;
|
|
3525
|
|
3526 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
|
614
|
3527 if (p == NULL) {
|
|
3528 return NGX_CONF_ERROR;
|
|
3529 }
|
|
3530
|
662
|
3531 pr->pattern.complex.value.data = p;
|
614
|
3532
|
|
3533 p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
|
|
3534 *p = '/';
|
|
3535
|
654
|
3536 ngx_str_set(&pr->replacement.value, "/");
|
202
|
3537 }
|
58
|
3538
|
|
3539 return NGX_CONF_OK;
|
|
3540 }
|
|
3541
|
654
|
3542
|
|
3543 if (value[1].data[0] == '~') {
|
|
3544 value[1].len--;
|
|
3545 value[1].data++;
|
|
3546
|
|
3547 if (value[1].data[0] == '*') {
|
|
3548 value[1].len--;
|
|
3549 value[1].data++;
|
662
|
3550
|
|
3551 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3552 return NGX_CONF_ERROR;
|
|
3553 }
|
|
3554
|
|
3555 } else {
|
|
3556 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
|
|
3557 return NGX_CONF_ERROR;
|
|
3558 }
|
654
|
3559 }
|
|
3560
|
|
3561 } else {
|
|
3562
|
|
3563 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3564
|
|
3565 ccv.cf = cf;
|
|
3566 ccv.value = &value[1];
|
662
|
3567 ccv.complex_value = &pr->pattern.complex;
|
654
|
3568
|
|
3569 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3570 return NGX_CONF_ERROR;
|
|
3571 }
|
|
3572
|
662
|
3573 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
58
|
3574 }
|
|
3575
|
654
|
3576
|
|
3577 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3578
|
|
3579 ccv.cf = cf;
|
|
3580 ccv.value = &value[2];
|
|
3581 ccv.complex_value = &pr->replacement;
|
|
3582
|
|
3583 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
58
|
3584 return NGX_CONF_ERROR;
|
|
3585 }
|
|
3586
|
|
3587 return NGX_CONF_OK;
|
|
3588 }
|
|
3589
|
|
3590
|
|
3591 static char *
|
662
|
3592 ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3593 {
|
|
3594 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3595
|
|
3596 ngx_str_t *value;
|
|
3597 ngx_http_proxy_rewrite_t *pr;
|
|
3598 ngx_http_compile_complex_value_t ccv;
|
|
3599
|
|
3600 if (plcf->cookie_domains == NULL) {
|
|
3601 return NGX_CONF_OK;
|
|
3602 }
|
|
3603
|
|
3604 value = cf->args->elts;
|
|
3605
|
|
3606 if (cf->args->nelts == 2) {
|
|
3607
|
|
3608 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3609 plcf->cookie_domains = NULL;
|
|
3610 return NGX_CONF_OK;
|
|
3611 }
|
|
3612
|
|
3613 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3614 "invalid parameter \"%V\"", &value[1]);
|
|
3615 return NGX_CONF_ERROR;
|
|
3616 }
|
|
3617
|
|
3618 if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) {
|
|
3619 plcf->cookie_domains = ngx_array_create(cf->pool, 1,
|
|
3620 sizeof(ngx_http_proxy_rewrite_t));
|
|
3621 if (plcf->cookie_domains == NULL) {
|
|
3622 return NGX_CONF_ERROR;
|
|
3623 }
|
|
3624 }
|
|
3625
|
|
3626 pr = ngx_array_push(plcf->cookie_domains);
|
|
3627 if (pr == NULL) {
|
|
3628 return NGX_CONF_ERROR;
|
|
3629 }
|
|
3630
|
|
3631 if (value[1].data[0] == '~') {
|
|
3632 value[1].len--;
|
|
3633 value[1].data++;
|
|
3634
|
|
3635 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3636 return NGX_CONF_ERROR;
|
|
3637 }
|
|
3638
|
|
3639 } else {
|
|
3640
|
|
3641 if (value[1].data[0] == '.') {
|
|
3642 value[1].len--;
|
|
3643 value[1].data++;
|
|
3644 }
|
|
3645
|
|
3646 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3647
|
|
3648 ccv.cf = cf;
|
|
3649 ccv.value = &value[1];
|
|
3650 ccv.complex_value = &pr->pattern.complex;
|
|
3651
|
|
3652 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3653 return NGX_CONF_ERROR;
|
|
3654 }
|
|
3655
|
|
3656 pr->handler = ngx_http_proxy_rewrite_domain_handler;
|
|
3657
|
|
3658 if (value[2].data[0] == '.') {
|
|
3659 value[2].len--;
|
|
3660 value[2].data++;
|
|
3661 }
|
|
3662 }
|
|
3663
|
|
3664 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3665
|
|
3666 ccv.cf = cf;
|
|
3667 ccv.value = &value[2];
|
|
3668 ccv.complex_value = &pr->replacement;
|
|
3669
|
|
3670 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3671 return NGX_CONF_ERROR;
|
|
3672 }
|
|
3673
|
|
3674 return NGX_CONF_OK;
|
|
3675 }
|
|
3676
|
|
3677
|
|
3678 static char *
|
|
3679 ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3680 {
|
|
3681 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3682
|
|
3683 ngx_str_t *value;
|
|
3684 ngx_http_proxy_rewrite_t *pr;
|
|
3685 ngx_http_compile_complex_value_t ccv;
|
|
3686
|
|
3687 if (plcf->cookie_paths == NULL) {
|
|
3688 return NGX_CONF_OK;
|
|
3689 }
|
|
3690
|
|
3691 value = cf->args->elts;
|
|
3692
|
|
3693 if (cf->args->nelts == 2) {
|
|
3694
|
|
3695 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3696 plcf->cookie_paths = NULL;
|
|
3697 return NGX_CONF_OK;
|
|
3698 }
|
|
3699
|
|
3700 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3701 "invalid parameter \"%V\"", &value[1]);
|
|
3702 return NGX_CONF_ERROR;
|
|
3703 }
|
|
3704
|
|
3705 if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
|
|
3706 plcf->cookie_paths = ngx_array_create(cf->pool, 1,
|
|
3707 sizeof(ngx_http_proxy_rewrite_t));
|
|
3708 if (plcf->cookie_paths == NULL) {
|
|
3709 return NGX_CONF_ERROR;
|
|
3710 }
|
|
3711 }
|
|
3712
|
|
3713 pr = ngx_array_push(plcf->cookie_paths);
|
|
3714 if (pr == NULL) {
|
|
3715 return NGX_CONF_ERROR;
|
|
3716 }
|
|
3717
|
|
3718 if (value[1].data[0] == '~') {
|
|
3719 value[1].len--;
|
|
3720 value[1].data++;
|
|
3721
|
|
3722 if (value[1].data[0] == '*') {
|
|
3723 value[1].len--;
|
|
3724 value[1].data++;
|
|
3725
|
|
3726 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3727 return NGX_CONF_ERROR;
|
|
3728 }
|
|
3729
|
|
3730 } else {
|
|
3731 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
|
|
3732 return NGX_CONF_ERROR;
|
|
3733 }
|
|
3734 }
|
|
3735
|
|
3736 } else {
|
|
3737
|
|
3738 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3739
|
|
3740 ccv.cf = cf;
|
|
3741 ccv.value = &value[1];
|
|
3742 ccv.complex_value = &pr->pattern.complex;
|
|
3743
|
|
3744 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3745 return NGX_CONF_ERROR;
|
|
3746 }
|
|
3747
|
|
3748 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
|
3749 }
|
|
3750
|
|
3751 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3752
|
|
3753 ccv.cf = cf;
|
|
3754 ccv.value = &value[2];
|
|
3755 ccv.complex_value = &pr->replacement;
|
|
3756
|
|
3757 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3758 return NGX_CONF_ERROR;
|
|
3759 }
|
|
3760
|
|
3761 return NGX_CONF_OK;
|
|
3762 }
|
|
3763
|
|
3764
|
|
3765 static ngx_int_t
|
|
3766 ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
|
|
3767 ngx_str_t *regex, ngx_uint_t caseless)
|
|
3768 {
|
|
3769 #if (NGX_PCRE)
|
|
3770 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
3771 ngx_regex_compile_t rc;
|
|
3772
|
|
3773 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
3774
|
|
3775 rc.pattern = *regex;
|
|
3776 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
3777 rc.err.data = errstr;
|
|
3778
|
|
3779 if (caseless) {
|
|
3780 rc.options = NGX_REGEX_CASELESS;
|
|
3781 }
|
|
3782
|
|
3783 pr->pattern.regex = ngx_http_regex_compile(cf, &rc);
|
|
3784 if (pr->pattern.regex == NULL) {
|
|
3785 return NGX_ERROR;
|
|
3786 }
|
|
3787
|
|
3788 pr->handler = ngx_http_proxy_rewrite_regex_handler;
|
|
3789
|
|
3790 return NGX_OK;
|
|
3791
|
|
3792 #else
|
|
3793
|
|
3794 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3795 "using regex \"%V\" requires PCRE library", regex);
|
|
3796 return NGX_ERROR;
|
|
3797
|
|
3798 #endif
|
|
3799 }
|
|
3800
|
|
3801
|
|
3802 static char *
|
320
|
3803 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3804 {
|
|
3805 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3806
|
|
3807 ngx_str_t *value;
|
|
3808 ngx_http_script_compile_t sc;
|
|
3809
|
496
|
3810 if (plcf->upstream.store != NGX_CONF_UNSET
|
|
3811 || plcf->upstream.store_lengths)
|
320
|
3812 {
|
|
3813 return "is duplicate";
|
|
3814 }
|
|
3815
|
|
3816 value = cf->args->elts;
|
|
3817
|
496
|
3818 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3819 plcf->upstream.store = 0;
|
320
|
3820 return NGX_CONF_OK;
|
|
3821 }
|
|
3822
|
496
|
3823 #if (NGX_HTTP_CACHE)
|
|
3824
|
|
3825 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
|
|
3826 && plcf->upstream.cache != NULL)
|
|
3827 {
|
|
3828 return "is incompatible with \"proxy_cache\"";
|
|
3829 }
|
|
3830
|
|
3831 #endif
|
|
3832
|
|
3833 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
3834 plcf->upstream.store = 1;
|
320
|
3835 return NGX_CONF_OK;
|
|
3836 }
|
|
3837
|
|
3838 /* include the terminating '\0' into script */
|
|
3839 value[1].len++;
|
|
3840
|
|
3841 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3842
|
|
3843 sc.cf = cf;
|
|
3844 sc.source = &value[1];
|
|
3845 sc.lengths = &plcf->upstream.store_lengths;
|
|
3846 sc.values = &plcf->upstream.store_values;
|
340
|
3847 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
3848 sc.complete_lengths = 1;
|
|
3849 sc.complete_values = 1;
|
|
3850
|
|
3851 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3852 return NGX_CONF_ERROR;
|
|
3853 }
|
|
3854
|
|
3855 return NGX_CONF_OK;
|
|
3856 }
|
|
3857
|
|
3858
|
464
|
3859 #if (NGX_HTTP_CACHE)
|
|
3860
|
|
3861 static char *
|
|
3862 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3863 {
|
|
3864 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3865
|
|
3866 ngx_str_t *value;
|
|
3867
|
|
3868 value = cf->args->elts;
|
|
3869
|
|
3870 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
|
|
3871 return "is duplicate";
|
|
3872 }
|
|
3873
|
|
3874 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3875 plcf->upstream.cache = NULL;
|
|
3876 return NGX_CONF_OK;
|
|
3877 }
|
|
3878
|
496
|
3879 if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
|
|
3880 return "is incompatible with \"proxy_store\"";
|
|
3881 }
|
|
3882
|
464
|
3883 plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
|
|
3884 &ngx_http_proxy_module);
|
|
3885 if (plcf->upstream.cache == NULL) {
|
|
3886 return NGX_CONF_ERROR;
|
|
3887 }
|
|
3888
|
|
3889 return NGX_CONF_OK;
|
|
3890 }
|
|
3891
|
472
|
3892
|
|
3893 static char *
|
|
3894 ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3895 {
|
|
3896 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3897
|
|
3898 ngx_str_t *value;
|
|
3899 ngx_http_compile_complex_value_t ccv;
|
|
3900
|
|
3901 value = cf->args->elts;
|
|
3902
|
|
3903 if (plcf->cache_key.value.len) {
|
|
3904 return "is duplicate";
|
|
3905 }
|
|
3906
|
|
3907 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3908
|
|
3909 ccv.cf = cf;
|
|
3910 ccv.value = &value[1];
|
|
3911 ccv.complex_value = &plcf->cache_key;
|
|
3912
|
|
3913 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3914 return NGX_CONF_ERROR;
|
|
3915 }
|
|
3916
|
|
3917 return NGX_CONF_OK;
|
|
3918 }
|
|
3919
|
464
|
3920 #endif
|
|
3921
|
|
3922
|
320
|
3923 static char *
|
58
|
3924 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
3925 {
|
|
3926 #if (NGX_FREEBSD)
|
|
3927 ssize_t *np = data;
|
|
3928
|
222
|
3929 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
3930 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3931 "\"proxy_send_lowat\" must be less than %d "
|
|
3932 "(sysctl net.inet.tcp.sendspace)",
|
|
3933 ngx_freebsd_net_inet_tcp_sendspace);
|
|
3934
|
|
3935 return NGX_CONF_ERROR;
|
|
3936 }
|
|
3937
|
|
3938 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
3939 ssize_t *np = data;
|
|
3940
|
|
3941 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
3942 "\"proxy_send_lowat\" is not supported, ignored");
|
|
3943
|
|
3944 *np = 0;
|
|
3945
|
|
3946 #endif
|
|
3947
|
|
3948 return NGX_CONF_OK;
|
|
3949 }
|
260
|
3950
|
|
3951
|
348
|
3952 #if (NGX_HTTP_SSL)
|
|
3953
|
|
3954 static ngx_int_t
|
|
3955 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
3956 {
|
|
3957 ngx_pool_cleanup_t *cln;
|
|
3958
|
|
3959 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
3960 if (plcf->upstream.ssl == NULL) {
|
|
3961 return NGX_ERROR;
|
|
3962 }
|
|
3963
|
|
3964 plcf->upstream.ssl->log = cf->log;
|
|
3965
|
|
3966 if (ngx_ssl_create(plcf->upstream.ssl,
|
658
|
3967 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
3968 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2,
|
|
3969 NULL)
|
348
|
3970 != NGX_OK)
|
|
3971 {
|
|
3972 return NGX_ERROR;
|
|
3973 }
|
|
3974
|
|
3975 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
3976 if (cln == NULL) {
|
|
3977 return NGX_ERROR;
|
|
3978 }
|
|
3979
|
|
3980 cln->handler = ngx_ssl_cleanup_ctx;
|
|
3981 cln->data = plcf->upstream.ssl;
|
|
3982
|
|
3983 return NGX_OK;
|
|
3984 }
|
|
3985
|
|
3986 #endif
|
|
3987
|
|
3988
|
464
|
3989 static void
|
|
3990 ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
|
348
|
3991 {
|
400
|
3992 if (u->family != AF_UNIX) {
|
464
|
3993
|
348
|
3994 if (u->no_port || u->port == u->default_port) {
|
464
|
3995
|
348
|
3996 v->host_header = u->host;
|
|
3997
|
|
3998 if (u->default_port == 80) {
|
570
|
3999 ngx_str_set(&v->port, "80");
|
348
|
4000
|
|
4001 } else {
|
570
|
4002 ngx_str_set(&v->port, "443");
|
348
|
4003 }
|
|
4004
|
|
4005 } else {
|
354
|
4006 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
4007 v->host_header.data = u->host.data;
|
|
4008 v->port = u->port_text;
|
348
|
4009 }
|
|
4010
|
464
|
4011 v->key_start.len += v->host_header.len;
|
|
4012
|
348
|
4013 } else {
|
570
|
4014 ngx_str_set(&v->host_header, "localhost");
|
|
4015 ngx_str_null(&v->port);
|
464
|
4016 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
|
348
|
4017 }
|
|
4018
|
|
4019 v->uri = u->uri;
|
|
4020 }
|