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