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