58
|
1
|
126
|
2 /*
|
58
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
58
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
662
|
13 typedef struct ngx_http_proxy_rewrite_s ngx_http_proxy_rewrite_t;
|
|
14
|
|
15 typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r,
|
|
16 ngx_table_elt_t *h, size_t prefix, size_t len,
|
|
17 ngx_http_proxy_rewrite_t *pr);
|
|
18
|
|
19 struct ngx_http_proxy_rewrite_s {
|
|
20 ngx_http_proxy_rewrite_pt handler;
|
58
|
21
|
|
22 union {
|
654
|
23 ngx_http_complex_value_t complex;
|
|
24 #if (NGX_PCRE)
|
|
25 ngx_http_regex_t *regex;
|
|
26 #endif
|
662
|
27 } pattern;
|
654
|
28
|
|
29 ngx_http_complex_value_t replacement;
|
58
|
30 };
|
|
31
|
|
32
|
|
33 typedef struct {
|
464
|
34 ngx_str_t key_start;
|
430
|
35 ngx_str_t schema;
|
348
|
36 ngx_str_t host_header;
|
|
37 ngx_str_t port;
|
|
38 ngx_str_t uri;
|
|
39 } ngx_http_proxy_vars_t;
|
|
40
|
|
41
|
|
42 typedef struct {
|
200
|
43 ngx_http_upstream_conf_t upstream;
|
|
44
|
|
45 ngx_array_t *flushes;
|
|
46 ngx_array_t *body_set_len;
|
|
47 ngx_array_t *body_set;
|
|
48 ngx_array_t *headers_set_len;
|
|
49 ngx_array_t *headers_set;
|
|
50 ngx_hash_t headers_set_hash;
|
|
51
|
|
52 ngx_array_t *headers_source;
|
|
53
|
348
|
54 ngx_array_t *proxy_lengths;
|
|
55 ngx_array_t *proxy_values;
|
|
56
|
200
|
57 ngx_array_t *redirects;
|
662
|
58 ngx_array_t *cookie_domains;
|
|
59 ngx_array_t *cookie_paths;
|
200
|
60
|
|
61 ngx_str_t body_source;
|
|
62
|
|
63 ngx_str_t method;
|
348
|
64 ngx_str_t location;
|
|
65 ngx_str_t url;
|
|
66
|
472
|
67 #if (NGX_HTTP_CACHE)
|
|
68 ngx_http_complex_value_t cache_key;
|
|
69 #endif
|
|
70
|
348
|
71 ngx_http_proxy_vars_t vars;
|
200
|
72
|
|
73 ngx_flag_t redirect;
|
298
|
74
|
640
|
75 ngx_uint_t http_version;
|
|
76
|
298
|
77 ngx_uint_t headers_hash_max_size;
|
|
78 ngx_uint_t headers_hash_bucket_size;
|
58
|
79 } ngx_http_proxy_loc_conf_t;
|
|
80
|
|
81
|
|
82 typedef struct {
|
580
|
83 ngx_http_status_t status;
|
692
|
84 ngx_http_chunked_t chunked;
|
348
|
85 ngx_http_proxy_vars_t vars;
|
692
|
86 off_t internal_body_length;
|
640
|
87
|
|
88 ngx_uint_t head; /* unsigned head:1 */
|
58
|
89 } ngx_http_proxy_ctx_t;
|
|
90
|
|
91
|
348
|
92 static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r,
|
|
93 ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf);
|
464
|
94 #if (NGX_HTTP_CACHE)
|
|
95 static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r);
|
|
96 #endif
|
58
|
97 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
|
|
98 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
|
|
99 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
|
|
100 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
|
640
|
101 static ngx_int_t ngx_http_proxy_input_filter_init(void *data);
|
|
102 static ngx_int_t ngx_http_proxy_copy_filter(ngx_event_pipe_t *p,
|
|
103 ngx_buf_t *buf);
|
|
104 static ngx_int_t ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p,
|
|
105 ngx_buf_t *buf);
|
|
106 static ngx_int_t ngx_http_proxy_non_buffered_copy_filter(void *data,
|
|
107 ssize_t bytes);
|
|
108 static ngx_int_t ngx_http_proxy_non_buffered_chunked_filter(void *data,
|
|
109 ssize_t bytes);
|
58
|
110 static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
|
|
111 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
|
|
112 ngx_int_t rc);
|
|
113
|
122
|
114 static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
115 ngx_http_variable_value_t *v, uintptr_t data);
|
|
116 static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
117 ngx_http_variable_value_t *v, uintptr_t data);
|
|
118 static ngx_int_t
|
58
|
119 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
120 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
121 static ngx_int_t
|
|
122 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
123 ngx_http_variable_value_t *v, uintptr_t data);
|
58
|
124 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
|
|
125 ngx_table_elt_t *h, size_t prefix);
|
662
|
126 static ngx_int_t ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r,
|
|
127 ngx_table_elt_t *h);
|
|
128 static ngx_int_t ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r,
|
|
129 ngx_table_elt_t *h, u_char *value, ngx_array_t *rewrites);
|
|
130 static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r,
|
|
131 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement);
|
58
|
132
|
|
133 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
|
|
134 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
|
|
135 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
136 void *parent, void *child);
|
464
|
137 static ngx_int_t ngx_http_proxy_merge_headers(ngx_conf_t *cf,
|
|
138 ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_loc_conf_t *prev);
|
58
|
139
|
|
140 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
141 void *conf);
|
|
142 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
143 void *conf);
|
662
|
144 static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
145 void *conf);
|
|
146 static char *ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
147 void *conf);
|
320
|
148 static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
149 void *conf);
|
464
|
150 #if (NGX_HTTP_CACHE)
|
|
151 static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
152 void *conf);
|
472
|
153 static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
154 void *conf);
|
464
|
155 #endif
|
58
|
156
|
|
157 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
|
158
|
662
|
159 static ngx_int_t ngx_http_proxy_rewrite_regex(ngx_conf_t *cf,
|
|
160 ngx_http_proxy_rewrite_t *pr, ngx_str_t *regex, ngx_uint_t caseless);
|
|
161
|
348
|
162 #if (NGX_HTTP_SSL)
|
|
163 static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf,
|
|
164 ngx_http_proxy_loc_conf_t *plcf);
|
|
165 #endif
|
464
|
166 static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);
|
348
|
167
|
58
|
168
|
|
169 static ngx_conf_post_t ngx_http_proxy_lowat_post =
|
|
170 { ngx_http_proxy_lowat_check };
|
|
171
|
|
172
|
|
173 static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
|
|
174 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
|
|
175 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
|
|
176 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
|
|
177 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
|
412
|
178 { ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 },
|
172
|
179 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
|
412
|
180 { ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 },
|
58
|
181 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
|
498
|
182 { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
|
214
|
183 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
|
58
|
184 { ngx_null_string, 0 }
|
|
185 };
|
|
186
|
|
187
|
640
|
188 static ngx_conf_enum_t ngx_http_proxy_http_version[] = {
|
|
189 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
|
|
190 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
|
|
191 { ngx_null_string, 0 }
|
|
192 };
|
|
193
|
|
194
|
464
|
195 ngx_module_t ngx_http_proxy_module;
|
|
196
|
|
197
|
58
|
198 static ngx_command_t ngx_http_proxy_commands[] = {
|
|
199
|
|
200 { ngx_string("proxy_pass"),
|
256
|
201 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
|
58
|
202 ngx_http_proxy_pass,
|
|
203 NGX_HTTP_LOC_CONF_OFFSET,
|
|
204 0,
|
|
205 NULL },
|
|
206
|
|
207 { ngx_string("proxy_redirect"),
|
|
208 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
209 ngx_http_proxy_redirect,
|
|
210 NGX_HTTP_LOC_CONF_OFFSET,
|
|
211 0,
|
|
212 NULL },
|
|
213
|
662
|
214 { ngx_string("proxy_cookie_domain"),
|
|
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
216 ngx_http_proxy_cookie_domain,
|
|
217 NGX_HTTP_LOC_CONF_OFFSET,
|
|
218 0,
|
|
219 NULL },
|
|
220
|
|
221 { ngx_string("proxy_cookie_path"),
|
|
222 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
223 ngx_http_proxy_cookie_path,
|
|
224 NGX_HTTP_LOC_CONF_OFFSET,
|
|
225 0,
|
|
226 NULL },
|
|
227
|
318
|
228 { ngx_string("proxy_store"),
|
320
|
229 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
230 ngx_http_proxy_store,
|
|
231 NGX_HTTP_LOC_CONF_OFFSET,
|
|
232 0,
|
|
233 NULL },
|
|
234
|
|
235 { ngx_string("proxy_store_access"),
|
318
|
236 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
|
237 ngx_conf_set_access_slot,
|
|
238 NGX_HTTP_LOC_CONF_OFFSET,
|
320
|
239 offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access),
|
318
|
240 NULL },
|
|
241
|
130
|
242 { ngx_string("proxy_buffering"),
|
|
243 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
244 ngx_conf_set_flag_slot,
|
|
245 NGX_HTTP_LOC_CONF_OFFSET,
|
|
246 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
|
|
247 NULL },
|
|
248
|
178
|
249 { ngx_string("proxy_ignore_client_abort"),
|
|
250 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
251 ngx_conf_set_flag_slot,
|
|
252 NGX_HTTP_LOC_CONF_OFFSET,
|
|
253 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
|
|
254 NULL },
|
|
255
|
540
|
256 { ngx_string("proxy_bind"),
|
|
257 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
558
|
258 ngx_http_upstream_bind_set_slot,
|
540
|
259 NGX_HTTP_LOC_CONF_OFFSET,
|
|
260 offsetof(ngx_http_proxy_loc_conf_t, upstream.local),
|
|
261 NULL },
|
|
262
|
58
|
263 { ngx_string("proxy_connect_timeout"),
|
|
264 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
265 ngx_conf_set_msec_slot,
|
|
266 NGX_HTTP_LOC_CONF_OFFSET,
|
|
267 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
|
|
268 NULL },
|
|
269
|
|
270 { ngx_string("proxy_send_timeout"),
|
|
271 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
272 ngx_conf_set_msec_slot,
|
|
273 NGX_HTTP_LOC_CONF_OFFSET,
|
|
274 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
|
|
275 NULL },
|
|
276
|
|
277 { ngx_string("proxy_send_lowat"),
|
|
278 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
279 ngx_conf_set_size_slot,
|
|
280 NGX_HTTP_LOC_CONF_OFFSET,
|
|
281 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
|
|
282 &ngx_http_proxy_lowat_post },
|
|
283
|
206
|
284 { ngx_string("proxy_intercept_errors"),
|
|
285 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
286 ngx_conf_set_flag_slot,
|
|
287 NGX_HTTP_LOC_CONF_OFFSET,
|
|
288 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
|
|
289 NULL },
|
|
290
|
58
|
291 { ngx_string("proxy_set_header"),
|
|
292 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
198
|
293 ngx_conf_set_keyval_slot,
|
58
|
294 NGX_HTTP_LOC_CONF_OFFSET,
|
|
295 offsetof(ngx_http_proxy_loc_conf_t, headers_source),
|
|
296 NULL },
|
|
297
|
298
|
298 { ngx_string("proxy_headers_hash_max_size"),
|
|
299 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
300 ngx_conf_set_num_slot,
|
|
301 NGX_HTTP_LOC_CONF_OFFSET,
|
|
302 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
|
|
303 NULL },
|
|
304
|
|
305 { ngx_string("proxy_headers_hash_bucket_size"),
|
|
306 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
307 ngx_conf_set_num_slot,
|
|
308 NGX_HTTP_LOC_CONF_OFFSET,
|
|
309 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
|
|
310 NULL },
|
|
311
|
126
|
312 { ngx_string("proxy_set_body"),
|
|
313 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
314 ngx_conf_set_str_slot,
|
|
315 NGX_HTTP_LOC_CONF_OFFSET,
|
|
316 offsetof(ngx_http_proxy_loc_conf_t, body_source),
|
|
317 NULL },
|
|
318
|
58
|
319 { ngx_string("proxy_method"),
|
|
320 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
110
|
321 ngx_conf_set_str_slot,
|
58
|
322 NGX_HTTP_LOC_CONF_OFFSET,
|
110
|
323 offsetof(ngx_http_proxy_loc_conf_t, method),
|
|
324 NULL },
|
58
|
325
|
|
326 { ngx_string("proxy_pass_request_headers"),
|
|
327 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
328 ngx_conf_set_flag_slot,
|
|
329 NGX_HTTP_LOC_CONF_OFFSET,
|
|
330 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
|
|
331 NULL },
|
|
332
|
|
333 { ngx_string("proxy_pass_request_body"),
|
|
334 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
335 ngx_conf_set_flag_slot,
|
|
336 NGX_HTTP_LOC_CONF_OFFSET,
|
|
337 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
|
|
338 NULL },
|
|
339
|
130
|
340 { ngx_string("proxy_buffer_size"),
|
|
341 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
342 ngx_conf_set_size_slot,
|
|
343 NGX_HTTP_LOC_CONF_OFFSET,
|
|
344 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
|
|
345 NULL },
|
|
346
|
58
|
347 { ngx_string("proxy_read_timeout"),
|
|
348 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
349 ngx_conf_set_msec_slot,
|
|
350 NGX_HTTP_LOC_CONF_OFFSET,
|
|
351 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
|
|
352 NULL },
|
|
353
|
|
354 { ngx_string("proxy_buffers"),
|
|
355 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
356 ngx_conf_set_bufs_slot,
|
|
357 NGX_HTTP_LOC_CONF_OFFSET,
|
|
358 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
|
|
359 NULL },
|
|
360
|
|
361 { ngx_string("proxy_busy_buffers_size"),
|
|
362 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
363 ngx_conf_set_size_slot,
|
|
364 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
365 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
|
58
|
366 NULL },
|
|
367
|
464
|
368 #if (NGX_HTTP_CACHE)
|
|
369
|
|
370 { ngx_string("proxy_cache"),
|
592
|
371 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
464
|
372 ngx_http_proxy_cache,
|
|
373 NGX_HTTP_LOC_CONF_OFFSET,
|
466
|
374 0,
|
464
|
375 NULL },
|
|
376
|
472
|
377 { ngx_string("proxy_cache_key"),
|
592
|
378 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
472
|
379 ngx_http_proxy_cache_key,
|
|
380 NGX_HTTP_LOC_CONF_OFFSET,
|
|
381 0,
|
|
382 NULL },
|
|
383
|
464
|
384 { ngx_string("proxy_cache_path"),
|
|
385 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
|
|
386 ngx_http_file_cache_set_slot,
|
|
387 0,
|
|
388 0,
|
|
389 &ngx_http_proxy_module },
|
|
390
|
588
|
391 { ngx_string("proxy_cache_bypass"),
|
|
392 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
393 ngx_http_set_predicate_slot,
|
|
394 NGX_HTTP_LOC_CONF_OFFSET,
|
|
395 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass),
|
|
396 NULL },
|
|
397
|
572
|
398 { ngx_string("proxy_no_cache"),
|
|
399 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
588
|
400 ngx_http_set_predicate_slot,
|
572
|
401 NGX_HTTP_LOC_CONF_OFFSET,
|
|
402 offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache),
|
|
403 NULL },
|
|
404
|
464
|
405 { ngx_string("proxy_cache_valid"),
|
|
406 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
407 ngx_http_file_cache_valid_set_slot,
|
|
408 NGX_HTTP_LOC_CONF_OFFSET,
|
|
409 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid),
|
|
410 NULL },
|
|
411
|
|
412 { ngx_string("proxy_cache_min_uses"),
|
|
413 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
414 ngx_conf_set_num_slot,
|
|
415 NGX_HTTP_LOC_CONF_OFFSET,
|
|
416 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
|
|
417 NULL },
|
|
418
|
|
419 { ngx_string("proxy_cache_use_stale"),
|
|
420 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
421 ngx_conf_set_bitmask_slot,
|
|
422 NGX_HTTP_LOC_CONF_OFFSET,
|
|
423 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale),
|
|
424 &ngx_http_proxy_next_upstream_masks },
|
|
425
|
494
|
426 { ngx_string("proxy_cache_methods"),
|
|
427 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
428 ngx_conf_set_bitmask_slot,
|
|
429 NGX_HTTP_LOC_CONF_OFFSET,
|
|
430 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods),
|
|
431 &ngx_http_upstream_cache_method_mask },
|
|
432
|
656
|
433 { ngx_string("proxy_cache_lock"),
|
|
434 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
435 ngx_conf_set_flag_slot,
|
|
436 NGX_HTTP_LOC_CONF_OFFSET,
|
|
437 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock),
|
|
438 NULL },
|
|
439
|
|
440 { ngx_string("proxy_cache_lock_timeout"),
|
|
441 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
442 ngx_conf_set_msec_slot,
|
|
443 NGX_HTTP_LOC_CONF_OFFSET,
|
|
444 offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout),
|
|
445 NULL },
|
|
446
|
464
|
447 #endif
|
|
448
|
58
|
449 { ngx_string("proxy_temp_path"),
|
|
450 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
451 ngx_conf_set_path_slot,
|
|
452 NGX_HTTP_LOC_CONF_OFFSET,
|
|
453 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
|
464
|
454 NULL },
|
58
|
455
|
|
456 { ngx_string("proxy_max_temp_file_size"),
|
|
457 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
458 ngx_conf_set_size_slot,
|
|
459 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
460 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
|
58
|
461 NULL },
|
|
462
|
|
463 { ngx_string("proxy_temp_file_write_size"),
|
|
464 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
465 ngx_conf_set_size_slot,
|
|
466 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
467 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
|
58
|
468 NULL },
|
|
469
|
|
470 { ngx_string("proxy_next_upstream"),
|
96
|
471 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
58
|
472 ngx_conf_set_bitmask_slot,
|
|
473 NGX_HTTP_LOC_CONF_OFFSET,
|
|
474 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
|
|
475 &ngx_http_proxy_next_upstream_masks },
|
|
476
|
198
|
477 { ngx_string("proxy_pass_header"),
|
592
|
478 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
198
|
479 ngx_conf_set_str_array_slot,
|
58
|
480 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
481 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
|
58
|
482 NULL },
|
|
483
|
198
|
484 { ngx_string("proxy_hide_header"),
|
592
|
485 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
198
|
486 ngx_conf_set_str_array_slot,
|
58
|
487 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
488 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
|
58
|
489 NULL },
|
|
490
|
484
|
491 { ngx_string("proxy_ignore_headers"),
|
|
492 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
493 ngx_conf_set_bitmask_slot,
|
|
494 NGX_HTTP_LOC_CONF_OFFSET,
|
|
495 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers),
|
584
|
496 &ngx_http_upstream_ignore_headers_masks },
|
484
|
497
|
640
|
498 { ngx_string("proxy_http_version"),
|
|
499 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
500 ngx_conf_set_enum_slot,
|
|
501 NGX_HTTP_LOC_CONF_OFFSET,
|
|
502 offsetof(ngx_http_proxy_loc_conf_t, http_version),
|
|
503 &ngx_http_proxy_http_version },
|
|
504
|
398
|
505 #if (NGX_HTTP_SSL)
|
|
506
|
|
507 { ngx_string("proxy_ssl_session_reuse"),
|
|
508 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
509 ngx_conf_set_flag_slot,
|
|
510 NGX_HTTP_LOC_CONF_OFFSET,
|
|
511 offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
|
|
512 NULL },
|
|
513
|
|
514 #endif
|
|
515
|
58
|
516 ngx_null_command
|
|
517 };
|
|
518
|
|
519
|
216
|
520 static ngx_http_module_t ngx_http_proxy_module_ctx = {
|
58
|
521 ngx_http_proxy_add_variables, /* preconfiguration */
|
|
522 NULL, /* postconfiguration */
|
|
523
|
|
524 NULL, /* create main configuration */
|
|
525 NULL, /* init main configuration */
|
|
526
|
|
527 NULL, /* create server configuration */
|
|
528 NULL, /* merge server configuration */
|
|
529
|
664
|
530 ngx_http_proxy_create_loc_conf, /* create location configuration */
|
|
531 ngx_http_proxy_merge_loc_conf /* merge location configuration */
|
58
|
532 };
|
|
533
|
|
534
|
|
535 ngx_module_t ngx_http_proxy_module = {
|
|
536 NGX_MODULE_V1,
|
|
537 &ngx_http_proxy_module_ctx, /* module context */
|
|
538 ngx_http_proxy_commands, /* module directives */
|
|
539 NGX_HTTP_MODULE, /* module type */
|
90
|
540 NULL, /* init master */
|
58
|
541 NULL, /* init module */
|
90
|
542 NULL, /* init process */
|
|
543 NULL, /* init thread */
|
|
544 NULL, /* exit thread */
|
|
545 NULL, /* exit process */
|
|
546 NULL, /* exit master */
|
|
547 NGX_MODULE_V1_PADDING
|
58
|
548 };
|
|
549
|
|
550
|
|
551 static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
|
640
|
552 static char ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF;
|
58
|
553
|
|
554
|
198
|
555 static ngx_keyval_t ngx_http_proxy_headers[] = {
|
|
556 { ngx_string("Host"), ngx_string("$proxy_host") },
|
|
557 { ngx_string("Connection"), ngx_string("close") },
|
692
|
558 { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
|
|
559 { ngx_string("Transfer-Encoding"), ngx_string("") },
|
198
|
560 { ngx_string("Keep-Alive"), ngx_string("") },
|
384
|
561 { ngx_string("Expect"), ngx_string("") },
|
640
|
562 { ngx_string("Upgrade"), ngx_string("") },
|
198
|
563 { ngx_null_string, ngx_null_string }
|
|
564 };
|
|
565
|
|
566
|
|
567 static ngx_str_t ngx_http_proxy_hide_headers[] = {
|
|
568 ngx_string("Date"),
|
|
569 ngx_string("Server"),
|
|
570 ngx_string("X-Pad"),
|
|
571 ngx_string("X-Accel-Expires"),
|
|
572 ngx_string("X-Accel-Redirect"),
|
|
573 ngx_string("X-Accel-Limit-Rate"),
|
364
|
574 ngx_string("X-Accel-Buffering"),
|
|
575 ngx_string("X-Accel-Charset"),
|
198
|
576 ngx_null_string
|
58
|
577 };
|
|
578
|
|
579
|
480
|
580 #if (NGX_HTTP_CACHE)
|
|
581
|
|
582 static ngx_keyval_t ngx_http_proxy_cache_headers[] = {
|
|
583 { ngx_string("Host"), ngx_string("$proxy_host") },
|
|
584 { ngx_string("Connection"), ngx_string("close") },
|
692
|
585 { ngx_string("Content-Length"), ngx_string("$proxy_internal_body_length") },
|
|
586 { ngx_string("Transfer-Encoding"), ngx_string("") },
|
480
|
587 { ngx_string("Keep-Alive"), ngx_string("") },
|
|
588 { ngx_string("Expect"), ngx_string("") },
|
648
|
589 { ngx_string("Upgrade"), ngx_string("") },
|
480
|
590 { ngx_string("If-Modified-Since"), ngx_string("") },
|
|
591 { ngx_string("If-Unmodified-Since"), ngx_string("") },
|
558
|
592 { ngx_string("If-None-Match"), ngx_string("") },
|
480
|
593 { ngx_string("If-Match"), ngx_string("") },
|
|
594 { ngx_string("Range"), ngx_string("") },
|
|
595 { ngx_string("If-Range"), ngx_string("") },
|
|
596 { ngx_null_string, ngx_null_string }
|
|
597 };
|
|
598
|
|
599 #endif
|
|
600
|
|
601
|
58
|
602 static ngx_http_variable_t ngx_http_proxy_vars[] = {
|
|
603
|
186
|
604 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
|
366
|
605 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
606
|
186
|
607 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
|
366
|
608 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
609
|
186
|
610 { ngx_string("proxy_add_x_forwarded_for"), NULL,
|
132
|
611 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
612
|
|
613 #if 0
|
186
|
614 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
615 #endif
|
|
616
|
186
|
617 { ngx_string("proxy_internal_body_length"), NULL,
|
132
|
618 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
126
|
619
|
186
|
620 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
58
|
621 };
|
|
622
|
|
623
|
464
|
624 static ngx_path_init_t ngx_http_proxy_temp_path = {
|
|
625 ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 }
|
|
626 };
|
|
627
|
|
628
|
58
|
629 static ngx_int_t
|
|
630 ngx_http_proxy_handler(ngx_http_request_t *r)
|
126
|
631 {
|
58
|
632 ngx_int_t rc;
|
|
633 ngx_http_upstream_t *u;
|
348
|
634 ngx_http_proxy_ctx_t *ctx;
|
58
|
635 ngx_http_proxy_loc_conf_t *plcf;
|
126
|
636
|
510
|
637 if (ngx_http_upstream_create(r) != NGX_OK) {
|
58
|
638 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
639 }
|
126
|
640
|
348
|
641 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
|
|
642 if (ctx == NULL) {
|
|
643 return NGX_ERROR;
|
|
644 }
|
|
645
|
|
646 ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);
|
|
647
|
|
648 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
649
|
510
|
650 u = r->upstream;
|
|
651
|
592
|
652 if (plcf->proxy_lengths == NULL) {
|
348
|
653 ctx->vars = plcf->vars;
|
430
|
654 u->schema = plcf->vars.schema;
|
348
|
655 #if (NGX_HTTP_SSL)
|
|
656 u->ssl = (plcf->upstream.ssl != NULL);
|
|
657 #endif
|
|
658
|
|
659 } else {
|
|
660 if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
|
|
661 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
662 }
|
|
663 }
|
|
664
|
58
|
665 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
666
|
|
667 u->conf = &plcf->upstream;
|
|
668
|
464
|
669 #if (NGX_HTTP_CACHE)
|
|
670 u->create_key = ngx_http_proxy_create_key;
|
|
671 #endif
|
58
|
672 u->create_request = ngx_http_proxy_create_request;
|
|
673 u->reinit_request = ngx_http_proxy_reinit_request;
|
|
674 u->process_header = ngx_http_proxy_process_status_line;
|
|
675 u->abort_request = ngx_http_proxy_abort_request;
|
|
676 u->finalize_request = ngx_http_proxy_finalize_request;
|
566
|
677 r->state = 0;
|
58
|
678
|
|
679 if (plcf->redirects) {
|
|
680 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
|
|
681 }
|
|
682
|
662
|
683 if (plcf->cookie_domains || plcf->cookie_paths) {
|
|
684 u->rewrite_cookie = ngx_http_proxy_rewrite_cookie;
|
|
685 }
|
|
686
|
198
|
687 u->buffering = plcf->upstream.buffering;
|
|
688
|
|
689 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
|
|
690 if (u->pipe == NULL) {
|
|
691 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
130
|
692 }
|
58
|
693
|
640
|
694 u->pipe->input_filter = ngx_http_proxy_copy_filter;
|
|
695 u->pipe->input_ctx = r;
|
|
696
|
|
697 u->input_filter_init = ngx_http_proxy_input_filter_init;
|
|
698 u->input_filter = ngx_http_proxy_non_buffered_copy_filter;
|
|
699 u->input_filter_ctx = r;
|
198
|
700
|
58
|
701 u->accel = 1;
|
126
|
702
|
58
|
703 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
|
|
704
|
|
705 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
706 return rc;
|
|
707 }
|
|
708
|
|
709 return NGX_DONE;
|
|
710 }
|
|
711
|
|
712
|
|
713 static ngx_int_t
|
348
|
714 ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
|
|
715 ngx_http_proxy_loc_conf_t *plcf)
|
|
716 {
|
464
|
717 u_char *p;
|
|
718 size_t add;
|
|
719 u_short port;
|
|
720 ngx_str_t proxy;
|
|
721 ngx_url_t url;
|
|
722 ngx_http_upstream_t *u;
|
348
|
723
|
|
724 if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
|
|
725 plcf->proxy_values->elts)
|
|
726 == NULL)
|
|
727 {
|
|
728 return NGX_ERROR;
|
|
729 }
|
|
730
|
632
|
731 if (proxy.len > 7
|
|
732 && ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0)
|
|
733 {
|
348
|
734 add = 7;
|
|
735 port = 80;
|
|
736
|
|
737 #if (NGX_HTTP_SSL)
|
|
738
|
632
|
739 } else if (proxy.len > 8
|
|
740 && ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0)
|
|
741 {
|
348
|
742 add = 8;
|
|
743 port = 443;
|
|
744 r->upstream->ssl = 1;
|
|
745
|
|
746 #endif
|
|
747
|
|
748 } else {
|
|
749 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
750 "invalid URL prefix in \"%V\"", &proxy);
|
|
751 return NGX_ERROR;
|
|
752 }
|
|
753
|
464
|
754 u = r->upstream;
|
|
755
|
|
756 u->schema.len = add;
|
|
757 u->schema.data = proxy.data;
|
|
758
|
|
759 ngx_memzero(&url, sizeof(ngx_url_t));
|
|
760
|
|
761 url.url.len = proxy.len - add;
|
|
762 url.url.data = proxy.data + add;
|
|
763 url.default_port = port;
|
|
764 url.uri_part = 1;
|
|
765 url.no_resolve = 1;
|
|
766
|
|
767 if (ngx_parse_url(r->pool, &url) != NGX_OK) {
|
|
768 if (url.err) {
|
348
|
769 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
464
|
770 "%s in upstream \"%V\"", url.err, &url.url);
|
348
|
771 }
|
|
772
|
|
773 return NGX_ERROR;
|
|
774 }
|
|
775
|
546
|
776 if (url.uri.len) {
|
|
777 if (url.uri.data[0] == '?') {
|
|
778 p = ngx_pnalloc(r->pool, url.uri.len + 1);
|
|
779 if (p == NULL) {
|
|
780 return NGX_ERROR;
|
|
781 }
|
|
782
|
|
783 *p++ = '/';
|
|
784 ngx_memcpy(p, url.uri.data, url.uri.len);
|
|
785
|
|
786 url.uri.len++;
|
|
787 url.uri.data = p - 1;
|
416
|
788 }
|
|
789 }
|
|
790
|
464
|
791 ctx->vars.key_start = u->schema;
|
|
792
|
|
793 ngx_http_proxy_set_vars(&url, &ctx->vars);
|
|
794
|
|
795 u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
|
|
796 if (u->resolved == NULL) {
|
348
|
797 return NGX_ERROR;
|
|
798 }
|
|
799
|
464
|
800 if (url.addrs && url.addrs[0].sockaddr) {
|
|
801 u->resolved->sockaddr = url.addrs[0].sockaddr;
|
|
802 u->resolved->socklen = url.addrs[0].socklen;
|
|
803 u->resolved->naddrs = 1;
|
|
804 u->resolved->host = url.addrs[0].name;
|
434
|
805
|
|
806 } else {
|
464
|
807 u->resolved->host = url.host;
|
|
808 u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
|
|
809 u->resolved->no_port = url.no_port;
|
434
|
810 }
|
348
|
811
|
|
812 return NGX_OK;
|
|
813 }
|
|
814
|
|
815
|
464
|
816 #if (NGX_HTTP_CACHE)
|
|
817
|
|
818 static ngx_int_t
|
|
819 ngx_http_proxy_create_key(ngx_http_request_t *r)
|
|
820 {
|
|
821 size_t len, loc_len;
|
|
822 u_char *p;
|
|
823 uintptr_t escape;
|
|
824 ngx_str_t *key;
|
|
825 ngx_http_upstream_t *u;
|
|
826 ngx_http_proxy_ctx_t *ctx;
|
|
827 ngx_http_proxy_loc_conf_t *plcf;
|
|
828
|
|
829 u = r->upstream;
|
|
830
|
|
831 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
832
|
|
833 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
834
|
|
835 key = ngx_array_push(&r->cache->keys);
|
|
836 if (key == NULL) {
|
|
837 return NGX_ERROR;
|
|
838 }
|
|
839
|
694
|
840 if (plcf->cache_key.value.data) {
|
472
|
841
|
|
842 if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) {
|
|
843 return NGX_ERROR;
|
|
844 }
|
|
845
|
|
846 return NGX_OK;
|
|
847 }
|
|
848
|
464
|
849 *key = ctx->vars.key_start;
|
|
850
|
|
851 key = ngx_array_push(&r->cache->keys);
|
|
852 if (key == NULL) {
|
|
853 return NGX_ERROR;
|
|
854 }
|
|
855
|
656
|
856 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
464
|
857
|
|
858 *key = ctx->vars.uri;
|
|
859 u->uri = ctx->vars.uri;
|
|
860
|
|
861 return NGX_OK;
|
|
862
|
|
863 } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
|
|
864 {
|
|
865 *key = r->unparsed_uri;
|
|
866 u->uri = r->unparsed_uri;
|
|
867
|
|
868 return NGX_OK;
|
|
869 }
|
|
870
|
|
871 loc_len = (r->valid_location && ctx->vars.uri.len) ? plcf->location.len : 0;
|
|
872
|
|
873 if (r->quoted_uri || r->internal) {
|
|
874 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
|
|
875 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
876 } else {
|
|
877 escape = 0;
|
|
878 }
|
|
879
|
|
880 len = ctx->vars.uri.len + r->uri.len - loc_len + escape
|
|
881 + sizeof("?") - 1 + r->args.len;
|
|
882
|
|
883 p = ngx_pnalloc(r->pool, len);
|
|
884 if (p == NULL) {
|
|
885 return NGX_ERROR;
|
|
886 }
|
|
887
|
|
888 key->data = p;
|
|
889
|
|
890 if (r->valid_location) {
|
|
891 p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
|
|
892 }
|
|
893
|
|
894 if (escape) {
|
|
895 ngx_escape_uri(p, r->uri.data + loc_len,
|
|
896 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
897 p += r->uri.len - loc_len + escape;
|
|
898
|
|
899 } else {
|
|
900 p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len);
|
|
901 }
|
|
902
|
|
903 if (r->args.len > 0) {
|
|
904 *p++ = '?';
|
|
905 p = ngx_copy(p, r->args.data, r->args.len);
|
|
906 }
|
|
907
|
|
908 key->len = p - key->data;
|
|
909 u->uri = *key;
|
|
910
|
|
911 return NGX_OK;
|
|
912 }
|
|
913
|
|
914 #endif
|
|
915
|
|
916
|
348
|
917 static ngx_int_t
|
58
|
918 ngx_http_proxy_create_request(ngx_http_request_t *r)
|
|
919 {
|
464
|
920 size_t len, uri_len, loc_len, body_len;
|
58
|
921 uintptr_t escape;
|
|
922 ngx_buf_t *b;
|
198
|
923 ngx_str_t method;
|
|
924 ngx_uint_t i, unparsed_uri;
|
58
|
925 ngx_chain_t *cl, *body;
|
|
926 ngx_list_part_t *part;
|
|
927 ngx_table_elt_t *header;
|
|
928 ngx_http_upstream_t *u;
|
348
|
929 ngx_http_proxy_ctx_t *ctx;
|
58
|
930 ngx_http_script_code_pt code;
|
|
931 ngx_http_script_engine_t e, le;
|
|
932 ngx_http_proxy_loc_conf_t *plcf;
|
|
933 ngx_http_script_len_code_pt lcode;
|
|
934
|
|
935 u = r->upstream;
|
|
936
|
|
937 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
938
|
110
|
939 if (u->method.len) {
|
|
940 /* HEAD was changed to GET to cache response */
|
|
941 method = u->method;
|
|
942 method.len++;
|
|
943
|
|
944 } else if (plcf->method.len) {
|
|
945 method = plcf->method;
|
|
946
|
58
|
947 } else {
|
110
|
948 method = r->method_name;
|
|
949 method.len++;
|
58
|
950 }
|
|
951
|
640
|
952 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
953
|
|
954 if (method.len == 5
|
|
955 && ngx_strncasecmp(method.data, (u_char *) "HEAD ", 5) == 0)
|
|
956 {
|
|
957 ctx->head = 1;
|
|
958 }
|
|
959
|
352
|
960 len = method.len + sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
|
|
961
|
58
|
962 escape = 0;
|
348
|
963 loc_len = 0;
|
|
964 unparsed_uri = 0;
|
|
965
|
656
|
966 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
464
|
967 uri_len = ctx->vars.uri.len;
|
348
|
968
|
|
969 } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
|
|
970 {
|
158
|
971 unparsed_uri = 1;
|
464
|
972 uri_len = r->unparsed_uri.len;
|
58
|
973
|
|
974 } else {
|
348
|
975 loc_len = (r->valid_location && ctx->vars.uri.len) ?
|
|
976 plcf->location.len : 0;
|
|
977
|
578
|
978 if (r->quoted_uri || r->space_in_uri || r->internal) {
|
58
|
979 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
|
|
980 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
981 }
|
|
982
|
464
|
983 uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape
|
|
984 + sizeof("?") - 1 + r->args.len;
|
58
|
985 }
|
|
986
|
464
|
987 if (uri_len == 0) {
|
|
988 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
989 "zero length URI to proxy");
|
|
990 return NGX_ERROR;
|
|
991 }
|
|
992
|
|
993 len += uri_len;
|
|
994
|
340
|
995 ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes);
|
122
|
996
|
126
|
997 if (plcf->body_set_len) {
|
|
998 le.ip = plcf->body_set_len->elts;
|
|
999 le.request = r;
|
|
1000 le.flushed = 1;
|
|
1001 body_len = 0;
|
|
1002
|
|
1003 while (*(uintptr_t *) le.ip) {
|
|
1004 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1005 body_len += lcode(&le);
|
|
1006 }
|
|
1007
|
348
|
1008 ctx->internal_body_length = body_len;
|
126
|
1009 len += body_len;
|
692
|
1010
|
|
1011 } else {
|
|
1012 ctx->internal_body_length = r->headers_in.content_length_n;
|
126
|
1013 }
|
|
1014
|
58
|
1015 le.ip = plcf->headers_set_len->elts;
|
|
1016 le.request = r;
|
122
|
1017 le.flushed = 1;
|
58
|
1018
|
|
1019 while (*(uintptr_t *) le.ip) {
|
|
1020 while (*(uintptr_t *) le.ip) {
|
|
1021 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1022 len += lcode(&le);
|
|
1023 }
|
|
1024 le.ip += sizeof(uintptr_t);
|
|
1025 }
|
|
1026
|
|
1027
|
|
1028 if (plcf->upstream.pass_request_headers) {
|
|
1029 part = &r->headers_in.headers.part;
|
|
1030 header = part->elts;
|
|
1031
|
|
1032 for (i = 0; /* void */; i++) {
|
|
1033
|
|
1034 if (i >= part->nelts) {
|
|
1035 if (part->next == NULL) {
|
|
1036 break;
|
|
1037 }
|
|
1038
|
|
1039 part = part->next;
|
|
1040 header = part->elts;
|
126
|
1041 i = 0;
|
58
|
1042 }
|
|
1043
|
198
|
1044 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
1045 header[i].lowcase_key, header[i].key.len))
|
58
|
1046 {
|
|
1047 continue;
|
|
1048 }
|
|
1049
|
|
1050 len += header[i].key.len + sizeof(": ") - 1
|
|
1051 + header[i].value.len + sizeof(CRLF) - 1;
|
|
1052 }
|
|
1053 }
|
|
1054
|
|
1055
|
|
1056 b = ngx_create_temp_buf(r->pool, len);
|
|
1057 if (b == NULL) {
|
|
1058 return NGX_ERROR;
|
|
1059 }
|
|
1060
|
|
1061 cl = ngx_alloc_chain_link(r->pool);
|
|
1062 if (cl == NULL) {
|
|
1063 return NGX_ERROR;
|
|
1064 }
|
|
1065
|
|
1066 cl->buf = b;
|
|
1067
|
|
1068
|
|
1069 /* the request line */
|
|
1070
|
122
|
1071 b->last = ngx_copy(b->last, method.data, method.len);
|
58
|
1072
|
104
|
1073 u->uri.data = b->last;
|
|
1074
|
656
|
1075 if (plcf->proxy_lengths && ctx->vars.uri.len) {
|
348
|
1076 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
|
1077
|
|
1078 } else if (unparsed_uri) {
|
122
|
1079 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
|
|
1080
|
58
|
1081 } else {
|
104
|
1082 if (r->valid_location) {
|
348
|
1083 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
104
|
1084 }
|
100
|
1085
|
58
|
1086 if (escape) {
|
|
1087 ngx_escape_uri(b->last, r->uri.data + loc_len,
|
|
1088 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
1089 b->last += r->uri.len - loc_len + escape;
|
|
1090
|
126
|
1091 } else {
|
122
|
1092 b->last = ngx_copy(b->last, r->uri.data + loc_len,
|
|
1093 r->uri.len - loc_len);
|
58
|
1094 }
|
|
1095
|
|
1096 if (r->args.len > 0) {
|
|
1097 *b->last++ = '?';
|
122
|
1098 b->last = ngx_copy(b->last, r->args.data, r->args.len);
|
58
|
1099 }
|
|
1100 }
|
|
1101
|
104
|
1102 u->uri.len = b->last - u->uri.data;
|
|
1103
|
640
|
1104 if (plcf->http_version == NGX_HTTP_VERSION_11) {
|
|
1105 b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,
|
|
1106 sizeof(ngx_http_proxy_version_11) - 1);
|
|
1107
|
|
1108 } else {
|
|
1109 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
|
|
1110 sizeof(ngx_http_proxy_version) - 1);
|
|
1111 }
|
58
|
1112
|
126
|
1113 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
58
|
1114
|
|
1115 e.ip = plcf->headers_set->elts;
|
|
1116 e.pos = b->last;
|
|
1117 e.request = r;
|
122
|
1118 e.flushed = 1;
|
58
|
1119
|
|
1120 le.ip = plcf->headers_set_len->elts;
|
|
1121
|
|
1122 while (*(uintptr_t *) le.ip) {
|
|
1123 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
64
|
1124
|
|
1125 /* skip the header line name length */
|
|
1126 (void) lcode(&le);
|
58
|
1127
|
|
1128 if (*(ngx_http_script_len_code_pt *) le.ip) {
|
|
1129
|
|
1130 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
|
|
1131 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1132 }
|
|
1133
|
|
1134 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
|
|
1135
|
|
1136 } else {
|
|
1137 e.skip = 0;
|
|
1138 }
|
|
1139
|
|
1140 le.ip += sizeof(uintptr_t);
|
|
1141
|
|
1142 while (*(uintptr_t *) e.ip) {
|
|
1143 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1144 code((ngx_http_script_engine_t *) &e);
|
126
|
1145 }
|
58
|
1146 e.ip += sizeof(uintptr_t);
|
|
1147 }
|
|
1148
|
|
1149 b->last = e.pos;
|
|
1150
|
|
1151
|
|
1152 if (plcf->upstream.pass_request_headers) {
|
|
1153 part = &r->headers_in.headers.part;
|
|
1154 header = part->elts;
|
126
|
1155
|
58
|
1156 for (i = 0; /* void */; i++) {
|
|
1157
|
|
1158 if (i >= part->nelts) {
|
|
1159 if (part->next == NULL) {
|
|
1160 break;
|
|
1161 }
|
|
1162
|
|
1163 part = part->next;
|
|
1164 header = part->elts;
|
126
|
1165 i = 0;
|
58
|
1166 }
|
|
1167
|
198
|
1168 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
1169 header[i].lowcase_key, header[i].key.len))
|
58
|
1170 {
|
|
1171 continue;
|
|
1172 }
|
|
1173
|
122
|
1174 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
|
58
|
1175
|
|
1176 *b->last++ = ':'; *b->last++ = ' ';
|
|
1177
|
122
|
1178 b->last = ngx_copy(b->last, header[i].value.data,
|
|
1179 header[i].value.len);
|
58
|
1180
|
|
1181 *b->last++ = CR; *b->last++ = LF;
|
|
1182
|
|
1183 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1184 "http proxy header: \"%V: %V\"",
|
|
1185 &header[i].key, &header[i].value);
|
|
1186 }
|
|
1187 }
|
|
1188
|
126
|
1189
|
58
|
1190 /* add "\r\n" at the header end */
|
|
1191 *b->last++ = CR; *b->last++ = LF;
|
|
1192
|
126
|
1193 if (plcf->body_set) {
|
|
1194 e.ip = plcf->body_set->elts;
|
|
1195 e.pos = b->last;
|
|
1196
|
|
1197 while (*(uintptr_t *) e.ip) {
|
|
1198 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1199 code((ngx_http_script_engine_t *) &e);
|
|
1200 }
|
128
|
1201
|
|
1202 b->last = e.pos;
|
126
|
1203 }
|
|
1204
|
358
|
1205 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1206 "http proxy header:\n\"%*s\"",
|
|
1207 (size_t) (b->last - b->pos), b->pos);
|
58
|
1208
|
126
|
1209 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
|
58
|
1210
|
|
1211 body = u->request_bufs;
|
|
1212 u->request_bufs = cl;
|
|
1213
|
|
1214 while (body) {
|
|
1215 b = ngx_alloc_buf(r->pool);
|
|
1216 if (b == NULL) {
|
|
1217 return NGX_ERROR;
|
|
1218 }
|
|
1219
|
|
1220 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
|
|
1221
|
|
1222 cl->next = ngx_alloc_chain_link(r->pool);
|
|
1223 if (cl->next == NULL) {
|
|
1224 return NGX_ERROR;
|
|
1225 }
|
|
1226
|
|
1227 cl = cl->next;
|
|
1228 cl->buf = b;
|
|
1229
|
|
1230 body = body->next;
|
|
1231 }
|
|
1232
|
|
1233 } else {
|
|
1234 u->request_bufs = cl;
|
|
1235 }
|
|
1236
|
634
|
1237 b->flush = 1;
|
58
|
1238 cl->next = NULL;
|
|
1239
|
|
1240 return NGX_OK;
|
|
1241 }
|
|
1242
|
|
1243
|
|
1244 static ngx_int_t
|
|
1245 ngx_http_proxy_reinit_request(ngx_http_request_t *r)
|
|
1246 {
|
348
|
1247 ngx_http_proxy_ctx_t *ctx;
|
|
1248
|
|
1249 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1250
|
|
1251 if (ctx == NULL) {
|
58
|
1252 return NGX_OK;
|
|
1253 }
|
|
1254
|
580
|
1255 ctx->status.code = 0;
|
|
1256 ctx->status.count = 0;
|
|
1257 ctx->status.start = NULL;
|
|
1258 ctx->status.end = NULL;
|
692
|
1259 ctx->chunked.state = 0;
|
58
|
1260
|
|
1261 r->upstream->process_header = ngx_http_proxy_process_status_line;
|
640
|
1262 r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter;
|
|
1263 r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter;
|
566
|
1264 r->state = 0;
|
58
|
1265
|
|
1266 return NGX_OK;
|
|
1267 }
|
|
1268
|
|
1269
|
|
1270 static ngx_int_t
|
|
1271 ngx_http_proxy_process_status_line(ngx_http_request_t *r)
|
|
1272 {
|
580
|
1273 size_t len;
|
58
|
1274 ngx_int_t rc;
|
|
1275 ngx_http_upstream_t *u;
|
348
|
1276 ngx_http_proxy_ctx_t *ctx;
|
|
1277
|
|
1278 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1279
|
|
1280 if (ctx == NULL) {
|
356
|
1281 return NGX_ERROR;
|
58
|
1282 }
|
|
1283
|
580
|
1284 u = r->upstream;
|
|
1285
|
|
1286 rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status);
|
58
|
1287
|
|
1288 if (rc == NGX_AGAIN) {
|
|
1289 return rc;
|
|
1290 }
|
|
1291
|
580
|
1292 if (rc == NGX_ERROR) {
|
464
|
1293
|
|
1294 #if (NGX_HTTP_CACHE)
|
|
1295
|
|
1296 if (r->cache) {
|
|
1297 r->http_version = NGX_HTTP_VERSION_9;
|
|
1298 return NGX_OK;
|
|
1299 }
|
|
1300
|
|
1301 #endif
|
|
1302
|
58
|
1303 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
126
|
1304 "upstream sent no valid HTTP/1.0 header");
|
58
|
1305
|
168
|
1306 #if 0
|
58
|
1307 if (u->accel) {
|
|
1308 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1309 }
|
168
|
1310 #endif
|
58
|
1311
|
|
1312 r->http_version = NGX_HTTP_VERSION_9;
|
346
|
1313 u->state->status = NGX_HTTP_OK;
|
640
|
1314 u->headers_in.connection_close = 1;
|
126
|
1315
|
58
|
1316 return NGX_OK;
|
|
1317 }
|
|
1318
|
464
|
1319 if (u->state) {
|
580
|
1320 u->state->status = ctx->status.code;
|
464
|
1321 }
|
|
1322
|
580
|
1323 u->headers_in.status_n = ctx->status.code;
|
|
1324
|
|
1325 len = ctx->status.end - ctx->status.start;
|
|
1326 u->headers_in.status_line.len = len;
|
|
1327
|
|
1328 u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
|
78
|
1329 if (u->headers_in.status_line.data == NULL) {
|
356
|
1330 return NGX_ERROR;
|
58
|
1331 }
|
122
|
1332
|
580
|
1333 ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len);
|
58
|
1334
|
|
1335 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1336 "http proxy status %ui \"%V\"",
|
270
|
1337 u->headers_in.status_n, &u->headers_in.status_line);
|
58
|
1338
|
640
|
1339 if (ctx->status.http_version < NGX_HTTP_VERSION_11) {
|
|
1340 u->headers_in.connection_close = 1;
|
|
1341 }
|
|
1342
|
58
|
1343 u->process_header = ngx_http_proxy_process_header;
|
|
1344
|
|
1345 return ngx_http_proxy_process_header(r);
|
|
1346 }
|
|
1347
|
|
1348
|
|
1349 static ngx_int_t
|
|
1350 ngx_http_proxy_process_header(ngx_http_request_t *r)
|
|
1351 {
|
|
1352 ngx_int_t rc;
|
|
1353 ngx_table_elt_t *h;
|
640
|
1354 ngx_http_upstream_t *u;
|
|
1355 ngx_http_proxy_ctx_t *ctx;
|
58
|
1356 ngx_http_upstream_header_t *hh;
|
|
1357 ngx_http_upstream_main_conf_t *umcf;
|
|
1358
|
|
1359 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
|
|
1360
|
378
|
1361 for ( ;; ) {
|
58
|
1362
|
412
|
1363 rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);
|
58
|
1364
|
|
1365 if (rc == NGX_OK) {
|
|
1366
|
|
1367 /* a header line has been parsed successfully */
|
|
1368
|
|
1369 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1370 if (h == NULL) {
|
356
|
1371 return NGX_ERROR;
|
58
|
1372 }
|
|
1373
|
|
1374 h->hash = r->header_hash;
|
|
1375
|
|
1376 h->key.len = r->header_name_end - r->header_name_start;
|
|
1377 h->value.len = r->header_end - r->header_start;
|
|
1378
|
382
|
1379 h->key.data = ngx_pnalloc(r->pool,
|
198
|
1380 h->key.len + 1 + h->value.len + 1 + h->key.len);
|
58
|
1381 if (h->key.data == NULL) {
|
356
|
1382 return NGX_ERROR;
|
58
|
1383 }
|
|
1384
|
|
1385 h->value.data = h->key.data + h->key.len + 1;
|
198
|
1386 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
|
58
|
1387
|
666
|
1388 ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
|
|
1389 h->key.data[h->key.len] = '\0';
|
|
1390 ngx_memcpy(h->value.data, r->header_start, h->value.len);
|
|
1391 h->value.data[h->value.len] = '\0';
|
58
|
1392
|
198
|
1393 if (h->key.len == r->lowcase_index) {
|
|
1394 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1395
|
|
1396 } else {
|
392
|
1397 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
58
|
1398 }
|
|
1399
|
198
|
1400 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
|
|
1401 h->lowcase_key, h->key.len);
|
|
1402
|
|
1403 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
356
|
1404 return NGX_ERROR;
|
198
|
1405 }
|
|
1406
|
58
|
1407 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1408 "http proxy header: \"%V: %V\"",
|
|
1409 &h->key, &h->value);
|
|
1410
|
|
1411 continue;
|
|
1412 }
|
|
1413
|
|
1414 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1415
|
|
1416 /* a whole header has been parsed successfully */
|
|
1417
|
|
1418 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1419 "http proxy header done");
|
|
1420
|
198
|
1421 /*
|
|
1422 * if no "Server" and "Date" in header line,
|
|
1423 * then add the special empty headers
|
|
1424 */
|
|
1425
|
|
1426 if (r->upstream->headers_in.server == NULL) {
|
|
1427 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1428 if (h == NULL) {
|
356
|
1429 return NGX_ERROR;
|
198
|
1430 }
|
|
1431
|
|
1432 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
|
|
1433 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
|
|
1434
|
570
|
1435 ngx_str_set(&h->key, "Server");
|
|
1436 ngx_str_null(&h->value);
|
198
|
1437 h->lowcase_key = (u_char *) "server";
|
|
1438 }
|
|
1439
|
|
1440 if (r->upstream->headers_in.date == NULL) {
|
|
1441 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1442 if (h == NULL) {
|
356
|
1443 return NGX_ERROR;
|
198
|
1444 }
|
|
1445
|
|
1446 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
|
|
1447
|
570
|
1448 ngx_str_set(&h->key, "Date");
|
|
1449 ngx_str_null(&h->value);
|
198
|
1450 h->lowcase_key = (u_char *) "date";
|
|
1451 }
|
|
1452
|
640
|
1453 /* clear content length if response is chunked */
|
|
1454
|
|
1455 u = r->upstream;
|
|
1456
|
|
1457 if (u->headers_in.chunked) {
|
|
1458 u->headers_in.content_length_n = -1;
|
|
1459 }
|
|
1460
|
|
1461 /*
|
|
1462 * set u->keepalive if response has no body; this allows to keep
|
|
1463 * connections alive in case of r->header_only or X-Accel-Redirect
|
|
1464 */
|
|
1465
|
|
1466 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1467
|
|
1468 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1469 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1470 || ctx->head
|
|
1471 || (!u->headers_in.chunked
|
|
1472 && u->headers_in.content_length_n == 0))
|
|
1473 {
|
|
1474 u->keepalive = !u->headers_in.connection_close;
|
|
1475 }
|
|
1476
|
58
|
1477 return NGX_OK;
|
|
1478 }
|
|
1479
|
70
|
1480 if (rc == NGX_AGAIN) {
|
|
1481 return NGX_AGAIN;
|
|
1482 }
|
|
1483
|
58
|
1484 /* there was error while a header line parsing */
|
|
1485
|
|
1486 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
168
|
1487 "upstream sent invalid header");
|
58
|
1488
|
|
1489 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1490 }
|
|
1491 }
|
|
1492
|
|
1493
|
640
|
1494 static ngx_int_t
|
|
1495 ngx_http_proxy_input_filter_init(void *data)
|
|
1496 {
|
|
1497 ngx_http_request_t *r = data;
|
|
1498 ngx_http_upstream_t *u;
|
|
1499 ngx_http_proxy_ctx_t *ctx;
|
|
1500
|
|
1501 u = r->upstream;
|
|
1502 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1503
|
672
|
1504 if (ctx == NULL) {
|
|
1505 return NGX_ERROR;
|
|
1506 }
|
|
1507
|
640
|
1508 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1509 "http proxy filter init s:%d h:%d c:%d l:%O",
|
|
1510 u->headers_in.status_n, ctx->head, u->headers_in.chunked,
|
|
1511 u->headers_in.content_length_n);
|
|
1512
|
|
1513 /* as per RFC2616, 4.4 Message Length */
|
|
1514
|
|
1515 if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
|
|
1516 || u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
|
|
1517 || ctx->head)
|
|
1518 {
|
|
1519 /* 1xx, 204, and 304 and replies to HEAD requests */
|
|
1520 /* no 1xx since we don't send Expect and Upgrade */
|
|
1521
|
|
1522 u->pipe->length = 0;
|
|
1523 u->length = 0;
|
|
1524 u->keepalive = !u->headers_in.connection_close;
|
|
1525
|
|
1526 } else if (u->headers_in.chunked) {
|
|
1527 /* chunked */
|
|
1528
|
|
1529 u->pipe->input_filter = ngx_http_proxy_chunked_filter;
|
|
1530 u->pipe->length = 3; /* "0" LF LF */
|
|
1531
|
|
1532 u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
|
|
1533 u->length = -1;
|
|
1534
|
|
1535 } else if (u->headers_in.content_length_n == 0) {
|
|
1536 /* empty body: special case as filter won't be called */
|
|
1537
|
|
1538 u->pipe->length = 0;
|
|
1539 u->length = 0;
|
|
1540 u->keepalive = !u->headers_in.connection_close;
|
|
1541
|
|
1542 } else {
|
|
1543 /* content length or connection close */
|
|
1544
|
|
1545 u->pipe->length = u->headers_in.content_length_n;
|
|
1546 u->length = u->headers_in.content_length_n;
|
|
1547 }
|
|
1548
|
|
1549 return NGX_OK;
|
|
1550 }
|
|
1551
|
|
1552
|
|
1553 static ngx_int_t
|
|
1554 ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1555 {
|
|
1556 ngx_buf_t *b;
|
|
1557 ngx_chain_t *cl;
|
|
1558 ngx_http_request_t *r;
|
|
1559
|
|
1560 if (buf->pos == buf->last) {
|
|
1561 return NGX_OK;
|
|
1562 }
|
|
1563
|
|
1564 if (p->free) {
|
|
1565 cl = p->free;
|
|
1566 b = cl->buf;
|
|
1567 p->free = cl->next;
|
|
1568 ngx_free_chain(p->pool, cl);
|
|
1569
|
|
1570 } else {
|
|
1571 b = ngx_alloc_buf(p->pool);
|
|
1572 if (b == NULL) {
|
|
1573 return NGX_ERROR;
|
|
1574 }
|
|
1575 }
|
|
1576
|
|
1577 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
1578 b->shadow = buf;
|
|
1579 b->tag = p->tag;
|
|
1580 b->last_shadow = 1;
|
|
1581 b->recycled = 1;
|
|
1582 buf->shadow = b;
|
|
1583
|
|
1584 cl = ngx_alloc_chain_link(p->pool);
|
|
1585 if (cl == NULL) {
|
|
1586 return NGX_ERROR;
|
|
1587 }
|
|
1588
|
|
1589 cl->buf = b;
|
|
1590 cl->next = NULL;
|
|
1591
|
|
1592 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
|
|
1593
|
|
1594 if (p->in) {
|
|
1595 *p->last_in = cl;
|
|
1596 } else {
|
|
1597 p->in = cl;
|
|
1598 }
|
|
1599 p->last_in = &cl->next;
|
|
1600
|
|
1601 if (p->length == -1) {
|
|
1602 return NGX_OK;
|
|
1603 }
|
|
1604
|
|
1605 p->length -= b->last - b->pos;
|
|
1606
|
|
1607 if (p->length == 0) {
|
|
1608 r = p->input_ctx;
|
|
1609 p->upstream_done = 1;
|
|
1610 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1611
|
|
1612 } else if (p->length < 0) {
|
|
1613 r = p->input_ctx;
|
|
1614 p->upstream_done = 1;
|
|
1615
|
642
|
1616 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
694
|
1617 "upstream sent more data than specified in "
|
|
1618 "\"Content-Length\" header");
|
640
|
1619 }
|
|
1620
|
|
1621 return NGX_OK;
|
|
1622 }
|
|
1623
|
|
1624
|
|
1625 static ngx_int_t
|
|
1626 ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
1627 {
|
|
1628 ngx_int_t rc;
|
|
1629 ngx_buf_t *b, **prev;
|
|
1630 ngx_chain_t *cl;
|
|
1631 ngx_http_request_t *r;
|
|
1632 ngx_http_proxy_ctx_t *ctx;
|
|
1633
|
|
1634 if (buf->pos == buf->last) {
|
|
1635 return NGX_OK;
|
|
1636 }
|
|
1637
|
|
1638 r = p->input_ctx;
|
|
1639 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1640
|
672
|
1641 if (ctx == NULL) {
|
|
1642 return NGX_ERROR;
|
|
1643 }
|
|
1644
|
640
|
1645 b = NULL;
|
|
1646 prev = &buf->shadow;
|
|
1647
|
|
1648 for ( ;; ) {
|
|
1649
|
692
|
1650 rc = ngx_http_parse_chunked(r, buf, &ctx->chunked);
|
640
|
1651
|
|
1652 if (rc == NGX_OK) {
|
|
1653
|
|
1654 /* a chunk has been parsed successfully */
|
|
1655
|
|
1656 if (p->free) {
|
|
1657 cl = p->free;
|
|
1658 b = cl->buf;
|
|
1659 p->free = cl->next;
|
|
1660 ngx_free_chain(p->pool, cl);
|
|
1661
|
|
1662 } else {
|
|
1663 b = ngx_alloc_buf(p->pool);
|
|
1664 if (b == NULL) {
|
|
1665 return NGX_ERROR;
|
|
1666 }
|
|
1667 }
|
|
1668
|
|
1669 ngx_memzero(b, sizeof(ngx_buf_t));
|
|
1670
|
|
1671 b->pos = buf->pos;
|
|
1672 b->start = buf->start;
|
|
1673 b->end = buf->end;
|
|
1674 b->tag = p->tag;
|
|
1675 b->temporary = 1;
|
|
1676 b->recycled = 1;
|
|
1677
|
|
1678 *prev = b;
|
|
1679 prev = &b->shadow;
|
|
1680
|
|
1681 cl = ngx_alloc_chain_link(p->pool);
|
|
1682 if (cl == NULL) {
|
|
1683 return NGX_ERROR;
|
|
1684 }
|
|
1685
|
|
1686 cl->buf = b;
|
|
1687 cl->next = NULL;
|
|
1688
|
|
1689 if (p->in) {
|
|
1690 *p->last_in = cl;
|
|
1691 } else {
|
|
1692 p->in = cl;
|
|
1693 }
|
|
1694 p->last_in = &cl->next;
|
|
1695
|
|
1696 /* STUB */ b->num = buf->num;
|
|
1697
|
|
1698 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1699 "input buf #%d %p", b->num, b->pos);
|
|
1700
|
692
|
1701 if (buf->last - buf->pos >= ctx->chunked.size) {
|
|
1702
|
|
1703 buf->pos += ctx->chunked.size;
|
640
|
1704 b->last = buf->pos;
|
692
|
1705 ctx->chunked.size = 0;
|
640
|
1706
|
|
1707 continue;
|
|
1708 }
|
|
1709
|
692
|
1710 ctx->chunked.size -= buf->last - buf->pos;
|
640
|
1711 buf->pos = buf->last;
|
|
1712 b->last = buf->last;
|
|
1713
|
|
1714 continue;
|
|
1715 }
|
|
1716
|
|
1717 if (rc == NGX_DONE) {
|
|
1718
|
|
1719 /* a whole response has been parsed successfully */
|
|
1720
|
|
1721 p->upstream_done = 1;
|
|
1722 r->upstream->keepalive = !r->upstream->headers_in.connection_close;
|
|
1723
|
|
1724 break;
|
|
1725 }
|
|
1726
|
|
1727 if (rc == NGX_AGAIN) {
|
|
1728
|
|
1729 /* set p->length, minimal amount of data we want to see */
|
|
1730
|
692
|
1731 p->length = ctx->chunked.length;
|
640
|
1732
|
|
1733 break;
|
|
1734 }
|
|
1735
|
|
1736 /* invalid response */
|
|
1737
|
642
|
1738 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
1739 "upstream sent invalid chunked response");
|
|
1740
|
|
1741 return NGX_ERROR;
|
|
1742 }
|
|
1743
|
|
1744 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1745 "http proxy chunked state %d, length %d",
|
692
|
1746 ctx->chunked.state, p->length);
|
640
|
1747
|
|
1748 if (b) {
|
|
1749 b->shadow = buf;
|
|
1750 b->last_shadow = 1;
|
|
1751
|
|
1752 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
1753 "input buf %p %z", b->pos, b->last - b->pos);
|
|
1754
|
|
1755 return NGX_OK;
|
|
1756 }
|
|
1757
|
|
1758 /* there is no data record in the buf, add it to free chain */
|
|
1759
|
|
1760 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
|
|
1761 return NGX_ERROR;
|
|
1762 }
|
|
1763
|
|
1764 return NGX_OK;
|
|
1765 }
|
|
1766
|
|
1767
|
|
1768 static ngx_int_t
|
|
1769 ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
|
|
1770 {
|
|
1771 ngx_http_request_t *r = data;
|
|
1772
|
|
1773 ngx_buf_t *b;
|
|
1774 ngx_chain_t *cl, **ll;
|
|
1775 ngx_http_upstream_t *u;
|
|
1776
|
|
1777 u = r->upstream;
|
|
1778
|
|
1779 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
1780 ll = &cl->next;
|
|
1781 }
|
|
1782
|
|
1783 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
1784 if (cl == NULL) {
|
|
1785 return NGX_ERROR;
|
|
1786 }
|
|
1787
|
|
1788 *ll = cl;
|
|
1789
|
|
1790 cl->buf->flush = 1;
|
|
1791 cl->buf->memory = 1;
|
|
1792
|
|
1793 b = &u->buffer;
|
|
1794
|
|
1795 cl->buf->pos = b->last;
|
|
1796 b->last += bytes;
|
|
1797 cl->buf->last = b->last;
|
|
1798 cl->buf->tag = u->output.tag;
|
|
1799
|
|
1800 if (u->length == -1) {
|
|
1801 return NGX_OK;
|
|
1802 }
|
|
1803
|
|
1804 u->length -= bytes;
|
|
1805
|
|
1806 if (u->length == 0) {
|
|
1807 u->keepalive = !u->headers_in.connection_close;
|
|
1808 }
|
|
1809
|
|
1810 return NGX_OK;
|
|
1811 }
|
|
1812
|
|
1813
|
|
1814 static ngx_int_t
|
|
1815 ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
|
|
1816 {
|
|
1817 ngx_http_request_t *r = data;
|
|
1818
|
|
1819 ngx_int_t rc;
|
|
1820 ngx_buf_t *b, *buf;
|
|
1821 ngx_chain_t *cl, **ll;
|
|
1822 ngx_http_upstream_t *u;
|
|
1823 ngx_http_proxy_ctx_t *ctx;
|
|
1824
|
|
1825 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
672
|
1826
|
|
1827 if (ctx == NULL) {
|
|
1828 return NGX_ERROR;
|
|
1829 }
|
|
1830
|
640
|
1831 u = r->upstream;
|
|
1832 buf = &u->buffer;
|
|
1833
|
|
1834 buf->pos = buf->last;
|
|
1835 buf->last += bytes;
|
|
1836
|
|
1837 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
|
|
1838 ll = &cl->next;
|
|
1839 }
|
|
1840
|
|
1841 for ( ;; ) {
|
|
1842
|
692
|
1843 rc = ngx_http_parse_chunked(r, buf, &ctx->chunked);
|
640
|
1844
|
|
1845 if (rc == NGX_OK) {
|
|
1846
|
|
1847 /* a chunk has been parsed successfully */
|
|
1848
|
|
1849 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
|
|
1850 if (cl == NULL) {
|
|
1851 return NGX_ERROR;
|
|
1852 }
|
|
1853
|
|
1854 *ll = cl;
|
|
1855 ll = &cl->next;
|
|
1856
|
|
1857 b = cl->buf;
|
|
1858
|
|
1859 b->flush = 1;
|
|
1860 b->memory = 1;
|
|
1861
|
|
1862 b->pos = buf->pos;
|
|
1863 b->tag = u->output.tag;
|
|
1864
|
692
|
1865 if (buf->last - buf->pos >= ctx->chunked.size) {
|
|
1866 buf->pos += ctx->chunked.size;
|
640
|
1867 b->last = buf->pos;
|
692
|
1868 ctx->chunked.size = 0;
|
640
|
1869
|
|
1870 } else {
|
692
|
1871 ctx->chunked.size -= buf->last - buf->pos;
|
640
|
1872 buf->pos = buf->last;
|
|
1873 b->last = buf->last;
|
|
1874 }
|
|
1875
|
|
1876 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1877 "http proxy out buf %p %z",
|
|
1878 b->pos, b->last - b->pos);
|
|
1879
|
|
1880 continue;
|
|
1881 }
|
|
1882
|
|
1883 if (rc == NGX_DONE) {
|
|
1884
|
|
1885 /* a whole response has been parsed successfully */
|
|
1886
|
|
1887 u->keepalive = !u->headers_in.connection_close;
|
|
1888 u->length = 0;
|
|
1889
|
|
1890 break;
|
|
1891 }
|
|
1892
|
|
1893 if (rc == NGX_AGAIN) {
|
|
1894 break;
|
|
1895 }
|
|
1896
|
|
1897 /* invalid response */
|
|
1898
|
642
|
1899 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
640
|
1900 "upstream sent invalid chunked response");
|
|
1901
|
|
1902 return NGX_ERROR;
|
|
1903 }
|
|
1904
|
|
1905 /* provide continuous buffer for subrequests in memory */
|
|
1906
|
|
1907 if (r->subrequest_in_memory) {
|
|
1908
|
|
1909 cl = u->out_bufs;
|
|
1910
|
|
1911 if (cl) {
|
|
1912 buf->pos = cl->buf->pos;
|
|
1913 }
|
|
1914
|
|
1915 buf->last = buf->pos;
|
|
1916
|
|
1917 for (cl = u->out_bufs; cl; cl = cl->next) {
|
|
1918 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1919 "http proxy in memory %p-%p %uz",
|
|
1920 cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf));
|
|
1921
|
|
1922 if (buf->last == cl->buf->pos) {
|
|
1923 buf->last = cl->buf->last;
|
|
1924 continue;
|
|
1925 }
|
|
1926
|
|
1927 buf->last = ngx_movemem(buf->last, cl->buf->pos,
|
|
1928 cl->buf->last - cl->buf->pos);
|
|
1929
|
|
1930 cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos);
|
|
1931 cl->buf->last = buf->last;
|
|
1932 }
|
|
1933 }
|
|
1934
|
|
1935 return NGX_OK;
|
|
1936 }
|
|
1937
|
|
1938
|
58
|
1939 static void
|
|
1940 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
1941 {
|
|
1942 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1943 "abort http proxy request");
|
126
|
1944
|
58
|
1945 return;
|
|
1946 }
|
|
1947
|
|
1948
|
|
1949 static void
|
|
1950 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
1951 {
|
58
|
1952 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1953 "finalize http proxy request");
|
|
1954
|
|
1955 return;
|
|
1956 }
|
|
1957
|
|
1958
|
122
|
1959 static ngx_int_t
|
|
1960 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
1961 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1962 {
|
348
|
1963 ngx_http_proxy_ctx_t *ctx;
|
|
1964
|
|
1965 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1966
|
|
1967 if (ctx == NULL) {
|
|
1968 v->not_found = 1;
|
|
1969 return NGX_OK;
|
|
1970 }
|
|
1971
|
|
1972 v->len = ctx->vars.host_header.len;
|
126
|
1973 v->valid = 1;
|
340
|
1974 v->no_cacheable = 0;
|
122
|
1975 v->not_found = 0;
|
348
|
1976 v->data = ctx->vars.host_header.data;
|
58
|
1977
|
122
|
1978 return NGX_OK;
|
58
|
1979 }
|
|
1980
|
|
1981
|
122
|
1982 static ngx_int_t
|
|
1983 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
1984 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1985 {
|
348
|
1986 ngx_http_proxy_ctx_t *ctx;
|
|
1987
|
|
1988 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1989
|
|
1990 if (ctx == NULL) {
|
|
1991 v->not_found = 1;
|
|
1992 return NGX_OK;
|
|
1993 }
|
|
1994
|
|
1995 v->len = ctx->vars.port.len;
|
126
|
1996 v->valid = 1;
|
340
|
1997 v->no_cacheable = 0;
|
122
|
1998 v->not_found = 0;
|
348
|
1999 v->data = ctx->vars.port.data;
|
58
|
2000
|
122
|
2001 return NGX_OK;
|
58
|
2002 }
|
|
2003
|
|
2004
|
122
|
2005 static ngx_int_t
|
58
|
2006 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
2007 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
2008 {
|
122
|
2009 u_char *p;
|
58
|
2010
|
126
|
2011 v->valid = 1;
|
340
|
2012 v->no_cacheable = 0;
|
122
|
2013 v->not_found = 0;
|
58
|
2014
|
|
2015 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
2016 v->len = r->connection->addr_text.len;
|
|
2017 v->data = r->connection->addr_text.data;
|
|
2018 return NGX_OK;
|
58
|
2019 }
|
|
2020
|
122
|
2021 v->len = r->headers_in.x_forwarded_for->value.len
|
|
2022 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
2023
|
382
|
2024 p = ngx_pnalloc(r->pool, v->len);
|
58
|
2025 if (p == NULL) {
|
122
|
2026 return NGX_ERROR;
|
58
|
2027 }
|
|
2028
|
122
|
2029 v->data = p;
|
58
|
2030
|
122
|
2031 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
2032 r->headers_in.x_forwarded_for->value.len);
|
58
|
2033
|
|
2034 *p++ = ','; *p++ = ' ';
|
|
2035
|
|
2036 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
2037
|
122
|
2038 return NGX_OK;
|
58
|
2039 }
|
|
2040
|
|
2041
|
|
2042 static ngx_int_t
|
126
|
2043 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
2044 ngx_http_variable_value_t *v, uintptr_t data)
|
|
2045 {
|
348
|
2046 ngx_http_proxy_ctx_t *ctx;
|
|
2047
|
|
2048 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
2049
|
692
|
2050 if (ctx == NULL || ctx->internal_body_length < 0) {
|
126
|
2051 v->not_found = 1;
|
|
2052 return NGX_OK;
|
|
2053 }
|
|
2054
|
|
2055 v->valid = 1;
|
340
|
2056 v->no_cacheable = 0;
|
126
|
2057 v->not_found = 0;
|
|
2058
|
692
|
2059 v->data = ngx_pnalloc(r->connection->pool, NGX_OFF_T_LEN);
|
126
|
2060
|
|
2061 if (v->data == NULL) {
|
|
2062 return NGX_ERROR;
|
|
2063 }
|
|
2064
|
692
|
2065 v->len = ngx_sprintf(v->data, "%O", ctx->internal_body_length) - v->data;
|
126
|
2066
|
|
2067 return NGX_OK;
|
|
2068 }
|
|
2069
|
|
2070
|
|
2071 static ngx_int_t
|
58
|
2072 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2073 size_t prefix)
|
|
2074 {
|
662
|
2075 size_t len;
|
58
|
2076 ngx_int_t rc;
|
|
2077 ngx_uint_t i;
|
662
|
2078 ngx_http_proxy_rewrite_t *pr;
|
58
|
2079 ngx_http_proxy_loc_conf_t *plcf;
|
|
2080
|
|
2081 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2082
|
|
2083 pr = plcf->redirects->elts;
|
|
2084
|
|
2085 if (pr == NULL) {
|
|
2086 return NGX_DECLINED;
|
|
2087 }
|
|
2088
|
662
|
2089 len = h->value.len - prefix;
|
|
2090
|
58
|
2091 for (i = 0; i < plcf->redirects->nelts; i++) {
|
662
|
2092 rc = pr[i].handler(r, h, prefix, len, &pr[i]);
|
|
2093
|
|
2094 if (rc != NGX_DECLINED) {
|
|
2095 return rc;
|
|
2096 }
|
|
2097 }
|
|
2098
|
|
2099 return NGX_DECLINED;
|
|
2100 }
|
|
2101
|
|
2102
|
|
2103 static ngx_int_t
|
|
2104 ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
|
|
2105 {
|
|
2106 size_t prefix;
|
|
2107 u_char *p;
|
|
2108 ngx_int_t rc, rv;
|
|
2109 ngx_http_proxy_loc_conf_t *plcf;
|
|
2110
|
|
2111 p = (u_char *) ngx_strchr(h->value.data, ';');
|
|
2112 if (p == NULL) {
|
|
2113 return NGX_DECLINED;
|
|
2114 }
|
|
2115
|
|
2116 prefix = p + 1 - h->value.data;
|
|
2117
|
|
2118 rv = NGX_DECLINED;
|
|
2119
|
|
2120 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
2121
|
|
2122 if (plcf->cookie_domains) {
|
|
2123 p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1);
|
|
2124
|
|
2125 if (p) {
|
|
2126 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7,
|
|
2127 plcf->cookie_domains);
|
|
2128 if (rc == NGX_ERROR) {
|
|
2129 return NGX_ERROR;
|
|
2130 }
|
|
2131
|
|
2132 if (rc != NGX_DECLINED) {
|
|
2133 rv = rc;
|
|
2134 }
|
|
2135 }
|
|
2136 }
|
|
2137
|
|
2138 if (plcf->cookie_paths) {
|
|
2139 p = ngx_strcasestrn(h->value.data + prefix, "path=", 5 - 1);
|
|
2140
|
|
2141 if (p) {
|
|
2142 rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 5,
|
|
2143 plcf->cookie_paths);
|
|
2144 if (rc == NGX_ERROR) {
|
|
2145 return NGX_ERROR;
|
|
2146 }
|
|
2147
|
|
2148 if (rc != NGX_DECLINED) {
|
|
2149 rv = rc;
|
|
2150 }
|
|
2151 }
|
|
2152 }
|
|
2153
|
|
2154 return rv;
|
|
2155 }
|
|
2156
|
|
2157
|
|
2158 static ngx_int_t
|
|
2159 ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2160 u_char *value, ngx_array_t *rewrites)
|
|
2161 {
|
|
2162 size_t len, prefix;
|
|
2163 u_char *p;
|
|
2164 ngx_int_t rc;
|
|
2165 ngx_uint_t i;
|
|
2166 ngx_http_proxy_rewrite_t *pr;
|
|
2167
|
|
2168 prefix = value - h->value.data;
|
|
2169
|
|
2170 p = (u_char *) ngx_strchr(value, ';');
|
|
2171
|
|
2172 len = p ? (size_t) (p - value) : (h->value.len - prefix);
|
|
2173
|
|
2174 pr = rewrites->elts;
|
|
2175
|
|
2176 for (i = 0; i < rewrites->nelts; i++) {
|
|
2177 rc = pr[i].handler(r, h, prefix, len, &pr[i]);
|
58
|
2178
|
|
2179 if (rc != NGX_DECLINED) {
|
|
2180 return rc;
|
|
2181 }
|
|
2182 }
|
|
2183
|
|
2184 return NGX_DECLINED;
|
|
2185 }
|
|
2186
|
|
2187
|
|
2188 static ngx_int_t
|
662
|
2189 ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r,
|
|
2190 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
58
|
2191 {
|
662
|
2192 ngx_str_t pattern, replacement;
|
|
2193
|
|
2194 if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
|
654
|
2195 return NGX_ERROR;
|
|
2196 }
|
|
2197
|
662
|
2198 if (pattern.len > len
|
|
2199 || ngx_rstrncmp(h->value.data + prefix, pattern.data,
|
|
2200 pattern.len) != 0)
|
58
|
2201 {
|
|
2202 return NGX_DECLINED;
|
|
2203 }
|
|
2204
|
654
|
2205 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
|
2206 return NGX_ERROR;
|
|
2207 }
|
|
2208
|
662
|
2209 return ngx_http_proxy_rewrite(r, h, prefix, pattern.len, &replacement);
|
58
|
2210 }
|
|
2211
|
|
2212
|
654
|
2213 #if (NGX_PCRE)
|
|
2214
|
58
|
2215 static ngx_int_t
|
662
|
2216 ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
2217 size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
58
|
2218 {
|
662
|
2219 ngx_str_t pattern, replacement;
|
|
2220
|
|
2221 pattern.len = len;
|
|
2222 pattern.data = h->value.data + prefix;
|
|
2223
|
|
2224 if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
|
58
|
2225 return NGX_DECLINED;
|
|
2226 }
|
|
2227
|
654
|
2228 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
58
|
2229 return NGX_ERROR;
|
|
2230 }
|
|
2231
|
662
|
2232 if (prefix == 0 && h->value.len == len) {
|
658
|
2233 h->value = replacement;
|
|
2234 return NGX_OK;
|
|
2235 }
|
|
2236
|
662
|
2237 return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
|
|
2238 }
|
|
2239
|
|
2240 #endif
|
|
2241
|
|
2242
|
|
2243 static ngx_int_t
|
|
2244 ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r,
|
|
2245 ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
|
|
2246 {
|
|
2247 u_char *p;
|
|
2248 ngx_str_t pattern, replacement;
|
|
2249
|
|
2250 if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
|
|
2251 return NGX_ERROR;
|
|
2252 }
|
|
2253
|
|
2254 p = h->value.data + prefix;
|
|
2255
|
|
2256 if (p[0] == '.') {
|
|
2257 p++;
|
|
2258 prefix++;
|
|
2259 len--;
|
|
2260 }
|
|
2261
|
|
2262 if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) {
|
|
2263 return NGX_DECLINED;
|
|
2264 }
|
|
2265
|
|
2266 if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
|
658
|
2267 return NGX_ERROR;
|
|
2268 }
|
|
2269
|
662
|
2270 return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
|
|
2271 }
|
|
2272
|
|
2273
|
|
2274 static ngx_int_t
|
|
2275 ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_table_elt_t *h, size_t prefix,
|
|
2276 size_t len, ngx_str_t *replacement)
|
|
2277 {
|
|
2278 u_char *p, *data;
|
|
2279 size_t new_len;
|
|
2280
|
|
2281 new_len = replacement->len + h->value.len - len;
|
|
2282
|
|
2283 if (replacement->len > len) {
|
|
2284
|
|
2285 data = ngx_pnalloc(r->pool, new_len);
|
|
2286 if (data == NULL) {
|
|
2287 return NGX_ERROR;
|
|
2288 }
|
|
2289
|
|
2290 p = ngx_copy(data, h->value.data, prefix);
|
|
2291 p = ngx_copy(p, replacement->data, replacement->len);
|
|
2292
|
|
2293 ngx_memcpy(p, h->value.data + prefix + len,
|
|
2294 h->value.len - len - prefix);
|
|
2295
|
|
2296 h->value.data = data;
|
|
2297
|
|
2298 } else {
|
|
2299 p = ngx_copy(h->value.data + prefix, replacement->data,
|
|
2300 replacement->len);
|
|
2301
|
|
2302 ngx_memmove(p, h->value.data + prefix + len,
|
|
2303 h->value.len - len - prefix);
|
|
2304 }
|
|
2305
|
|
2306 h->value.len = new_len;
|
58
|
2307
|
|
2308 return NGX_OK;
|
|
2309 }
|
|
2310
|
|
2311
|
|
2312 static ngx_int_t
|
|
2313 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
2314 {
|
|
2315 ngx_http_variable_t *var, *v;
|
|
2316
|
|
2317 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
2318 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
2319 if (var == NULL) {
|
|
2320 return NGX_ERROR;
|
|
2321 }
|
|
2322
|
186
|
2323 var->get_handler = v->get_handler;
|
58
|
2324 var->data = v->data;
|
|
2325 }
|
|
2326
|
|
2327 return NGX_OK;
|
|
2328 }
|
|
2329
|
|
2330
|
|
2331 static void *
|
|
2332 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
2333 {
|
|
2334 ngx_http_proxy_loc_conf_t *conf;
|
|
2335
|
|
2336 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
2337 if (conf == NULL) {
|
496
|
2338 return NULL;
|
58
|
2339 }
|
|
2340
|
|
2341 /*
|
|
2342 * set by ngx_pcalloc():
|
|
2343 *
|
|
2344 * conf->upstream.bufs.num = 0;
|
484
|
2345 * conf->upstream.ignore_headers = 0;
|
58
|
2346 * conf->upstream.next_upstream = 0;
|
494
|
2347 * conf->upstream.cache_use_stale = 0;
|
|
2348 * conf->upstream.cache_methods = 0;
|
58
|
2349 * conf->upstream.temp_path = NULL;
|
198
|
2350 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
2351 * conf->upstream.uri = { 0, NULL };
|
|
2352 * conf->upstream.location = NULL;
|
320
|
2353 * conf->upstream.store_lengths = NULL;
|
|
2354 * conf->upstream.store_values = NULL;
|
58
|
2355 *
|
110
|
2356 * conf->method = NULL;
|
58
|
2357 * conf->headers_source = NULL;
|
|
2358 * conf->headers_set_len = NULL;
|
|
2359 * conf->headers_set = NULL;
|
|
2360 * conf->headers_set_hash = NULL;
|
126
|
2361 * conf->body_set_len = NULL;
|
|
2362 * conf->body_set = NULL;
|
|
2363 * conf->body_source = { 0, NULL };
|
566
|
2364 * conf->redirects = NULL;
|
58
|
2365 */
|
126
|
2366
|
320
|
2367 conf->upstream.store = NGX_CONF_UNSET;
|
|
2368 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
2369 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
2370 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
2371
|
58
|
2372 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
2373 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
2374 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
2375
|
|
2376 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
2377 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
2378
|
|
2379 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
2380 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
2381 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
2382
|
|
2383 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
2384 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
2385
|
464
|
2386 #if (NGX_HTTP_CACHE)
|
|
2387 conf->upstream.cache = NGX_CONF_UNSET_PTR;
|
|
2388 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
|
588
|
2389 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
|
572
|
2390 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
|
464
|
2391 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
|
656
|
2392 conf->upstream.cache_lock = NGX_CONF_UNSET;
|
|
2393 conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
|
464
|
2394 #endif
|
|
2395
|
356
|
2396 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
2397 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
2398
|
206
|
2399 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
2400 #if (NGX_HTTP_SSL)
|
|
2401 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
2402 #endif
|
58
|
2403
|
|
2404 /* "proxy_cyclic_temp_file" is disabled */
|
|
2405 conf->upstream.cyclic_temp_file = 0;
|
|
2406
|
|
2407 conf->redirect = NGX_CONF_UNSET;
|
198
|
2408 conf->upstream.change_buffering = 1;
|
58
|
2409
|
662
|
2410 conf->cookie_domains = NGX_CONF_UNSET_PTR;
|
|
2411 conf->cookie_paths = NGX_CONF_UNSET_PTR;
|
|
2412
|
640
|
2413 conf->http_version = NGX_CONF_UNSET_UINT;
|
|
2414
|
298
|
2415 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
2416 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
2417
|
632
|
2418 ngx_str_set(&conf->upstream.module, "proxy");
|
|
2419
|
58
|
2420 return conf;
|
|
2421 }
|
|
2422
|
|
2423
|
|
2424 static char *
|
|
2425 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2426 {
|
|
2427 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
2428 ngx_http_proxy_loc_conf_t *conf = child;
|
|
2429
|
614
|
2430 u_char *p;
|
464
|
2431 size_t size;
|
|
2432 ngx_hash_init_t hash;
|
592
|
2433 ngx_http_core_loc_conf_t *clcf;
|
662
|
2434 ngx_http_proxy_rewrite_t *pr;
|
464
|
2435 ngx_http_script_compile_t sc;
|
58
|
2436
|
320
|
2437 if (conf->upstream.store != 0) {
|
|
2438 ngx_conf_merge_value(conf->upstream.store,
|
496
|
2439 prev->upstream.store, 0);
|
320
|
2440
|
|
2441 if (conf->upstream.store_lengths == NULL) {
|
|
2442 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
2443 conf->upstream.store_values = prev->upstream.store_values;
|
|
2444 }
|
|
2445 }
|
|
2446
|
|
2447 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
2448 prev->upstream.store_access, 0600);
|
318
|
2449
|
130
|
2450 ngx_conf_merge_value(conf->upstream.buffering,
|
|
2451 prev->upstream.buffering, 1);
|
|
2452
|
178
|
2453 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
2454 prev->upstream.ignore_client_abort, 0);
|
|
2455
|
58
|
2456 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
2457 prev->upstream.connect_timeout, 60000);
|
|
2458
|
|
2459 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
2460 prev->upstream.send_timeout, 60000);
|
|
2461
|
|
2462 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
2463 prev->upstream.read_timeout, 60000);
|
|
2464
|
|
2465 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
2466 prev->upstream.send_lowat, 0);
|
|
2467
|
130
|
2468 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
2469 prev->upstream.buffer_size,
|
58
|
2470 (size_t) ngx_pagesize);
|
|
2471
|
|
2472 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
2473 8, ngx_pagesize);
|
|
2474
|
|
2475 if (conf->upstream.bufs.num < 2) {
|
126
|
2476 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
2477 "there must be at least 2 \"proxy_buffers\"");
|
|
2478 return NGX_CONF_ERROR;
|
|
2479 }
|
126
|
2480
|
58
|
2481
|
130
|
2482 size = conf->upstream.buffer_size;
|
126
|
2483 if (size < conf->upstream.bufs.size) {
|
58
|
2484 size = conf->upstream.bufs.size;
|
|
2485 }
|
|
2486
|
|
2487
|
78
|
2488 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
2489 prev->upstream.busy_buffers_size_conf,
|
58
|
2490 NGX_CONF_UNSET_SIZE);
|
|
2491
|
78
|
2492 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2493 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
2494 } else {
|
|
2495 conf->upstream.busy_buffers_size =
|
|
2496 conf->upstream.busy_buffers_size_conf;
|
|
2497 }
|
126
|
2498
|
78
|
2499 if (conf->upstream.busy_buffers_size < size) {
|
58
|
2500 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
2501 "\"proxy_busy_buffers_size\" must be equal to or greater than "
|
|
2502 "the maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2503 "one of the \"proxy_buffers\"");
|
|
2504
|
|
2505 return NGX_CONF_ERROR;
|
78
|
2506 }
|
58
|
2507
|
78
|
2508 if (conf->upstream.busy_buffers_size
|
|
2509 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
2510 {
|
|
2511 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2512 "\"proxy_busy_buffers_size\" must be less than "
|
|
2513 "the size of all \"proxy_buffers\" minus one buffer");
|
|
2514
|
|
2515 return NGX_CONF_ERROR;
|
|
2516 }
|
78
|
2517
|
58
|
2518
|
78
|
2519 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
2520 prev->upstream.temp_file_write_size_conf,
|
58
|
2521 NGX_CONF_UNSET_SIZE);
|
|
2522
|
78
|
2523 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2524 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
2525 } else {
|
|
2526 conf->upstream.temp_file_write_size =
|
|
2527 conf->upstream.temp_file_write_size_conf;
|
|
2528 }
|
126
|
2529
|
78
|
2530 if (conf->upstream.temp_file_write_size < size) {
|
58
|
2531 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
2532 "\"proxy_temp_file_write_size\" must be equal to or greater "
|
|
2533 "than the maximum of the value of \"proxy_buffer_size\" and "
|
58
|
2534 "one of the \"proxy_buffers\"");
|
|
2535
|
|
2536 return NGX_CONF_ERROR;
|
|
2537 }
|
|
2538
|
78
|
2539 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
2540 prev->upstream.max_temp_file_size_conf,
|
58
|
2541 NGX_CONF_UNSET_SIZE);
|
|
2542
|
78
|
2543 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
2544 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
2545 } else {
|
|
2546 conf->upstream.max_temp_file_size =
|
|
2547 conf->upstream.max_temp_file_size_conf;
|
|
2548 }
|
58
|
2549
|
78
|
2550 if (conf->upstream.max_temp_file_size != 0
|
|
2551 && conf->upstream.max_temp_file_size < size)
|
58
|
2552 {
|
|
2553 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
2554 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
672
|
2555 "temporary files usage or must be equal to or greater than "
|
|
2556 "the maximum of the value of \"proxy_buffer_size\" and "
|
110
|
2557 "one of the \"proxy_buffers\"");
|
58
|
2558
|
|
2559 return NGX_CONF_ERROR;
|
|
2560 }
|
|
2561
|
78
|
2562
|
484
|
2563 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
|
|
2564 prev->upstream.ignore_headers,
|
|
2565 NGX_CONF_BITMASK_SET);
|
|
2566
|
|
2567
|
58
|
2568 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
2569 prev->upstream.next_upstream,
|
|
2570 (NGX_CONF_BITMASK_SET
|
|
2571 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
2572 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
2573
|
214
|
2574 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2575 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
2576 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2577 }
|
|
2578
|
464
|
2579 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
|
58
|
2580 prev->upstream.temp_path,
|
464
|
2581 &ngx_http_proxy_temp_path)
|
|
2582 != NGX_OK)
|
|
2583 {
|
|
2584 return NGX_CONF_ERROR;
|
|
2585 }
|
|
2586
|
|
2587
|
|
2588 #if (NGX_HTTP_CACHE)
|
|
2589
|
|
2590 ngx_conf_merge_ptr_value(conf->upstream.cache,
|
|
2591 prev->upstream.cache, NULL);
|
|
2592
|
|
2593 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
|
|
2594 ngx_shm_zone_t *shm_zone;
|
|
2595
|
|
2596 shm_zone = conf->upstream.cache;
|
|
2597
|
|
2598 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2599 "\"proxy_cache\" zone \"%V\" is unknown",
|
480
|
2600 &shm_zone->shm.name);
|
464
|
2601
|
|
2602 return NGX_CONF_ERROR;
|
|
2603 }
|
|
2604
|
|
2605 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
|
|
2606 prev->upstream.cache_min_uses, 1);
|
|
2607
|
|
2608 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
|
|
2609 prev->upstream.cache_use_stale,
|
|
2610 (NGX_CONF_BITMASK_SET
|
|
2611 |NGX_HTTP_UPSTREAM_FT_OFF));
|
|
2612
|
658
|
2613 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
2614 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
|
|
2615 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
2616 }
|
|
2617
|
|
2618 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
|
|
2619 conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
|
|
2620 }
|
|
2621
|
494
|
2622 if (conf->upstream.cache_methods == 0) {
|
|
2623 conf->upstream.cache_methods = prev->upstream.cache_methods;
|
|
2624 }
|
|
2625
|
|
2626 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
|
|
2627
|
588
|
2628 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
|
|
2629 prev->upstream.cache_bypass, NULL);
|
|
2630
|
572
|
2631 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
|
|
2632 prev->upstream.no_cache, NULL);
|
|
2633
|
588
|
2634 if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
|
|
2635 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
|
2636 "\"proxy_no_cache\" functionality has been changed in 0.8.46, "
|
|
2637 "now it should be used together with \"proxy_cache_bypass\"");
|
|
2638 }
|
|
2639
|
464
|
2640 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
|
|
2641 prev->upstream.cache_valid, NULL);
|
|
2642
|
472
|
2643 if (conf->cache_key.value.data == NULL) {
|
|
2644 conf->cache_key = prev->cache_key;
|
|
2645 }
|
|
2646
|
656
|
2647 ngx_conf_merge_value(conf->upstream.cache_lock,
|
|
2648 prev->upstream.cache_lock, 0);
|
|
2649
|
|
2650 ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
|
|
2651 prev->upstream.cache_lock_timeout, 5000);
|
|
2652
|
464
|
2653 #endif
|
58
|
2654
|
110
|
2655 if (conf->method.len == 0) {
|
|
2656 conf->method = prev->method;
|
|
2657
|
|
2658 } else {
|
|
2659 conf->method.data[conf->method.len] = ' ';
|
|
2660 conf->method.len++;
|
58
|
2661 }
|
|
2662
|
|
2663 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
2664 prev->upstream.pass_request_headers, 1);
|
|
2665 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
2666 prev->upstream.pass_request_body, 1);
|
|
2667
|
206
|
2668 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
2669 prev->upstream.intercept_errors, 0);
|
58
|
2670
|
398
|
2671 #if (NGX_HTTP_SSL)
|
|
2672 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
2673 prev->upstream.ssl_session_reuse, 1);
|
|
2674 #endif
|
|
2675
|
58
|
2676 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
2677
|
|
2678 if (conf->redirect) {
|
|
2679
|
|
2680 if (conf->redirects == NULL) {
|
|
2681 conf->redirects = prev->redirects;
|
|
2682 }
|
|
2683
|
348
|
2684 if (conf->redirects == NULL && conf->url.data) {
|
58
|
2685
|
|
2686 conf->redirects = ngx_array_create(cf->pool, 1,
|
662
|
2687 sizeof(ngx_http_proxy_rewrite_t));
|
58
|
2688 if (conf->redirects == NULL) {
|
|
2689 return NGX_CONF_ERROR;
|
|
2690 }
|
|
2691
|
|
2692 pr = ngx_array_push(conf->redirects);
|
|
2693 if (pr == NULL) {
|
|
2694 return NGX_CONF_ERROR;
|
|
2695 }
|
|
2696
|
662
|
2697 ngx_memzero(&pr->pattern.complex,
|
654
|
2698 sizeof(ngx_http_complex_value_t));
|
|
2699
|
|
2700 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
|
2701
|
662
|
2702 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
348
|
2703
|
|
2704 if (conf->vars.uri.len) {
|
662
|
2705 pr->pattern.complex.value = conf->url;
|
654
|
2706 pr->replacement.value = conf->location;
|
202
|
2707
|
|
2708 } else {
|
662
|
2709 pr->pattern.complex.value.len = conf->url.len
|
|
2710 + sizeof("/") - 1;
|
|
2711
|
|
2712 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
|
614
|
2713 if (p == NULL) {
|
|
2714 return NGX_CONF_ERROR;
|
|
2715 }
|
|
2716
|
662
|
2717 pr->pattern.complex.value.data = p;
|
614
|
2718
|
|
2719 p = ngx_cpymem(p, conf->url.data, conf->url.len);
|
|
2720 *p = '/';
|
|
2721
|
654
|
2722 ngx_str_set(&pr->replacement.value, "/");
|
202
|
2723 }
|
58
|
2724 }
|
|
2725 }
|
|
2726
|
662
|
2727 ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL);
|
|
2728
|
|
2729 ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL);
|
|
2730
|
482
|
2731 #if (NGX_HTTP_SSL)
|
|
2732 if (conf->upstream.ssl == NULL) {
|
|
2733 conf->upstream.ssl = prev->upstream.ssl;
|
|
2734 }
|
|
2735 #endif
|
|
2736
|
640
|
2737 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
|
|
2738 NGX_HTTP_VERSION_10);
|
|
2739
|
298
|
2740 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
2741 prev->headers_hash_max_size, 512);
|
|
2742
|
|
2743 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
2744 prev->headers_hash_bucket_size, 64);
|
|
2745
|
|
2746 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
2747 ngx_cacheline_size);
|
|
2748
|
356
|
2749 hash.max_size = conf->headers_hash_max_size;
|
|
2750 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2751 hash.name = "proxy_headers_hash";
|
|
2752
|
|
2753 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
584
|
2754 &prev->upstream, ngx_http_proxy_hide_headers, &hash)
|
198
|
2755 != NGX_OK)
|
|
2756 {
|
|
2757 return NGX_CONF_ERROR;
|
|
2758 }
|
|
2759
|
260
|
2760 if (conf->upstream.upstream == NULL) {
|
|
2761 conf->upstream.upstream = prev->upstream.upstream;
|
348
|
2762 conf->vars = prev->vars;
|
58
|
2763 }
|
|
2764
|
592
|
2765 if (conf->proxy_lengths == NULL) {
|
|
2766 conf->proxy_lengths = prev->proxy_lengths;
|
|
2767 conf->proxy_values = prev->proxy_values;
|
|
2768 }
|
|
2769
|
|
2770 if (conf->upstream.upstream || conf->proxy_lengths) {
|
|
2771 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2772 if (clcf->handler == NULL && clcf->lmt_excpt) {
|
|
2773 clcf->handler = ngx_http_proxy_handler;
|
|
2774 conf->location = prev->location;
|
|
2775 }
|
|
2776 }
|
126
|
2777
|
|
2778 if (conf->body_source.data == NULL) {
|
|
2779 conf->body_source = prev->body_source;
|
|
2780 conf->body_set_len = prev->body_set_len;
|
|
2781 conf->body_set = prev->body_set;
|
|
2782 }
|
|
2783
|
128
|
2784 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
2785
|
|
2786 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2787
|
|
2788 sc.cf = cf;
|
|
2789 sc.source = &conf->body_source;
|
|
2790 sc.flushes = &conf->flushes;
|
|
2791 sc.lengths = &conf->body_set_len;
|
|
2792 sc.values = &conf->body_set;
|
|
2793 sc.complete_lengths = 1;
|
|
2794 sc.complete_values = 1;
|
|
2795
|
|
2796 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2797 return NGX_CONF_ERROR;
|
|
2798 }
|
|
2799 }
|
|
2800
|
464
|
2801 if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
|
|
2802 return NGX_CONF_ERROR;
|
|
2803 }
|
|
2804
|
|
2805 return NGX_CONF_OK;
|
|
2806 }
|
|
2807
|
|
2808
|
|
2809 static ngx_int_t
|
|
2810 ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
|
|
2811 ngx_http_proxy_loc_conf_t *prev)
|
|
2812 {
|
|
2813 u_char *p;
|
|
2814 size_t size;
|
|
2815 uintptr_t *code;
|
|
2816 ngx_uint_t i;
|
648
|
2817 ngx_array_t headers_names, headers_merged;
|
464
|
2818 ngx_keyval_t *src, *s, *h;
|
|
2819 ngx_hash_key_t *hk;
|
|
2820 ngx_hash_init_t hash;
|
|
2821 ngx_http_script_compile_t sc;
|
|
2822 ngx_http_script_copy_code_t *copy;
|
126
|
2823
|
58
|
2824 if (conf->headers_source == NULL) {
|
122
|
2825 conf->flushes = prev->flushes;
|
58
|
2826 conf->headers_set_len = prev->headers_set_len;
|
|
2827 conf->headers_set = prev->headers_set;
|
|
2828 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
2829 conf->headers_source = prev->headers_source;
|
58
|
2830 }
|
|
2831
|
498
|
2832 if (conf->headers_set_hash.buckets
|
502
|
2833 #if (NGX_HTTP_CACHE)
|
|
2834 && ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
|
|
2835 #endif
|
|
2836 )
|
498
|
2837 {
|
464
|
2838 return NGX_OK;
|
58
|
2839 }
|
|
2840
|
|
2841
|
576
|
2842 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
|
|
2843 != NGX_OK)
|
|
2844 {
|
464
|
2845 return NGX_ERROR;
|
58
|
2846 }
|
|
2847
|
648
|
2848 if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
|
|
2849 != NGX_OK)
|
|
2850 {
|
|
2851 return NGX_ERROR;
|
|
2852 }
|
|
2853
|
58
|
2854 if (conf->headers_source == NULL) {
|
|
2855 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
2856 sizeof(ngx_keyval_t));
|
58
|
2857 if (conf->headers_source == NULL) {
|
464
|
2858 return NGX_ERROR;
|
58
|
2859 }
|
|
2860 }
|
|
2861
|
|
2862 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
2863 if (conf->headers_set_len == NULL) {
|
464
|
2864 return NGX_ERROR;
|
58
|
2865 }
|
|
2866
|
|
2867 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
2868 if (conf->headers_set == NULL) {
|
464
|
2869 return NGX_ERROR;
|
58
|
2870 }
|
|
2871
|
|
2872
|
480
|
2873 #if (NGX_HTTP_CACHE)
|
|
2874
|
|
2875 h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
|
|
2876 ngx_http_proxy_headers;
|
|
2877 #else
|
|
2878
|
|
2879 h = ngx_http_proxy_headers;
|
|
2880
|
|
2881 #endif
|
|
2882
|
648
|
2883 src = conf->headers_source->elts;
|
|
2884 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2885
|
|
2886 s = ngx_array_push(&headers_merged);
|
|
2887 if (s == NULL) {
|
|
2888 return NGX_ERROR;
|
|
2889 }
|
|
2890
|
|
2891 *s = src[i];
|
|
2892 }
|
|
2893
|
480
|
2894 while (h->key.len) {
|
58
|
2895
|
648
|
2896 src = headers_merged.elts;
|
|
2897 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
2898 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
2899 goto next;
|
|
2900 }
|
|
2901 }
|
|
2902
|
648
|
2903 s = ngx_array_push(&headers_merged);
|
58
|
2904 if (s == NULL) {
|
464
|
2905 return NGX_ERROR;
|
58
|
2906 }
|
|
2907
|
|
2908 *s = *h;
|
|
2909
|
|
2910 next:
|
|
2911
|
480
|
2912 h++;
|
58
|
2913 }
|
|
2914
|
648
|
2915
|
|
2916 src = headers_merged.elts;
|
|
2917 for (i = 0; i < headers_merged.nelts; i++) {
|
58
|
2918
|
576
|
2919 hk = ngx_array_push(&headers_names);
|
198
|
2920 if (hk == NULL) {
|
464
|
2921 return NGX_ERROR;
|
58
|
2922 }
|
|
2923
|
198
|
2924 hk->key = src[i].key;
|
|
2925 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
2926 hk->value = (void *) 1;
|
58
|
2927
|
68
|
2928 if (src[i].value.len == 0) {
|
|
2929 continue;
|
|
2930 }
|
|
2931
|
58
|
2932 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
2933 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2934 sizeof(ngx_http_script_copy_code_t));
|
|
2935 if (copy == NULL) {
|
464
|
2936 return NGX_ERROR;
|
58
|
2937 }
|
|
2938
|
|
2939 copy->code = (ngx_http_script_code_pt)
|
|
2940 ngx_http_script_copy_len_code;
|
|
2941 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2942 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2943
|
|
2944
|
|
2945 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2946 + src[i].key.len + sizeof(": ") - 1
|
|
2947 + src[i].value.len + sizeof(CRLF) - 1
|
|
2948 + sizeof(uintptr_t) - 1)
|
|
2949 & ~(sizeof(uintptr_t) - 1);
|
|
2950
|
|
2951 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2952 if (copy == NULL) {
|
464
|
2953 return NGX_ERROR;
|
58
|
2954 }
|
|
2955
|
|
2956 copy->code = ngx_http_script_copy_code;
|
|
2957 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2958 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2959
|
|
2960 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2961
|
|
2962 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2963 *p++ = ':'; *p++ = ' ';
|
|
2964 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
2965 *p++ = CR; *p = LF;
|
|
2966
|
|
2967 } else {
|
|
2968 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2969 sizeof(ngx_http_script_copy_code_t));
|
|
2970 if (copy == NULL) {
|
464
|
2971 return NGX_ERROR;
|
58
|
2972 }
|
|
2973
|
|
2974 copy->code = (ngx_http_script_code_pt)
|
|
2975 ngx_http_script_copy_len_code;
|
|
2976 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2977
|
|
2978
|
|
2979 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2980 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
2981 & ~(sizeof(uintptr_t) - 1);
|
|
2982
|
|
2983 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2984 if (copy == NULL) {
|
464
|
2985 return NGX_ERROR;
|
58
|
2986 }
|
|
2987
|
|
2988 copy->code = ngx_http_script_copy_code;
|
|
2989 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2990
|
|
2991 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2992 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2993 *p++ = ':'; *p = ' ';
|
|
2994
|
|
2995
|
|
2996 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2997
|
|
2998 sc.cf = cf;
|
|
2999 sc.source = &src[i].value;
|
122
|
3000 sc.flushes = &conf->flushes;
|
58
|
3001 sc.lengths = &conf->headers_set_len;
|
|
3002 sc.values = &conf->headers_set;
|
|
3003
|
|
3004 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
464
|
3005 return NGX_ERROR;
|
58
|
3006 }
|
|
3007
|
|
3008
|
|
3009 copy = ngx_array_push_n(conf->headers_set_len,
|
|
3010 sizeof(ngx_http_script_copy_code_t));
|
|
3011 if (copy == NULL) {
|
464
|
3012 return NGX_ERROR;
|
58
|
3013 }
|
|
3014
|
|
3015 copy->code = (ngx_http_script_code_pt)
|
|
3016 ngx_http_script_copy_len_code;
|
|
3017 copy->len = sizeof(CRLF) - 1;
|
|
3018
|
|
3019
|
|
3020 size = (sizeof(ngx_http_script_copy_code_t)
|
|
3021 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
3022 & ~(sizeof(uintptr_t) - 1);
|
|
3023
|
|
3024 copy = ngx_array_push_n(conf->headers_set, size);
|
|
3025 if (copy == NULL) {
|
464
|
3026 return NGX_ERROR;
|
58
|
3027 }
|
|
3028
|
|
3029 copy->code = ngx_http_script_copy_code;
|
|
3030 copy->len = sizeof(CRLF) - 1;
|
|
3031
|
|
3032 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
3033 *p++ = CR; *p = LF;
|
|
3034 }
|
|
3035
|
|
3036 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3037 if (code == NULL) {
|
464
|
3038 return NGX_ERROR;
|
58
|
3039 }
|
|
3040
|
|
3041 *code = (uintptr_t) NULL;
|
|
3042
|
|
3043 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
3044 if (code == NULL) {
|
464
|
3045 return NGX_ERROR;
|
58
|
3046 }
|
|
3047
|
|
3048 *code = (uintptr_t) NULL;
|
|
3049 }
|
|
3050
|
|
3051 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
3052 if (code == NULL) {
|
464
|
3053 return NGX_ERROR;
|
58
|
3054 }
|
|
3055
|
|
3056 *code = (uintptr_t) NULL;
|
|
3057
|
|
3058
|
198
|
3059 hash.hash = &conf->headers_set_hash;
|
|
3060 hash.key = ngx_hash_key_lc;
|
298
|
3061 hash.max_size = conf->headers_hash_max_size;
|
|
3062 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
3063 hash.name = "proxy_headers_hash";
|
198
|
3064 hash.pool = cf->pool;
|
|
3065 hash.temp_pool = NULL;
|
|
3066
|
576
|
3067 return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
|
58
|
3068 }
|
|
3069
|
|
3070
|
|
3071 static char *
|
|
3072 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3073 {
|
|
3074 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3075
|
348
|
3076 size_t add;
|
|
3077 u_short port;
|
|
3078 ngx_str_t *value, *url;
|
|
3079 ngx_url_t u;
|
|
3080 ngx_uint_t n;
|
|
3081 ngx_http_core_loc_conf_t *clcf;
|
|
3082 ngx_http_script_compile_t sc;
|
58
|
3083
|
430
|
3084 if (plcf->upstream.upstream || plcf->proxy_lengths) {
|
104
|
3085 return "is duplicate";
|
|
3086 }
|
|
3087
|
348
|
3088 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
3089
|
510
|
3090 clcf->handler = ngx_http_proxy_handler;
|
|
3091
|
|
3092 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
3093 clcf->auto_redirect = 1;
|
|
3094 }
|
|
3095
|
58
|
3096 value = cf->args->elts;
|
|
3097
|
|
3098 url = &value[1];
|
|
3099
|
348
|
3100 n = ngx_http_script_variables_count(url);
|
|
3101
|
|
3102 if (n) {
|
|
3103
|
|
3104 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3105
|
|
3106 sc.cf = cf;
|
|
3107 sc.source = url;
|
|
3108 sc.lengths = &plcf->proxy_lengths;
|
|
3109 sc.values = &plcf->proxy_values;
|
|
3110 sc.variables = n;
|
|
3111 sc.complete_lengths = 1;
|
|
3112 sc.complete_values = 1;
|
354
|
3113
|
348
|
3114 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3115 return NGX_CONF_ERROR;
|
|
3116 }
|
|
3117
|
|
3118 #if (NGX_HTTP_SSL)
|
|
3119 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3120 return NGX_CONF_ERROR;
|
|
3121 }
|
|
3122 #endif
|
|
3123
|
|
3124 return NGX_CONF_OK;
|
|
3125 }
|
|
3126
|
286
|
3127 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
3128 add = 7;
|
140
|
3129 port = 80;
|
126
|
3130
|
286
|
3131 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
3132
|
|
3133 #if (NGX_HTTP_SSL)
|
348
|
3134 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
3135 return NGX_CONF_ERROR;
|
|
3136 }
|
126
|
3137
|
|
3138 add = 8;
|
140
|
3139 port = 443;
|
126
|
3140 #else
|
|
3141 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3142 "https protocol requires SSL support");
|
|
3143 return NGX_CONF_ERROR;
|
|
3144 #endif
|
|
3145
|
|
3146 } else {
|
58
|
3147 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
3148 return NGX_CONF_ERROR;
|
|
3149 }
|
|
3150
|
200
|
3151 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
3152
|
|
3153 u.url.len = url->len - add;
|
|
3154 u.url.data = url->data + add;
|
266
|
3155 u.default_port = port;
|
|
3156 u.uri_part = 1;
|
260
|
3157 u.no_resolve = 1;
|
|
3158
|
|
3159 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
3160 if (plcf->upstream.upstream == NULL) {
|
58
|
3161 return NGX_CONF_ERROR;
|
|
3162 }
|
|
3163
|
430
|
3164 plcf->vars.schema.len = add;
|
|
3165 plcf->vars.schema.data = url->data;
|
464
|
3166 plcf->vars.key_start = plcf->vars.schema;
|
|
3167
|
|
3168 ngx_http_proxy_set_vars(&u, &plcf->vars);
|
58
|
3169
|
464
|
3170 plcf->location = clcf->name;
|
|
3171
|
324
|
3172 if (clcf->named
|
58
|
3173 #if (NGX_PCRE)
|
324
|
3174 || clcf->regex
|
|
3175 #endif
|
|
3176 || clcf->noname)
|
|
3177 {
|
348
|
3178 if (plcf->vars.uri.len) {
|
122
|
3179 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
3180 "\"proxy_pass\" cannot have URI part in "
|
256
|
3181 "location given by regular expression, "
|
324
|
3182 "or inside named location, "
|
672
|
3183 "or inside \"if\" statement, "
|
|
3184 "or inside \"limit_except\" block");
|
122
|
3185 return NGX_CONF_ERROR;
|
|
3186 }
|
|
3187
|
348
|
3188 plcf->location.len = 0;
|
104
|
3189 }
|
|
3190
|
348
|
3191 plcf->url = *url;
|
58
|
3192
|
|
3193 return NGX_CONF_OK;
|
|
3194 }
|
|
3195
|
|
3196
|
|
3197 static char *
|
|
3198 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3199 {
|
|
3200 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3201
|
654
|
3202 u_char *p;
|
|
3203 ngx_str_t *value;
|
662
|
3204 ngx_http_proxy_rewrite_t *pr;
|
654
|
3205 ngx_http_compile_complex_value_t ccv;
|
58
|
3206
|
|
3207 if (plcf->redirect == 0) {
|
|
3208 return NGX_CONF_OK;
|
|
3209 }
|
|
3210
|
660
|
3211 plcf->redirect = 1;
|
|
3212
|
58
|
3213 value = cf->args->elts;
|
|
3214
|
484
|
3215 if (cf->args->nelts == 2) {
|
|
3216 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3217 plcf->redirect = 0;
|
|
3218 plcf->redirects = NULL;
|
|
3219 return NGX_CONF_OK;
|
|
3220 }
|
|
3221
|
|
3222 if (ngx_strcmp(value[1].data, "false") == 0) {
|
|
3223 ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
|
|
3224 "invalid parameter \"false\", use \"off\" instead");
|
|
3225 plcf->redirect = 0;
|
|
3226 plcf->redirects = NULL;
|
|
3227 return NGX_CONF_OK;
|
|
3228 }
|
|
3229
|
|
3230 if (ngx_strcmp(value[1].data, "default") != 0) {
|
|
3231 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3232 "invalid parameter \"%V\"", &value[1]);
|
|
3233 return NGX_CONF_ERROR;
|
|
3234 }
|
58
|
3235 }
|
|
3236
|
|
3237 if (plcf->redirects == NULL) {
|
|
3238 plcf->redirects = ngx_array_create(cf->pool, 1,
|
662
|
3239 sizeof(ngx_http_proxy_rewrite_t));
|
58
|
3240 if (plcf->redirects == NULL) {
|
|
3241 return NGX_CONF_ERROR;
|
|
3242 }
|
|
3243 }
|
|
3244
|
|
3245 pr = ngx_array_push(plcf->redirects);
|
|
3246 if (pr == NULL) {
|
|
3247 return NGX_CONF_ERROR;
|
|
3248 }
|
|
3249
|
484
|
3250 if (ngx_strcmp(value[1].data, "default") == 0) {
|
566
|
3251 if (plcf->proxy_lengths) {
|
|
3252 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
3253 "\"proxy_redirect default\" cannot be used "
|
566
|
3254 "with \"proxy_pass\" directive with variables");
|
|
3255 return NGX_CONF_ERROR;
|
|
3256 }
|
|
3257
|
348
|
3258 if (plcf->url.data == NULL) {
|
58
|
3259 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
672
|
3260 "\"proxy_redirect default\" should be placed "
|
58
|
3261 "after the \"proxy_pass\" directive");
|
|
3262 return NGX_CONF_ERROR;
|
|
3263 }
|
|
3264
|
662
|
3265 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
|
3266
|
|
3267 ngx_memzero(&pr->pattern.complex, sizeof(ngx_http_complex_value_t));
|
654
|
3268
|
|
3269 ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
|
348
|
3270
|
|
3271 if (plcf->vars.uri.len) {
|
662
|
3272 pr->pattern.complex.value = plcf->url;
|
654
|
3273 pr->replacement.value = plcf->location;
|
202
|
3274
|
|
3275 } else {
|
662
|
3276 pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1;
|
|
3277
|
|
3278 p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
|
614
|
3279 if (p == NULL) {
|
|
3280 return NGX_CONF_ERROR;
|
|
3281 }
|
|
3282
|
662
|
3283 pr->pattern.complex.value.data = p;
|
614
|
3284
|
|
3285 p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
|
|
3286 *p = '/';
|
|
3287
|
654
|
3288 ngx_str_set(&pr->replacement.value, "/");
|
202
|
3289 }
|
58
|
3290
|
|
3291 return NGX_CONF_OK;
|
|
3292 }
|
|
3293
|
654
|
3294
|
|
3295 if (value[1].data[0] == '~') {
|
|
3296 value[1].len--;
|
|
3297 value[1].data++;
|
|
3298
|
|
3299 if (value[1].data[0] == '*') {
|
|
3300 value[1].len--;
|
|
3301 value[1].data++;
|
662
|
3302
|
|
3303 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3304 return NGX_CONF_ERROR;
|
|
3305 }
|
|
3306
|
|
3307 } else {
|
|
3308 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
|
|
3309 return NGX_CONF_ERROR;
|
|
3310 }
|
654
|
3311 }
|
|
3312
|
|
3313 } else {
|
|
3314
|
|
3315 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3316
|
|
3317 ccv.cf = cf;
|
|
3318 ccv.value = &value[1];
|
662
|
3319 ccv.complex_value = &pr->pattern.complex;
|
654
|
3320
|
|
3321 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3322 return NGX_CONF_ERROR;
|
|
3323 }
|
|
3324
|
662
|
3325 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
58
|
3326 }
|
|
3327
|
654
|
3328
|
|
3329 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3330
|
|
3331 ccv.cf = cf;
|
|
3332 ccv.value = &value[2];
|
|
3333 ccv.complex_value = &pr->replacement;
|
|
3334
|
|
3335 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
58
|
3336 return NGX_CONF_ERROR;
|
|
3337 }
|
|
3338
|
|
3339 return NGX_CONF_OK;
|
|
3340 }
|
|
3341
|
|
3342
|
|
3343 static char *
|
662
|
3344 ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3345 {
|
|
3346 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3347
|
|
3348 ngx_str_t *value;
|
|
3349 ngx_http_proxy_rewrite_t *pr;
|
|
3350 ngx_http_compile_complex_value_t ccv;
|
|
3351
|
|
3352 if (plcf->cookie_domains == NULL) {
|
|
3353 return NGX_CONF_OK;
|
|
3354 }
|
|
3355
|
|
3356 value = cf->args->elts;
|
|
3357
|
|
3358 if (cf->args->nelts == 2) {
|
|
3359
|
|
3360 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3361 plcf->cookie_domains = NULL;
|
|
3362 return NGX_CONF_OK;
|
|
3363 }
|
|
3364
|
|
3365 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3366 "invalid parameter \"%V\"", &value[1]);
|
|
3367 return NGX_CONF_ERROR;
|
|
3368 }
|
|
3369
|
|
3370 if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) {
|
|
3371 plcf->cookie_domains = ngx_array_create(cf->pool, 1,
|
|
3372 sizeof(ngx_http_proxy_rewrite_t));
|
|
3373 if (plcf->cookie_domains == NULL) {
|
|
3374 return NGX_CONF_ERROR;
|
|
3375 }
|
|
3376 }
|
|
3377
|
|
3378 pr = ngx_array_push(plcf->cookie_domains);
|
|
3379 if (pr == NULL) {
|
|
3380 return NGX_CONF_ERROR;
|
|
3381 }
|
|
3382
|
|
3383 if (value[1].data[0] == '~') {
|
|
3384 value[1].len--;
|
|
3385 value[1].data++;
|
|
3386
|
|
3387 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3388 return NGX_CONF_ERROR;
|
|
3389 }
|
|
3390
|
|
3391 } else {
|
|
3392
|
|
3393 if (value[1].data[0] == '.') {
|
|
3394 value[1].len--;
|
|
3395 value[1].data++;
|
|
3396 }
|
|
3397
|
|
3398 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3399
|
|
3400 ccv.cf = cf;
|
|
3401 ccv.value = &value[1];
|
|
3402 ccv.complex_value = &pr->pattern.complex;
|
|
3403
|
|
3404 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3405 return NGX_CONF_ERROR;
|
|
3406 }
|
|
3407
|
|
3408 pr->handler = ngx_http_proxy_rewrite_domain_handler;
|
|
3409
|
|
3410 if (value[2].data[0] == '.') {
|
|
3411 value[2].len--;
|
|
3412 value[2].data++;
|
|
3413 }
|
|
3414 }
|
|
3415
|
|
3416 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3417
|
|
3418 ccv.cf = cf;
|
|
3419 ccv.value = &value[2];
|
|
3420 ccv.complex_value = &pr->replacement;
|
|
3421
|
|
3422 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3423 return NGX_CONF_ERROR;
|
|
3424 }
|
|
3425
|
|
3426 return NGX_CONF_OK;
|
|
3427 }
|
|
3428
|
|
3429
|
|
3430 static char *
|
|
3431 ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3432 {
|
|
3433 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3434
|
|
3435 ngx_str_t *value;
|
|
3436 ngx_http_proxy_rewrite_t *pr;
|
|
3437 ngx_http_compile_complex_value_t ccv;
|
|
3438
|
|
3439 if (plcf->cookie_paths == NULL) {
|
|
3440 return NGX_CONF_OK;
|
|
3441 }
|
|
3442
|
|
3443 value = cf->args->elts;
|
|
3444
|
|
3445 if (cf->args->nelts == 2) {
|
|
3446
|
|
3447 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3448 plcf->cookie_paths = NULL;
|
|
3449 return NGX_CONF_OK;
|
|
3450 }
|
|
3451
|
|
3452 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3453 "invalid parameter \"%V\"", &value[1]);
|
|
3454 return NGX_CONF_ERROR;
|
|
3455 }
|
|
3456
|
|
3457 if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
|
|
3458 plcf->cookie_paths = ngx_array_create(cf->pool, 1,
|
|
3459 sizeof(ngx_http_proxy_rewrite_t));
|
|
3460 if (plcf->cookie_paths == NULL) {
|
|
3461 return NGX_CONF_ERROR;
|
|
3462 }
|
|
3463 }
|
|
3464
|
|
3465 pr = ngx_array_push(plcf->cookie_paths);
|
|
3466 if (pr == NULL) {
|
|
3467 return NGX_CONF_ERROR;
|
|
3468 }
|
|
3469
|
|
3470 if (value[1].data[0] == '~') {
|
|
3471 value[1].len--;
|
|
3472 value[1].data++;
|
|
3473
|
|
3474 if (value[1].data[0] == '*') {
|
|
3475 value[1].len--;
|
|
3476 value[1].data++;
|
|
3477
|
|
3478 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
|
|
3479 return NGX_CONF_ERROR;
|
|
3480 }
|
|
3481
|
|
3482 } else {
|
|
3483 if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
|
|
3484 return NGX_CONF_ERROR;
|
|
3485 }
|
|
3486 }
|
|
3487
|
|
3488 } else {
|
|
3489
|
|
3490 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3491
|
|
3492 ccv.cf = cf;
|
|
3493 ccv.value = &value[1];
|
|
3494 ccv.complex_value = &pr->pattern.complex;
|
|
3495
|
|
3496 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3497 return NGX_CONF_ERROR;
|
|
3498 }
|
|
3499
|
|
3500 pr->handler = ngx_http_proxy_rewrite_complex_handler;
|
|
3501 }
|
|
3502
|
|
3503 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3504
|
|
3505 ccv.cf = cf;
|
|
3506 ccv.value = &value[2];
|
|
3507 ccv.complex_value = &pr->replacement;
|
|
3508
|
|
3509 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3510 return NGX_CONF_ERROR;
|
|
3511 }
|
|
3512
|
|
3513 return NGX_CONF_OK;
|
|
3514 }
|
|
3515
|
|
3516
|
|
3517 static ngx_int_t
|
|
3518 ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
|
|
3519 ngx_str_t *regex, ngx_uint_t caseless)
|
|
3520 {
|
|
3521 #if (NGX_PCRE)
|
|
3522 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
3523 ngx_regex_compile_t rc;
|
|
3524
|
|
3525 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
|
|
3526
|
|
3527 rc.pattern = *regex;
|
|
3528 rc.err.len = NGX_MAX_CONF_ERRSTR;
|
|
3529 rc.err.data = errstr;
|
|
3530
|
|
3531 if (caseless) {
|
|
3532 rc.options = NGX_REGEX_CASELESS;
|
|
3533 }
|
|
3534
|
|
3535 pr->pattern.regex = ngx_http_regex_compile(cf, &rc);
|
|
3536 if (pr->pattern.regex == NULL) {
|
|
3537 return NGX_ERROR;
|
|
3538 }
|
|
3539
|
|
3540 pr->handler = ngx_http_proxy_rewrite_regex_handler;
|
|
3541
|
|
3542 return NGX_OK;
|
|
3543
|
|
3544 #else
|
|
3545
|
|
3546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3547 "using regex \"%V\" requires PCRE library", regex);
|
|
3548 return NGX_ERROR;
|
|
3549
|
|
3550 #endif
|
|
3551 }
|
|
3552
|
|
3553
|
|
3554 static char *
|
320
|
3555 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3556 {
|
|
3557 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3558
|
|
3559 ngx_str_t *value;
|
|
3560 ngx_http_script_compile_t sc;
|
|
3561
|
496
|
3562 if (plcf->upstream.store != NGX_CONF_UNSET
|
|
3563 || plcf->upstream.store_lengths)
|
320
|
3564 {
|
|
3565 return "is duplicate";
|
|
3566 }
|
|
3567
|
|
3568 value = cf->args->elts;
|
|
3569
|
496
|
3570 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3571 plcf->upstream.store = 0;
|
320
|
3572 return NGX_CONF_OK;
|
|
3573 }
|
|
3574
|
496
|
3575 #if (NGX_HTTP_CACHE)
|
|
3576
|
|
3577 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
|
|
3578 && plcf->upstream.cache != NULL)
|
|
3579 {
|
|
3580 return "is incompatible with \"proxy_cache\"";
|
|
3581 }
|
|
3582
|
|
3583 #endif
|
|
3584
|
|
3585 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
3586 plcf->upstream.store = 1;
|
320
|
3587 return NGX_CONF_OK;
|
|
3588 }
|
|
3589
|
|
3590 /* include the terminating '\0' into script */
|
|
3591 value[1].len++;
|
|
3592
|
|
3593 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
3594
|
|
3595 sc.cf = cf;
|
|
3596 sc.source = &value[1];
|
|
3597 sc.lengths = &plcf->upstream.store_lengths;
|
|
3598 sc.values = &plcf->upstream.store_values;
|
340
|
3599 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
3600 sc.complete_lengths = 1;
|
|
3601 sc.complete_values = 1;
|
|
3602
|
|
3603 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
3604 return NGX_CONF_ERROR;
|
|
3605 }
|
|
3606
|
|
3607 return NGX_CONF_OK;
|
|
3608 }
|
|
3609
|
|
3610
|
464
|
3611 #if (NGX_HTTP_CACHE)
|
|
3612
|
|
3613 static char *
|
|
3614 ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3615 {
|
|
3616 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3617
|
|
3618 ngx_str_t *value;
|
|
3619
|
|
3620 value = cf->args->elts;
|
|
3621
|
|
3622 if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
|
|
3623 return "is duplicate";
|
|
3624 }
|
|
3625
|
|
3626 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
3627 plcf->upstream.cache = NULL;
|
|
3628 return NGX_CONF_OK;
|
|
3629 }
|
|
3630
|
496
|
3631 if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
|
|
3632 return "is incompatible with \"proxy_store\"";
|
|
3633 }
|
|
3634
|
464
|
3635 plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
|
|
3636 &ngx_http_proxy_module);
|
|
3637 if (plcf->upstream.cache == NULL) {
|
|
3638 return NGX_CONF_ERROR;
|
|
3639 }
|
|
3640
|
|
3641 return NGX_CONF_OK;
|
|
3642 }
|
|
3643
|
472
|
3644
|
|
3645 static char *
|
|
3646 ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
3647 {
|
|
3648 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
3649
|
|
3650 ngx_str_t *value;
|
|
3651 ngx_http_compile_complex_value_t ccv;
|
|
3652
|
|
3653 value = cf->args->elts;
|
|
3654
|
694
|
3655 if (plcf->cache_key.value.data) {
|
472
|
3656 return "is duplicate";
|
|
3657 }
|
|
3658
|
|
3659 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
3660
|
|
3661 ccv.cf = cf;
|
|
3662 ccv.value = &value[1];
|
|
3663 ccv.complex_value = &plcf->cache_key;
|
|
3664
|
|
3665 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
3666 return NGX_CONF_ERROR;
|
|
3667 }
|
|
3668
|
|
3669 return NGX_CONF_OK;
|
|
3670 }
|
|
3671
|
464
|
3672 #endif
|
|
3673
|
|
3674
|
320
|
3675 static char *
|
58
|
3676 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
3677 {
|
|
3678 #if (NGX_FREEBSD)
|
|
3679 ssize_t *np = data;
|
|
3680
|
222
|
3681 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
3682 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
3683 "\"proxy_send_lowat\" must be less than %d "
|
|
3684 "(sysctl net.inet.tcp.sendspace)",
|
|
3685 ngx_freebsd_net_inet_tcp_sendspace);
|
|
3686
|
|
3687 return NGX_CONF_ERROR;
|
|
3688 }
|
|
3689
|
|
3690 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
3691 ssize_t *np = data;
|
|
3692
|
|
3693 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
3694 "\"proxy_send_lowat\" is not supported, ignored");
|
|
3695
|
|
3696 *np = 0;
|
|
3697
|
|
3698 #endif
|
|
3699
|
|
3700 return NGX_CONF_OK;
|
|
3701 }
|
260
|
3702
|
|
3703
|
348
|
3704 #if (NGX_HTTP_SSL)
|
|
3705
|
|
3706 static ngx_int_t
|
|
3707 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
3708 {
|
|
3709 ngx_pool_cleanup_t *cln;
|
|
3710
|
|
3711 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
3712 if (plcf->upstream.ssl == NULL) {
|
|
3713 return NGX_ERROR;
|
|
3714 }
|
|
3715
|
|
3716 plcf->upstream.ssl->log = cf->log;
|
|
3717
|
|
3718 if (ngx_ssl_create(plcf->upstream.ssl,
|
658
|
3719 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
3720 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2,
|
|
3721 NULL)
|
348
|
3722 != NGX_OK)
|
|
3723 {
|
|
3724 return NGX_ERROR;
|
|
3725 }
|
|
3726
|
|
3727 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
3728 if (cln == NULL) {
|
|
3729 return NGX_ERROR;
|
|
3730 }
|
|
3731
|
|
3732 cln->handler = ngx_ssl_cleanup_ctx;
|
|
3733 cln->data = plcf->upstream.ssl;
|
|
3734
|
|
3735 return NGX_OK;
|
|
3736 }
|
|
3737
|
|
3738 #endif
|
|
3739
|
|
3740
|
464
|
3741 static void
|
|
3742 ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
|
348
|
3743 {
|
400
|
3744 if (u->family != AF_UNIX) {
|
464
|
3745
|
348
|
3746 if (u->no_port || u->port == u->default_port) {
|
464
|
3747
|
348
|
3748 v->host_header = u->host;
|
|
3749
|
|
3750 if (u->default_port == 80) {
|
570
|
3751 ngx_str_set(&v->port, "80");
|
348
|
3752
|
|
3753 } else {
|
570
|
3754 ngx_str_set(&v->port, "443");
|
348
|
3755 }
|
|
3756
|
|
3757 } else {
|
354
|
3758 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
3759 v->host_header.data = u->host.data;
|
|
3760 v->port = u->port_text;
|
348
|
3761 }
|
|
3762
|
464
|
3763 v->key_start.len += v->host_header.len;
|
|
3764
|
348
|
3765 } else {
|
570
|
3766 ngx_str_set(&v->host_header, "localhost");
|
|
3767 ngx_str_null(&v->port);
|
464
|
3768 v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;
|
348
|
3769 }
|
|
3770
|
|
3771 v->uri = u->uri;
|
|
3772 }
|