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