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