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