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