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