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