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