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
|
|
1281 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
|
|
1282 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
|
|
1283
|
198
|
1284 if (h->key.len == r->lowcase_index) {
|
|
1285 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1286
|
|
1287 } else {
|
392
|
1288 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
58
|
1289 }
|
|
1290
|
198
|
1291 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
|
|
1292 h->lowcase_key, h->key.len);
|
|
1293
|
|
1294 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
356
|
1295 return NGX_ERROR;
|
198
|
1296 }
|
|
1297
|
58
|
1298 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1299 "http proxy header: \"%V: %V\"",
|
|
1300 &h->key, &h->value);
|
|
1301
|
|
1302 continue;
|
|
1303 }
|
|
1304
|
|
1305 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1306
|
|
1307 /* a whole header has been parsed successfully */
|
|
1308
|
|
1309 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1310 "http proxy header done");
|
|
1311
|
198
|
1312 /*
|
|
1313 * if no "Server" and "Date" in header line,
|
|
1314 * then add the special empty headers
|
|
1315 */
|
|
1316
|
|
1317 if (r->upstream->headers_in.server == NULL) {
|
|
1318 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1319 if (h == NULL) {
|
356
|
1320 return NGX_ERROR;
|
198
|
1321 }
|
|
1322
|
|
1323 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
|
|
1324 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
|
|
1325
|
570
|
1326 ngx_str_set(&h->key, "Server");
|
|
1327 ngx_str_null(&h->value);
|
198
|
1328 h->lowcase_key = (u_char *) "server";
|
|
1329 }
|
|
1330
|
|
1331 if (r->upstream->headers_in.date == NULL) {
|
|
1332 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1333 if (h == NULL) {
|
356
|
1334 return NGX_ERROR;
|
198
|
1335 }
|
|
1336
|
|
1337 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
|
|
1338
|
570
|
1339 ngx_str_set(&h->key, "Date");
|
|
1340 ngx_str_null(&h->value);
|
198
|
1341 h->lowcase_key = (u_char *) "date";
|
|
1342 }
|
|
1343
|
58
|
1344 return NGX_OK;
|
|
1345 }
|
|
1346
|
70
|
1347 if (rc == NGX_AGAIN) {
|
|
1348 return NGX_AGAIN;
|
|
1349 }
|
|
1350
|
58
|
1351 /* there was error while a header line parsing */
|
|
1352
|
|
1353 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
168
|
1354 "upstream sent invalid header");
|
58
|
1355
|
|
1356 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1357 }
|
|
1358 }
|
|
1359
|
|
1360
|
|
1361 static void
|
|
1362 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
1363 {
|
|
1364 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1365 "abort http proxy request");
|
126
|
1366
|
58
|
1367 return;
|
|
1368 }
|
|
1369
|
|
1370
|
|
1371 static void
|
|
1372 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
1373 {
|
58
|
1374 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1375 "finalize http proxy request");
|
|
1376
|
|
1377 return;
|
|
1378 }
|
|
1379
|
|
1380
|
122
|
1381 static ngx_int_t
|
|
1382 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
1383 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1384 {
|
348
|
1385 ngx_http_proxy_ctx_t *ctx;
|
|
1386
|
|
1387 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1388
|
|
1389 if (ctx == NULL) {
|
|
1390 v->not_found = 1;
|
|
1391 return NGX_OK;
|
|
1392 }
|
|
1393
|
|
1394 v->len = ctx->vars.host_header.len;
|
126
|
1395 v->valid = 1;
|
340
|
1396 v->no_cacheable = 0;
|
122
|
1397 v->not_found = 0;
|
348
|
1398 v->data = ctx->vars.host_header.data;
|
58
|
1399
|
122
|
1400 return NGX_OK;
|
58
|
1401 }
|
|
1402
|
|
1403
|
122
|
1404 static ngx_int_t
|
|
1405 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
1406 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1407 {
|
348
|
1408 ngx_http_proxy_ctx_t *ctx;
|
|
1409
|
|
1410 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1411
|
|
1412 if (ctx == NULL) {
|
|
1413 v->not_found = 1;
|
|
1414 return NGX_OK;
|
|
1415 }
|
|
1416
|
|
1417 v->len = ctx->vars.port.len;
|
126
|
1418 v->valid = 1;
|
340
|
1419 v->no_cacheable = 0;
|
122
|
1420 v->not_found = 0;
|
348
|
1421 v->data = ctx->vars.port.data;
|
58
|
1422
|
122
|
1423 return NGX_OK;
|
58
|
1424 }
|
|
1425
|
|
1426
|
122
|
1427 static ngx_int_t
|
58
|
1428 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
1429 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1430 {
|
122
|
1431 u_char *p;
|
58
|
1432
|
126
|
1433 v->valid = 1;
|
340
|
1434 v->no_cacheable = 0;
|
122
|
1435 v->not_found = 0;
|
58
|
1436
|
|
1437 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
1438 v->len = r->connection->addr_text.len;
|
|
1439 v->data = r->connection->addr_text.data;
|
|
1440 return NGX_OK;
|
58
|
1441 }
|
|
1442
|
122
|
1443 v->len = r->headers_in.x_forwarded_for->value.len
|
|
1444 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
1445
|
382
|
1446 p = ngx_pnalloc(r->pool, v->len);
|
58
|
1447 if (p == NULL) {
|
122
|
1448 return NGX_ERROR;
|
58
|
1449 }
|
|
1450
|
122
|
1451 v->data = p;
|
58
|
1452
|
122
|
1453 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
1454 r->headers_in.x_forwarded_for->value.len);
|
58
|
1455
|
|
1456 *p++ = ','; *p++ = ' ';
|
|
1457
|
|
1458 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
1459
|
122
|
1460 return NGX_OK;
|
58
|
1461 }
|
|
1462
|
|
1463
|
|
1464 static ngx_int_t
|
126
|
1465 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
1466 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1467 {
|
348
|
1468 ngx_http_proxy_ctx_t *ctx;
|
|
1469
|
|
1470 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1471
|
|
1472 if (ctx == NULL) {
|
126
|
1473 v->not_found = 1;
|
|
1474 return NGX_OK;
|
|
1475 }
|
|
1476
|
|
1477 v->valid = 1;
|
340
|
1478 v->no_cacheable = 0;
|
126
|
1479 v->not_found = 0;
|
|
1480
|
382
|
1481 v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
|
126
|
1482
|
|
1483 if (v->data == NULL) {
|
|
1484 return NGX_ERROR;
|
|
1485 }
|
|
1486
|
348
|
1487 v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
|
126
|
1488
|
|
1489 return NGX_OK;
|
|
1490 }
|
|
1491
|
|
1492
|
|
1493 static ngx_int_t
|
58
|
1494 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1495 size_t prefix)
|
|
1496 {
|
|
1497 ngx_int_t rc;
|
|
1498 ngx_uint_t i;
|
|
1499 ngx_http_proxy_loc_conf_t *plcf;
|
|
1500 ngx_http_proxy_redirect_t *pr;
|
|
1501
|
|
1502 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
1503
|
|
1504 pr = plcf->redirects->elts;
|
|
1505
|
|
1506 if (pr == NULL) {
|
|
1507 return NGX_DECLINED;
|
|
1508 }
|
|
1509
|
|
1510 for (i = 0; i < plcf->redirects->nelts; i++) {
|
154
|
1511 rc = pr[i].handler(r, h, prefix, &pr[i]);
|
58
|
1512
|
|
1513 if (rc != NGX_DECLINED) {
|
|
1514 return rc;
|
|
1515 }
|
|
1516 }
|
|
1517
|
|
1518 return NGX_DECLINED;
|
|
1519 }
|
|
1520
|
|
1521
|
|
1522 static ngx_int_t
|
|
1523 ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1524 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
1525 {
|
|
1526 size_t len;
|
|
1527 u_char *data, *p;
|
|
1528
|
|
1529 if (pr->redirect.len > h->value.len - prefix
|
|
1530 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
1531 pr->redirect.len) != 0)
|
|
1532 {
|
|
1533 return NGX_DECLINED;
|
|
1534 }
|
|
1535
|
578
|
1536 len = pr->replacement.text.len + h->value.len - pr->redirect.len;
|
58
|
1537
|
382
|
1538 data = ngx_pnalloc(r->pool, len);
|
58
|
1539 if (data == NULL) {
|
|
1540 return NGX_ERROR;
|
|
1541 }
|
|
1542
|
578
|
1543 p = ngx_copy(data, h->value.data, prefix);
|
58
|
1544
|
202
|
1545 if (pr->replacement.text.len) {
|
|
1546 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len);
|
|
1547 }
|
58
|
1548
|
|
1549 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len,
|
|
1550 h->value.len - pr->redirect.len - prefix);
|
|
1551
|
|
1552 h->value.len = len;
|
|
1553 h->value.data = data;
|
|
1554
|
|
1555 return NGX_OK;
|
|
1556 }
|
|
1557
|
|
1558
|
|
1559 static ngx_int_t
|
|
1560 ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1561 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
1562 {
|
|
1563 size_t len;
|
|
1564 u_char *data, *p;
|
|
1565 ngx_http_script_code_pt code;
|
|
1566 ngx_http_script_engine_t e;
|
|
1567 ngx_http_script_len_code_pt lcode;
|
|
1568
|
|
1569 if (pr->redirect.len > h->value.len - prefix
|
|
1570 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
1571 pr->redirect.len) != 0)
|
|
1572 {
|
|
1573 return NGX_DECLINED;
|
|
1574 }
|
|
1575
|
|
1576 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
|
1577
|
|
1578 e.ip = pr->replacement.vars.lengths;
|
|
1579 e.request = r;
|
|
1580
|
578
|
1581 len = h->value.len - pr->redirect.len;
|
238
|
1582
|
|
1583 while (*(uintptr_t *) e.ip) {
|
58
|
1584 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
238
|
1585 len += lcode(&e);
|
58
|
1586 }
|
|
1587
|
382
|
1588 data = ngx_pnalloc(r->pool, len);
|
58
|
1589 if (data == NULL) {
|
|
1590 return NGX_ERROR;
|
|
1591 }
|
|
1592
|
578
|
1593 p = ngx_copy(data, h->value.data, prefix);
|
58
|
1594
|
|
1595 e.ip = pr->replacement.vars.values;
|
|
1596 e.pos = p;
|
|
1597
|
|
1598 while (*(uintptr_t *) e.ip) {
|
|
1599 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1600 code(&e);
|
|
1601 }
|
|
1602
|
238
|
1603 ngx_memcpy(e.pos, h->value.data + prefix + pr->redirect.len,
|
|
1604 h->value.len - pr->redirect.len - prefix);
|
|
1605
|
58
|
1606 h->value.len = len;
|
|
1607 h->value.data = data;
|
|
1608
|
|
1609 return NGX_OK;
|
|
1610 }
|
|
1611
|
|
1612
|
|
1613 static ngx_int_t
|
|
1614 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
1615 {
|
|
1616 ngx_http_variable_t *var, *v;
|
|
1617
|
|
1618 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
1619 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
1620 if (var == NULL) {
|
|
1621 return NGX_ERROR;
|
|
1622 }
|
|
1623
|
186
|
1624 var->get_handler = v->get_handler;
|
58
|
1625 var->data = v->data;
|
|
1626 }
|
|
1627
|
|
1628 return NGX_OK;
|
|
1629 }
|
|
1630
|
|
1631
|
|
1632 static void *
|
|
1633 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
1634 {
|
|
1635 ngx_http_proxy_loc_conf_t *conf;
|
|
1636
|
|
1637 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
1638 if (conf == NULL) {
|
496
|
1639 return NULL;
|
58
|
1640 }
|
|
1641
|
|
1642 /*
|
|
1643 * set by ngx_pcalloc():
|
|
1644 *
|
|
1645 * conf->upstream.bufs.num = 0;
|
484
|
1646 * conf->upstream.ignore_headers = 0;
|
58
|
1647 * conf->upstream.next_upstream = 0;
|
494
|
1648 * conf->upstream.cache_use_stale = 0;
|
|
1649 * conf->upstream.cache_methods = 0;
|
58
|
1650 * conf->upstream.temp_path = NULL;
|
198
|
1651 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
1652 * conf->upstream.uri = { 0, NULL };
|
|
1653 * conf->upstream.location = NULL;
|
320
|
1654 * conf->upstream.store_lengths = NULL;
|
|
1655 * conf->upstream.store_values = NULL;
|
58
|
1656 *
|
110
|
1657 * conf->method = NULL;
|
58
|
1658 * conf->headers_source = NULL;
|
|
1659 * conf->headers_set_len = NULL;
|
|
1660 * conf->headers_set = NULL;
|
|
1661 * conf->headers_set_hash = NULL;
|
126
|
1662 * conf->body_set_len = NULL;
|
|
1663 * conf->body_set = NULL;
|
|
1664 * conf->body_source = { 0, NULL };
|
566
|
1665 * conf->redirects = NULL;
|
58
|
1666 */
|
126
|
1667
|
320
|
1668 conf->upstream.store = NGX_CONF_UNSET;
|
|
1669 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
1670 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
1671 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
1672
|
58
|
1673 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
1674 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
1675 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
1676
|
|
1677 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
1678 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
1679
|
|
1680 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
1681 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
1682 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
1683
|
|
1684 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
1685 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
1686
|
464
|
1687 #if (NGX_HTTP_CACHE)
|
|
1688 conf->upstream.cache = NGX_CONF_UNSET_PTR;
|
|
1689 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
|
588
|
1690 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
|
572
|
1691 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
|
464
|
1692 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
|
|
1693 #endif
|
|
1694
|
356
|
1695 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
1696 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
1697
|
206
|
1698 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
1699 #if (NGX_HTTP_SSL)
|
|
1700 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
1701 #endif
|
58
|
1702
|
|
1703 /* "proxy_cyclic_temp_file" is disabled */
|
|
1704 conf->upstream.cyclic_temp_file = 0;
|
|
1705
|
|
1706 conf->redirect = NGX_CONF_UNSET;
|
198
|
1707 conf->upstream.change_buffering = 1;
|
58
|
1708
|
298
|
1709 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
1710 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
1711
|
632
|
1712 ngx_str_set(&conf->upstream.module, "proxy");
|
|
1713
|
58
|
1714 return conf;
|
|
1715 }
|
|
1716
|
|
1717
|
|
1718 static char *
|
|
1719 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1720 {
|
|
1721 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
1722 ngx_http_proxy_loc_conf_t *conf = child;
|
|
1723
|
614
|
1724 u_char *p;
|
464
|
1725 size_t size;
|
|
1726 ngx_hash_init_t hash;
|
592
|
1727 ngx_http_core_loc_conf_t *clcf;
|
464
|
1728 ngx_http_proxy_redirect_t *pr;
|
|
1729 ngx_http_script_compile_t sc;
|
58
|
1730
|
320
|
1731 if (conf->upstream.store != 0) {
|
|
1732 ngx_conf_merge_value(conf->upstream.store,
|
496
|
1733 prev->upstream.store, 0);
|
320
|
1734
|
|
1735 if (conf->upstream.store_lengths == NULL) {
|
|
1736 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
1737 conf->upstream.store_values = prev->upstream.store_values;
|
|
1738 }
|
|
1739 }
|
|
1740
|
|
1741 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
1742 prev->upstream.store_access, 0600);
|
318
|
1743
|
130
|
1744 ngx_conf_merge_value(conf->upstream.buffering,
|
|
1745 prev->upstream.buffering, 1);
|
|
1746
|
178
|
1747 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
1748 prev->upstream.ignore_client_abort, 0);
|
|
1749
|
58
|
1750 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
1751 prev->upstream.connect_timeout, 60000);
|
|
1752
|
|
1753 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
1754 prev->upstream.send_timeout, 60000);
|
|
1755
|
|
1756 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
1757 prev->upstream.read_timeout, 60000);
|
|
1758
|
|
1759 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
1760 prev->upstream.send_lowat, 0);
|
|
1761
|
130
|
1762 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
1763 prev->upstream.buffer_size,
|
58
|
1764 (size_t) ngx_pagesize);
|
|
1765
|
|
1766 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
1767 8, ngx_pagesize);
|
|
1768
|
|
1769 if (conf->upstream.bufs.num < 2) {
|
126
|
1770 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
1771 "there must be at least 2 \"proxy_buffers\"");
|
|
1772 return NGX_CONF_ERROR;
|
|
1773 }
|
126
|
1774
|
58
|
1775
|
130
|
1776 size = conf->upstream.buffer_size;
|
126
|
1777 if (size < conf->upstream.bufs.size) {
|
58
|
1778 size = conf->upstream.bufs.size;
|
|
1779 }
|
|
1780
|
|
1781
|
78
|
1782 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
1783 prev->upstream.busy_buffers_size_conf,
|
58
|
1784 NGX_CONF_UNSET_SIZE);
|
|
1785
|
78
|
1786 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1787 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
1788 } else {
|
|
1789 conf->upstream.busy_buffers_size =
|
|
1790 conf->upstream.busy_buffers_size_conf;
|
|
1791 }
|
126
|
1792
|
78
|
1793 if (conf->upstream.busy_buffers_size < size) {
|
58
|
1794 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1795 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
288
|
1796 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
1797 "one of the \"proxy_buffers\"");
|
|
1798
|
|
1799 return NGX_CONF_ERROR;
|
78
|
1800 }
|
58
|
1801
|
78
|
1802 if (conf->upstream.busy_buffers_size
|
|
1803 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
1804 {
|
|
1805 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1806 "\"proxy_busy_buffers_size\" must be less than "
|
|
1807 "the size of all \"proxy_buffers\" minus one buffer");
|
|
1808
|
|
1809 return NGX_CONF_ERROR;
|
|
1810 }
|
78
|
1811
|
58
|
1812
|
78
|
1813 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
1814 prev->upstream.temp_file_write_size_conf,
|
58
|
1815 NGX_CONF_UNSET_SIZE);
|
|
1816
|
78
|
1817 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1818 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
1819 } else {
|
|
1820 conf->upstream.temp_file_write_size =
|
|
1821 conf->upstream.temp_file_write_size_conf;
|
|
1822 }
|
126
|
1823
|
78
|
1824 if (conf->upstream.temp_file_write_size < size) {
|
58
|
1825 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1826 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
288
|
1827 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
1828 "one of the \"proxy_buffers\"");
|
|
1829
|
|
1830 return NGX_CONF_ERROR;
|
|
1831 }
|
|
1832
|
78
|
1833 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
1834 prev->upstream.max_temp_file_size_conf,
|
58
|
1835 NGX_CONF_UNSET_SIZE);
|
|
1836
|
78
|
1837 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1838 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
1839 } else {
|
|
1840 conf->upstream.max_temp_file_size =
|
|
1841 conf->upstream.max_temp_file_size_conf;
|
|
1842 }
|
58
|
1843
|
78
|
1844 if (conf->upstream.max_temp_file_size != 0
|
|
1845 && conf->upstream.max_temp_file_size < size)
|
58
|
1846 {
|
|
1847 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
1848 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
58
|
1849 "the temporary files usage or must be equal or bigger than "
|
288
|
1850 "maximum of the value of \"proxy_buffer_size\" and "
|
110
|
1851 "one of the \"proxy_buffers\"");
|
58
|
1852
|
|
1853 return NGX_CONF_ERROR;
|
|
1854 }
|
|
1855
|
78
|
1856
|
484
|
1857 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
|
|
1858 prev->upstream.ignore_headers,
|
|
1859 NGX_CONF_BITMASK_SET);
|
|
1860
|
|
1861
|
58
|
1862 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
1863 prev->upstream.next_upstream,
|
|
1864 (NGX_CONF_BITMASK_SET
|
|
1865 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
1866 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
1867
|
214
|
1868 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
1869 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
1870 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
1871 }
|
|
1872
|
464
|
1873 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
|
58
|
1874 prev->upstream.temp_path,
|
464
|
1875 &ngx_http_proxy_temp_path)
|
|
1876 != NGX_OK)
|
|
1877 {
|
|
1878 return NGX_CONF_ERROR;
|
|
1879 }
|
|
1880
|
|
1881
|
|
1882 #if (NGX_HTTP_CACHE)
|
|
1883
|
|
1884 ngx_conf_merge_ptr_value(conf->upstream.cache,
|
|
1885 prev->upstream.cache, NULL);
|
|
1886
|
|
1887 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
|
|
1888 ngx_shm_zone_t *shm_zone;
|
|
1889
|
|
1890 shm_zone = conf->upstream.cache;
|
|
1891
|
|
1892 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1893 "\"proxy_cache\" zone \"%V\" is unknown",
|
480
|
1894 &shm_zone->shm.name);
|
464
|
1895
|
|
1896 return NGX_CONF_ERROR;
|
|
1897 }
|
|
1898
|
|
1899 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
|
|
1900 prev->upstream.cache_min_uses, 1);
|
|
1901
|
|
1902 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
|
|
1903 prev->upstream.cache_use_stale,
|
|
1904 (NGX_CONF_BITMASK_SET
|
|
1905 |NGX_HTTP_UPSTREAM_FT_OFF));
|
|
1906
|
644
|
1907 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
1908 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
|
|
1909 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
1910 }
|
|
1911
|
|
1912 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
1913 conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
|
|
1914 }
|
|
1915
|
494
|
1916 if (conf->upstream.cache_methods == 0) {
|
|
1917 conf->upstream.cache_methods = prev->upstream.cache_methods;
|
|
1918 }
|
|
1919
|
|
1920 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
|
|
1921
|
588
|
1922 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
|
|
1923 prev->upstream.cache_bypass, NULL);
|
|
1924
|
572
|
1925 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
|
|
1926 prev->upstream.no_cache, NULL);
|
|
1927
|
588
|
1928 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
|
|
1929 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
1930 "\"proxy_no_cache\" functionality has been changed in 0.8.46, "
|
|
1931 "now it should be used together with \"proxy_cache_bypass\"");
|
|
1932 }
|
|
1933
|
464
|
1934 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
|
|
1935 prev->upstream.cache_valid, NULL);
|
|
1936
|
472
|
1937 if (conf->cache_key.value.data == NULL) {
|
|
1938 conf->cache_key = prev->cache_key;
|
|
1939 }
|
|
1940
|
464
|
1941 #endif
|
58
|
1942
|
110
|
1943 if (conf->method.len == 0) {
|
|
1944 conf->method = prev->method;
|
|
1945
|
|
1946 } else {
|
|
1947 conf->method.data[conf->method.len] = ' ';
|
|
1948 conf->method.len++;
|
58
|
1949 }
|
|
1950
|
|
1951 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
1952 prev->upstream.pass_request_headers, 1);
|
|
1953 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
1954 prev->upstream.pass_request_body, 1);
|
|
1955
|
206
|
1956 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
1957 prev->upstream.intercept_errors, 0);
|
58
|
1958
|
398
|
1959 #if (NGX_HTTP_SSL)
|
|
1960 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
1961 prev->upstream.ssl_session_reuse, 1);
|
|
1962 #endif
|
|
1963
|
58
|
1964 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
1965
|
|
1966 if (conf->redirect) {
|
|
1967
|
|
1968 if (conf->redirects == NULL) {
|
|
1969 conf->redirects = prev->redirects;
|
|
1970 }
|
|
1971
|
348
|
1972 if (conf->redirects == NULL && conf->url.data) {
|
58
|
1973
|
|
1974 conf->redirects = ngx_array_create(cf->pool, 1,
|
|
1975 sizeof(ngx_http_proxy_redirect_t));
|
|
1976 if (conf->redirects == NULL) {
|
|
1977 return NGX_CONF_ERROR;
|
|
1978 }
|
|
1979
|
|
1980 pr = ngx_array_push(conf->redirects);
|
|
1981 if (pr == NULL) {
|
|
1982 return NGX_CONF_ERROR;
|
|
1983 }
|
|
1984
|
|
1985 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
1986
|
|
1987 if (conf->vars.uri.len) {
|
614
|
1988 pr->redirect = conf->url;
|
348
|
1989 pr->replacement.text = conf->location;
|
202
|
1990
|
|
1991 } else {
|
614
|
1992 pr->redirect.len = conf->url.len + sizeof("/") - 1;
|
|
1993
|
|
1994 p = ngx_pnalloc(cf->pool, pr->redirect.len);
|
|
1995 if (p == NULL) {
|
|
1996 return NGX_CONF_ERROR;
|
|
1997 }
|
|
1998
|
|
1999 pr->redirect.data = p;
|
|
2000
|
|
2001 p = ngx_cpymem(p, conf->url.data, conf->url.len);
|
|
2002 *p = '/';
|
|
2003
|
|
2004 ngx_str_set(&pr->replacement.text, "/");
|
202
|
2005 }
|
58
|
2006 }
|
|
2007 }
|
|
2008
|
482
|
2009 #if (NGX_HTTP_SSL)
|
|
2010 if (conf->upstream.ssl == NULL) {
|
|
2011 conf->upstream.ssl = prev->upstream.ssl;
|
|
2012 }
|
|
2013 #endif
|
|
2014
|
298
|
2015 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
2016 prev->headers_hash_max_size, 512);
|
|
2017
|
|
2018 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
2019 prev->headers_hash_bucket_size, 64);
|
|
2020
|
|
2021 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
2022 ngx_cacheline_size);
|
|
2023
|
356
|
2024 hash.max_size = conf->headers_hash_max_size;
|
|
2025 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2026 hash.name = "proxy_headers_hash";
|
|
2027
|
|
2028 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
584
|
2029 &prev->upstream, ngx_http_proxy_hide_headers, &hash)
|
198
|
2030 != NGX_OK)
|
|
2031 {
|
|
2032 return NGX_CONF_ERROR;
|
|
2033 }
|
|
2034
|
260
|
2035 if (conf->upstream.upstream == NULL) {
|
|
2036 conf->upstream.upstream = prev->upstream.upstream;
|
348
|
2037 conf->vars = prev->vars;
|
58
|
2038 }
|
|
2039
|
592
|
2040 if (conf->proxy_lengths == NULL) {
|
|
2041 conf->proxy_lengths = prev->proxy_lengths;
|
|
2042 conf->proxy_values = prev->proxy_values;
|
|
2043 }
|
|
2044
|
|
2045 if (conf->upstream.upstream || conf->proxy_lengths) {
|
|
2046 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2047 if (clcf->handler == NULL && clcf->lmt_excpt) {
|
|
2048 clcf->handler = ngx_http_proxy_handler;
|
|
2049 conf->location = prev->location;
|
|
2050 }
|
|
2051 }
|
126
|
2052
|
|
2053 if (conf->body_source.data == NULL) {
|
|
2054 conf->body_source = prev->body_source;
|
|
2055 conf->body_set_len = prev->body_set_len;
|
|
2056 conf->body_set = prev->body_set;
|
|
2057 }
|
|
2058
|
128
|
2059 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
2060
|
|
2061 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2062
|
|
2063 sc.cf = cf;
|
|
2064 sc.source = &conf->body_source;
|
|
2065 sc.flushes = &conf->flushes;
|
|
2066 sc.lengths = &conf->body_set_len;
|
|
2067 sc.values = &conf->body_set;
|
|
2068 sc.complete_lengths = 1;
|
|
2069 sc.complete_values = 1;
|
|
2070
|
|
2071 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2072 return NGX_CONF_ERROR;
|
|
2073 }
|
|
2074 }
|
|
2075
|
464
|
2076 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
|
|
2077 return NGX_CONF_ERROR;
|
|
2078 }
|
|
2079
|
|
2080 return NGX_CONF_OK;
|
|
2081 }
|
|
2082
|
|
2083
|
|
2084 static ngx_int_t
|
|
2085 ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
|
|
2086 ngx_http_proxy_loc_conf_t *prev)
|
|
2087 {
|
|
2088 u_char *p;
|
|
2089 size_t size;
|
|
2090 uintptr_t *code;
|
|
2091 ngx_uint_t i;
|
642
|
2092 ngx_array_t headers_names, headers_merged;
|
464
|
2093 ngx_keyval_t *src, *s, *h;
|
|
2094 ngx_hash_key_t *hk;
|
|
2095 ngx_hash_init_t hash;
|
|
2096 ngx_http_script_compile_t sc;
|
|
2097 ngx_http_script_copy_code_t *copy;
|
126
|
2098
|
58
|
2099 if (conf->headers_source == NULL) {
|
122
|
2100 conf->flushes = prev->flushes;
|
58
|
2101 conf->headers_set_len = prev->headers_set_len;
|
|
2102 conf->headers_set = prev->headers_set;
|
|
2103 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
2104 conf->headers_source = prev->headers_source;
|
58
|
2105 }
|
|
2106
|
498
|
2107 if (conf->headers_set_hash.buckets
|
642
|
2108 && ((conf->body_source.data == NULL)
|
|
2109 == (prev->body_source.data == NULL))
|
502
|
2110 #if (NGX_HTTP_CACHE)
|
|
2111 && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
|
|
2112 #endif
|
|
2113 )
|
498
|
2114 {
|
464
|
2115 return NGX_OK;
|
58
|
2116 }
|
|
2117
|
|
2118
|
576
|
2119 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
2120 != NGX_OK)
|
|
2121 {
|
464
|
2122 return NGX_ERROR;
|
58
|
2123 }
|
|
2124
|
642
|
2125 if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
|
|
2126 != NGX_OK)
|
|
2127 {
|
|
2128 return NGX_ERROR;
|
|
2129 }
|
|
2130
|
58
|
2131 if (conf->headers_source == NULL) {
|
|
2132 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
2133 sizeof(ngx_keyval_t));
|
58
|
2134 if (conf->headers_source == NULL) {
|
464
|
2135 return NGX_ERROR;
|
58
|
2136 }
|
|
2137 }
|
|
2138
|
|
2139 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
2140 if (conf->headers_set_len == NULL) {
|
464
|
2141 return NGX_ERROR;
|
58
|
2142 }
|
|
2143
|
|
2144 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
2145 if (conf->headers_set == NULL) {
|
464
|
2146 return NGX_ERROR;
|
58
|
2147 }
|
|
2148
|
|
2149
|
480
|
2150 #if (NGX_HTTP_CACHE)
|
|
2151
|
|
2152 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
|
|
2153 ngx_http_proxy_headers;
|
|
2154 #else
|
|
2155
|
|
2156 h = ngx_http_proxy_headers;
|
|
2157
|
|
2158 #endif
|
|
2159
|
642
|
2160 src = conf->headers_source->elts;
|
|
2161 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2162
|
|
2163 s = ngx_array_push(&headers_merged);
|
|
2164 if (s == NULL) {
|
|
2165 return NGX_ERROR;
|
|
2166 }
|
|
2167
|
|
2168 *s = src[i];
|
|
2169 }
|
|
2170
|
480
|
2171 while (h->key.len) {
|
58
|
2172
|
642
|
2173 src = headers_merged.elts;
|
|
2174 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
2175 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
2176 goto next;
|
|
2177 }
|
|
2178 }
|
|
2179
|
642
|
2180 s = ngx_array_push(&headers_merged);
|
58
|
2181 if (s == NULL) {
|
464
|
2182 return NGX_ERROR;
|
58
|
2183 }
|
|
2184
|
|
2185 *s = *h;
|
|
2186
|
|
2187 next:
|
|
2188
|
480
|
2189 h++;
|
58
|
2190 }
|
|
2191
|
642
|
2192 if (conf->body_source.data) {
|
|
2193 s = ngx_array_push(&headers_merged);
|
|
2194 if (s == NULL) {
|
|
2195 return NGX_ERROR;
|
|
2196 }
|
|
2197
|
|
2198 ngx_str_set(&s->key, "Content-Length");
|
|
2199 ngx_str_set(&s->value, "$proxy_internal_body_length");
|
|
2200 }
|
|
2201
|
|
2202
|
|
2203 src = headers_merged.elts;
|
|
2204 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
2205
|
576
|
2206 hk = ngx_array_push(&headers_names);
|
198
|
2207 if (hk == NULL) {
|
464
|
2208 return NGX_ERROR;
|
58
|
2209 }
|
|
2210
|
198
|
2211 hk->key = src[i].key;
|
|
2212 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
2213 hk->value = (void *) 1;
|
58
|
2214
|
68
|
2215 if (src[i].value.len == 0) {
|
|
2216 continue;
|
|
2217 }
|
|
2218
|
58
|
2219 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
2220 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2221 sizeof(ngx_http_script_copy_code_t));
|
|
2222 if (copy == NULL) {
|
464
|
2223 return NGX_ERROR;
|
58
|
2224 }
|
|
2225
|
|
2226 copy->code = (ngx_http_script_code_pt)
|
|
2227 ngx_http_script_copy_len_code;
|
|
2228 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2229 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2230
|
|
2231
|
|
2232 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2233 + src[i].key.len + sizeof(": ") - 1
|
|
2234 + src[i].value.len + sizeof(CRLF) - 1
|
|
2235 + sizeof(uintptr_t) - 1)
|
|
2236 & ~(sizeof(uintptr_t) - 1);
|
|
2237
|
|
2238 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2239 if (copy == NULL) {
|
464
|
2240 return NGX_ERROR;
|
58
|
2241 }
|
|
2242
|
|
2243 copy->code = ngx_http_script_copy_code;
|
|
2244 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2245 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2246
|
|
2247 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2248
|
|
2249 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2250 *p++ = ':'; *p++ = ' ';
|
|
2251 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
2252 *p++ = CR; *p = LF;
|
|
2253
|
|
2254 } else {
|
|
2255 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2256 sizeof(ngx_http_script_copy_code_t));
|
|
2257 if (copy == NULL) {
|
464
|
2258 return NGX_ERROR;
|
58
|
2259 }
|
|
2260
|
|
2261 copy->code = (ngx_http_script_code_pt)
|
|
2262 ngx_http_script_copy_len_code;
|
|
2263 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2264
|
|
2265
|
|
2266 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2267 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
2268 & ~(sizeof(uintptr_t) - 1);
|
|
2269
|
|
2270 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2271 if (copy == NULL) {
|
464
|
2272 return NGX_ERROR;
|
58
|
2273 }
|
|
2274
|
|
2275 copy->code = ngx_http_script_copy_code;
|
|
2276 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2277
|
|
2278 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2279 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2280 *p++ = ':'; *p = ' ';
|
|
2281
|
|
2282
|
|
2283 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2284
|
|
2285 sc.cf = cf;
|
|
2286 sc.source = &src[i].value;
|
122
|
2287 sc.flushes = &conf->flushes;
|
58
|
2288 sc.lengths = &conf->headers_set_len;
|
|
2289 sc.values = &conf->headers_set;
|
|
2290
|
|
2291 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
464
|
2292 return NGX_ERROR;
|
58
|
2293 }
|
|
2294
|
|
2295
|
|
2296 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2297 sizeof(ngx_http_script_copy_code_t));
|
|
2298 if (copy == NULL) {
|
464
|
2299 return NGX_ERROR;
|
58
|
2300 }
|
|
2301
|
|
2302 copy->code = (ngx_http_script_code_pt)
|
|
2303 ngx_http_script_copy_len_code;
|
|
2304 copy->len = sizeof(CRLF) - 1;
|
|
2305
|
|
2306
|
|
2307 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2308 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
2309 & ~(sizeof(uintptr_t) - 1);
|
|
2310
|
|
2311 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2312 if (copy == NULL) {
|
464
|
2313 return NGX_ERROR;
|
58
|
2314 }
|
|
2315
|
|
2316 copy->code = ngx_http_script_copy_code;
|
|
2317 copy->len = sizeof(CRLF) - 1;
|
|
2318
|
|
2319 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2320 *p++ = CR; *p = LF;
|
|
2321 }
|
|
2322
|
|
2323 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
2324 if (code == NULL) {
|
464
|
2325 return NGX_ERROR;
|
58
|
2326 }
|
|
2327
|
|
2328 *code = (uintptr_t) NULL;
|
|
2329
|
|
2330 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
2331 if (code == NULL) {
|
464
|
2332 return NGX_ERROR;
|
58
|
2333 }
|
|
2334
|
|
2335 *code = (uintptr_t) NULL;
|
|
2336 }
|
|
2337
|
|
2338 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
2339 if (code == NULL) {
|
464
|
2340 return NGX_ERROR;
|
58
|
2341 }
|
|
2342
|
|
2343 *code = (uintptr_t) NULL;
|
|
2344
|
|
2345
|
198
|
2346 hash.hash = &conf->headers_set_hash;
|
|
2347 hash.key = ngx_hash_key_lc;
|
298
|
2348 hash.max_size = conf->headers_hash_max_size;
|
|
2349 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2350 hash.name = "proxy_headers_hash";
|
198
|
2351 hash.pool = cf->pool;
|
|
2352 hash.temp_pool = NULL;
|
|
2353
|
576
|
2354 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
|
58
|
2355 }
|
|
2356
|
|
2357
|
|
2358 static char *
|
|
2359 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2360 {
|
|
2361 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2362
|
348
|
2363 size_t add;
|
|
2364 u_short port;
|
|
2365 ngx_str_t *value, *url;
|
|
2366 ngx_url_t u;
|
|
2367 ngx_uint_t n;
|
|
2368 ngx_http_core_loc_conf_t *clcf;
|
|
2369 ngx_http_script_compile_t sc;
|
58
|
2370
|
430
|
2371 if (plcf->upstream.upstream || plcf->proxy_lengths) {
|
104
|
2372 return "is duplicate";
|
|
2373 }
|
|
2374
|
348
|
2375 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2376
|
510
|
2377 clcf->handler = ngx_http_proxy_handler;
|
|
2378
|
|
2379 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
2380 clcf->auto_redirect = 1;
|
|
2381 }
|
|
2382
|
58
|
2383 value = cf->args->elts;
|
|
2384
|
|
2385 url = &value[1];
|
|
2386
|
348
|
2387 n = ngx_http_script_variables_count(url);
|
|
2388
|
|
2389 if (n) {
|
|
2390
|
|
2391 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2392
|
|
2393 sc.cf = cf;
|
|
2394 sc.source = url;
|
|
2395 sc.lengths = &plcf->proxy_lengths;
|
|
2396 sc.values = &plcf->proxy_values;
|
|
2397 sc.variables = n;
|
|
2398 sc.complete_lengths = 1;
|
|
2399 sc.complete_values = 1;
|
354
|
2400
|
348
|
2401 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2402 return NGX_CONF_ERROR;
|
|
2403 }
|
|
2404
|
|
2405 #if (NGX_HTTP_SSL)
|
|
2406 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
2407 return NGX_CONF_ERROR;
|
|
2408 }
|
|
2409 #endif
|
|
2410
|
|
2411 return NGX_CONF_OK;
|
|
2412 }
|
|
2413
|
286
|
2414 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
2415 add = 7;
|
140
|
2416 port = 80;
|
126
|
2417
|
286
|
2418 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
2419
|
|
2420 #if (NGX_HTTP_SSL)
|
348
|
2421 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
2422 return NGX_CONF_ERROR;
|
|
2423 }
|
126
|
2424
|
|
2425 add = 8;
|
140
|
2426 port = 443;
|
126
|
2427 #else
|
|
2428 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2429 "https protocol requires SSL support");
|
|
2430 return NGX_CONF_ERROR;
|
|
2431 #endif
|
|
2432
|
|
2433 } else {
|
58
|
2434 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
2435 return NGX_CONF_ERROR;
|
|
2436 }
|
|
2437
|
200
|
2438 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
2439
|
|
2440 u.url.len = url->len - add;
|
|
2441 u.url.data = url->data + add;
|
266
|
2442 u.default_port = port;
|
|
2443 u.uri_part = 1;
|
260
|
2444 u.no_resolve = 1;
|
|
2445
|
|
2446 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
2447 if (plcf->upstream.upstream == NULL) {
|
58
|
2448 return NGX_CONF_ERROR;
|
|
2449 }
|
|
2450
|
430
|
2451 plcf->vars.schema.len = add;
|
|
2452 plcf->vars.schema.data = url->data;
|
464
|
2453 plcf->vars.key_start = plcf->vars.schema;
|
|
2454
|
|
2455 ngx_http_proxy_set_vars(&u, &plcf->vars);
|
58
|
2456
|
464
|
2457 plcf->location = clcf->name;
|
|
2458
|
324
|
2459 if (clcf->named
|
58
|
2460 #if (NGX_PCRE)
|
324
|
2461 || clcf->regex
|
|
2462 #endif
|
|
2463 || clcf->noname)
|
|
2464 {
|
348
|
2465 if (plcf->vars.uri.len) {
|
122
|
2466 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2467 "\"proxy_pass\" may not have URI part in "
|
256
|
2468 "location given by regular expression, "
|
324
|
2469 "or inside named location, "
|
256
|
2470 "or inside the \"if\" statement, "
|
|
2471 "or inside the \"limit_except\" block");
|
122
|
2472 return NGX_CONF_ERROR;
|
|
2473 }
|
|
2474
|
348
|
2475 plcf->location.len = 0;
|
104
|
2476 }
|
|
2477
|
348
|
2478 plcf->url = *url;
|
58
|
2479
|
|
2480 return NGX_CONF_OK;
|
|
2481 }
|
|
2482
|
|
2483
|
|
2484 static char *
|
|
2485 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2486 {
|
|
2487 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2488
|
614
|
2489 u_char *p;
|
58
|
2490 ngx_str_t *value;
|
|
2491 ngx_array_t *vars_lengths, *vars_values;
|
|
2492 ngx_http_script_compile_t sc;
|
|
2493 ngx_http_proxy_redirect_t *pr;
|
|
2494
|
|
2495 if (plcf->redirect == 0) {
|
|
2496 return NGX_CONF_OK;
|
|
2497 }
|
|
2498
|
646
|
2499 plcf->redirect = 1;
|
|
2500
|
58
|
2501 value = cf->args->elts;
|
|
2502
|
484
|
2503 if (cf->args->nelts == 2) {
|
|
2504 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
2505 plcf->redirect = 0;
|
|
2506 plcf->redirects = NULL;
|
|
2507 return NGX_CONF_OK;
|
|
2508 }
|
|
2509
|
|
2510 if (ngx_strcmp(value[1].data, "false") == 0) {
|
|
2511 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
2512 "invalid parameter \"false\", use \"off\" instead");
|
|
2513 plcf->redirect = 0;
|
|
2514 plcf->redirects = NULL;
|
|
2515 return NGX_CONF_OK;
|
|
2516 }
|
|
2517
|
|
2518 if (ngx_strcmp(value[1].data, "default") != 0) {
|
|
2519 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2520 "invalid parameter \"%V\"", &value[1]);
|
|
2521 return NGX_CONF_ERROR;
|
|
2522 }
|
58
|
2523 }
|
|
2524
|
|
2525 if (plcf->redirects == NULL) {
|
|
2526 plcf->redirects = ngx_array_create(cf->pool, 1,
|
|
2527 sizeof(ngx_http_proxy_redirect_t));
|
|
2528 if (plcf->redirects == NULL) {
|
|
2529 return NGX_CONF_ERROR;
|
|
2530 }
|
|
2531 }
|
|
2532
|
|
2533 pr = ngx_array_push(plcf->redirects);
|
|
2534 if (pr == NULL) {
|
|
2535 return NGX_CONF_ERROR;
|
|
2536 }
|
|
2537
|
484
|
2538 if (ngx_strcmp(value[1].data, "default") == 0) {
|
566
|
2539 if (plcf->proxy_lengths) {
|
|
2540 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2541 "\"proxy_redirect default\" may not be used "
|
|
2542 "with \"proxy_pass\" directive with variables");
|
|
2543 return NGX_CONF_ERROR;
|
|
2544 }
|
|
2545
|
348
|
2546 if (plcf->url.data == NULL) {
|
58
|
2547 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
566
|
2548 "\"proxy_redirect default\" must go "
|
58
|
2549 "after the \"proxy_pass\" directive");
|
|
2550 return NGX_CONF_ERROR;
|
|
2551 }
|
|
2552
|
|
2553 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
2554
|
|
2555 if (plcf->vars.uri.len) {
|
614
|
2556 pr->redirect = plcf->url;
|
348
|
2557 pr->replacement.text = plcf->location;
|
202
|
2558
|
|
2559 } else {
|
614
|
2560 pr->redirect.len = plcf->url.len + sizeof("/") - 1;
|
|
2561
|
|
2562 p = ngx_pnalloc(cf->pool, pr->redirect.len);
|
|
2563 if (p == NULL) {
|
|
2564 return NGX_CONF_ERROR;
|
|
2565 }
|
|
2566
|
|
2567 pr->redirect.data = p;
|
|
2568
|
|
2569 p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
|
|
2570 *p = '/';
|
|
2571
|
|
2572 ngx_str_set(&pr->replacement.text, "/");
|
202
|
2573 }
|
58
|
2574
|
|
2575 return NGX_CONF_OK;
|
|
2576 }
|
|
2577
|
|
2578 if (ngx_http_script_variables_count(&value[2]) == 0) {
|
|
2579 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
|
2580 pr->redirect = value[1];
|
|
2581 pr->replacement.text = value[2];
|
|
2582
|
|
2583 return NGX_CONF_OK;
|
|
2584 }
|
|
2585
|
|
2586 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2587
|
|
2588 vars_lengths = NULL;
|
|
2589 vars_values = NULL;
|
|
2590
|
|
2591 sc.cf = cf;
|
|
2592 sc.source = &value[2];
|
|
2593 sc.lengths = &vars_lengths;
|
|
2594 sc.values = &vars_values;
|
|
2595 sc.complete_lengths = 1;
|
|
2596 sc.complete_values = 1;
|
|
2597
|
|
2598 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2599 return NGX_CONF_ERROR;
|
|
2600 }
|
|
2601
|
|
2602 pr->handler = ngx_http_proxy_rewrite_redirect_vars;
|
|
2603 pr->redirect = value[1];
|
|
2604 pr->replacement.vars.lengths = vars_lengths->elts;
|
|
2605 pr->replacement.vars.values = vars_values->elts;
|
|
2606
|
|
2607 return NGX_CONF_OK;
|
|
2608 }
|
|
2609
|
|
2610
|
|
2611 static char *
|
320
|
2612 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2613 {
|
|
2614 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2615
|
|
2616 ngx_str_t *value;
|
|
2617 ngx_http_script_compile_t sc;
|
|
2618
|
496
|
2619 if (plcf->upstream.store != NGX_CONF_UNSET
|
|
2620 || plcf->upstream.store_lengths)
|
320
|
2621 {
|
|
2622 return "is duplicate";
|
|
2623 }
|
|
2624
|
|
2625 value = cf->args->elts;
|
|
2626
|
496
|
2627 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
2628 plcf->upstream.store = 0;
|
320
|
2629 return NGX_CONF_OK;
|
|
2630 }
|
|
2631
|
496
|
2632 #if (NGX_HTTP_CACHE)
|
|
2633
|
|
2634 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
|
|
2635 && plcf->upstream.cache != NULL)
|
|
2636 {
|
|
2637 return "is incompatible with \"proxy_cache\"";
|
|
2638 }
|
|
2639
|
|
2640 #endif
|
|
2641
|
|
2642 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
2643 plcf->upstream.store = 1;
|
320
|
2644 return NGX_CONF_OK;
|
|
2645 }
|
|
2646
|
|
2647 /* include the terminating '\0' into script */
|
|
2648 value[1].len++;
|
|
2649
|
|
2650 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2651
|
|
2652 sc.cf = cf;
|
|
2653 sc.source = &value[1];
|
|
2654 sc.lengths = &plcf->upstream.store_lengths;
|
|
2655 sc.values = &plcf->upstream.store_values;
|
340
|
2656 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
2657 sc.complete_lengths = 1;
|
|
2658 sc.complete_values = 1;
|
|
2659
|
|
2660 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2661 return NGX_CONF_ERROR;
|
|
2662 }
|
|
2663
|
|
2664 return NGX_CONF_OK;
|
|
2665 }
|
|
2666
|
|
2667
|
464
|
2668 #if (NGX_HTTP_CACHE)
|
|
2669
|
|
2670 static char *
|
|
2671 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2672 {
|
|
2673 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2674
|
|
2675 ngx_str_t *value;
|
|
2676
|
|
2677 value = cf->args->elts;
|
|
2678
|
|
2679 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
|
|
2680 return "is duplicate";
|
|
2681 }
|
|
2682
|
|
2683 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
2684 plcf->upstream.cache = NULL;
|
|
2685 return NGX_CONF_OK;
|
|
2686 }
|
|
2687
|
496
|
2688 if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
|
|
2689 return "is incompatible with \"proxy_store\"";
|
|
2690 }
|
|
2691
|
464
|
2692 plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
|
|
2693 &ngx_http_proxy_module);
|
|
2694 if (plcf->upstream.cache == NULL) {
|
|
2695 return NGX_CONF_ERROR;
|
|
2696 }
|
|
2697
|
|
2698 return NGX_CONF_OK;
|
|
2699 }
|
|
2700
|
472
|
2701
|
|
2702 static char *
|
|
2703 ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2704 {
|
|
2705 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2706
|
|
2707 ngx_str_t *value;
|
|
2708 ngx_http_compile_complex_value_t ccv;
|
|
2709
|
|
2710 value = cf->args->elts;
|
|
2711
|
|
2712 if (plcf->cache_key.value.len) {
|
|
2713 return "is duplicate";
|
|
2714 }
|
|
2715
|
|
2716 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
2717
|
|
2718 ccv.cf = cf;
|
|
2719 ccv.value = &value[1];
|
|
2720 ccv.complex_value = &plcf->cache_key;
|
|
2721
|
|
2722 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
2723 return NGX_CONF_ERROR;
|
|
2724 }
|
|
2725
|
|
2726 return NGX_CONF_OK;
|
|
2727 }
|
|
2728
|
464
|
2729 #endif
|
|
2730
|
|
2731
|
320
|
2732 static char *
|
58
|
2733 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
2734 {
|
|
2735 #if (NGX_FREEBSD)
|
|
2736 ssize_t *np = data;
|
|
2737
|
222
|
2738 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
2739 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2740 "\"proxy_send_lowat\" must be less than %d "
|
|
2741 "(sysctl net.inet.tcp.sendspace)",
|
|
2742 ngx_freebsd_net_inet_tcp_sendspace);
|
|
2743
|
|
2744 return NGX_CONF_ERROR;
|
|
2745 }
|
|
2746
|
|
2747 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
2748 ssize_t *np = data;
|
|
2749
|
|
2750 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
2751 "\"proxy_send_lowat\" is not supported, ignored");
|
|
2752
|
|
2753 *np = 0;
|
|
2754
|
|
2755 #endif
|
|
2756
|
|
2757 return NGX_CONF_OK;
|
|
2758 }
|
260
|
2759
|
|
2760
|
348
|
2761 #if (NGX_HTTP_SSL)
|
|
2762
|
|
2763 static ngx_int_t
|
|
2764 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
2765 {
|
|
2766 ngx_pool_cleanup_t *cln;
|
|
2767
|
|
2768 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
2769 if (plcf->upstream.ssl == NULL) {
|
|
2770 return NGX_ERROR;
|
|
2771 }
|
|
2772
|
|
2773 plcf->upstream.ssl->log = cf->log;
|
|
2774
|
|
2775 if (ngx_ssl_create(plcf->upstream.ssl,
|
644
|
2776 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
2777 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2,
|
|
2778 NULL)
|
348
|
2779 != NGX_OK)
|
|
2780 {
|
|
2781 return NGX_ERROR;
|
|
2782 }
|
|
2783
|
|
2784 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
2785 if (cln == NULL) {
|
|
2786 return NGX_ERROR;
|
|
2787 }
|
|
2788
|
|
2789 cln->handler = ngx_ssl_cleanup_ctx;
|
|
2790 cln->data = plcf->upstream.ssl;
|
|
2791
|
|
2792 return NGX_OK;
|
|
2793 }
|
|
2794
|
|
2795 #endif
|
|
2796
|
|
2797
|
464
|
2798 static void
|
|
2799 ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
|
348
|
2800 {
|
400
|
2801 if (u->family != AF_UNIX) {
|
464
|
2802
|
348
|
2803 if (u->no_port || u->port == u->default_port) {
|
464
|
2804
|
348
|
2805 v->host_header = u->host;
|
|
2806
|
|
2807 if (u->default_port == 80) {
|
570
|
2808 ngx_str_set(&v->port, "80");
|
348
|
2809
|
|
2810 } else {
|
570
|
2811 ngx_str_set(&v->port, "443");
|
348
|
2812 }
|
|
2813
|
|
2814 } else {
|
354
|
2815 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
2816 v->host_header.data = u->host.data;
|
|
2817 v->port = u->port_text;
|
348
|
2818 }
|
|
2819
|
464
|
2820 v->key_start.len += v->host_header.len;
|
|
2821
|
348
|
2822 } else {
|
570
|
2823 ngx_str_set(&v->host_header, "localhost");
|
|
2824 ngx_str_null(&v->port);
|
464
|
2825 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
|
348
|
2826 }
|
|
2827
|
|
2828 v->uri = u->uri;
|
|
2829 }
|