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