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