0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10 #include <ngx_http_proxy_handler.h>
|
|
11
|
|
12
|
|
13 static ngx_int_t ngx_http_proxy_handler(ngx_http_request_t *r);
|
26
|
14 static ngx_int_t ngx_http_proxy_cache_get(ngx_http_proxy_ctx_t *p);
|
0
|
15
|
28
|
16 static size_t ngx_http_proxy_log_proxy_state_getlen(ngx_http_request_t *r,
|
|
17 uintptr_t data);
|
0
|
18 static u_char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r,
|
28
|
19 u_char *buf,
|
|
20 ngx_http_log_op_t *op);
|
|
21
|
|
22 #if 0
|
0
|
23 static u_char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r,
|
|
24 u_char *buf, uintptr_t data);
|
|
25 static u_char *ngx_http_proxy_log_reason(ngx_http_request_t *r, u_char *buf,
|
|
26 uintptr_t data);
|
28
|
27 #endif
|
0
|
28
|
28
|
29 static ngx_int_t ngx_http_proxy_add_log_formats(ngx_conf_t *cf);
|
0
|
30 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
|
|
31 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
32 void *parent, void *child);
|
|
33
|
|
34 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
35 void *conf);
|
|
36 static char *ngx_http_proxy_parse_upstream(ngx_str_t *url,
|
|
37 ngx_http_proxy_upstream_conf_t *u);
|
|
38
|
2
|
39 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
|
40
|
|
41 static ngx_conf_post_t ngx_http_proxy_lowat_post =
|
|
42 { ngx_http_proxy_lowat_check } ;
|
|
43
|
0
|
44
|
|
45 static ngx_conf_bitmask_t next_upstream_masks[] = {
|
|
46 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR },
|
|
47 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT },
|
|
48 { ngx_string("invalid_header"), NGX_HTTP_PROXY_FT_INVALID_HEADER },
|
|
49 { ngx_string("http_500"), NGX_HTTP_PROXY_FT_HTTP_500 },
|
|
50 { ngx_string("http_404"), NGX_HTTP_PROXY_FT_HTTP_404 },
|
|
51 { ngx_null_string, 0 }
|
|
52 };
|
|
53
|
|
54
|
|
55 static ngx_conf_bitmask_t use_stale_masks[] = {
|
|
56 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR },
|
|
57 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT },
|
|
58 { ngx_string("invalid_header"), NGX_HTTP_PROXY_FT_INVALID_HEADER },
|
|
59 { ngx_string("http_500"), NGX_HTTP_PROXY_FT_HTTP_500 },
|
|
60 { ngx_string("busy_lock"), NGX_HTTP_PROXY_FT_BUSY_LOCK },
|
|
61 { ngx_string("max_waiting"), NGX_HTTP_PROXY_FT_MAX_WAITING },
|
|
62 { ngx_null_string, 0 }
|
|
63 };
|
|
64
|
|
65
|
|
66 static ngx_conf_num_bounds_t ngx_http_proxy_lm_factor_bounds = {
|
|
67 ngx_conf_check_num_bounds, 0, 100
|
|
68 };
|
|
69
|
|
70
|
|
71 static ngx_command_t ngx_http_proxy_commands[] = {
|
|
72
|
|
73 { ngx_string("proxy_pass"),
|
|
74 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
75 ngx_http_proxy_set_pass,
|
|
76 NGX_HTTP_LOC_CONF_OFFSET,
|
|
77 0,
|
|
78 NULL },
|
|
79
|
|
80 { ngx_string("proxy_connect_timeout"),
|
|
81 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
82 ngx_conf_set_msec_slot,
|
|
83 NGX_HTTP_LOC_CONF_OFFSET,
|
|
84 offsetof(ngx_http_proxy_loc_conf_t, connect_timeout),
|
|
85 NULL },
|
|
86
|
|
87 { ngx_string("proxy_send_timeout"),
|
|
88 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
89 ngx_conf_set_msec_slot,
|
|
90 NGX_HTTP_LOC_CONF_OFFSET,
|
|
91 offsetof(ngx_http_proxy_loc_conf_t, send_timeout),
|
|
92 NULL },
|
|
93
|
2
|
94 { ngx_string("proxy_send_lowat"),
|
|
95 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
96 ngx_conf_set_size_slot,
|
|
97 NGX_HTTP_LOC_CONF_OFFSET,
|
|
98 offsetof(ngx_http_proxy_loc_conf_t, send_lowat),
|
|
99 &ngx_http_proxy_lowat_post },
|
|
100
|
0
|
101 { ngx_string("proxy_preserve_host"),
|
|
102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
103 ngx_conf_set_flag_slot,
|
|
104 NGX_HTTP_LOC_CONF_OFFSET,
|
|
105 offsetof(ngx_http_proxy_loc_conf_t, preserve_host),
|
|
106 NULL },
|
|
107
|
6
|
108 { ngx_string("proxy_set_x_url"),
|
|
109 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
110 ngx_conf_set_flag_slot,
|
|
111 NGX_HTTP_LOC_CONF_OFFSET,
|
|
112 offsetof(ngx_http_proxy_loc_conf_t, set_x_url),
|
|
113 NULL },
|
|
114
|
0
|
115 { ngx_string("proxy_set_x_real_ip"),
|
|
116 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
117 ngx_conf_set_flag_slot,
|
|
118 NGX_HTTP_LOC_CONF_OFFSET,
|
|
119 offsetof(ngx_http_proxy_loc_conf_t, set_x_real_ip),
|
|
120 NULL },
|
|
121
|
|
122 { ngx_string("proxy_add_x_forwarded_for"),
|
|
123 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
124 ngx_conf_set_flag_slot,
|
|
125 NGX_HTTP_LOC_CONF_OFFSET,
|
|
126 offsetof(ngx_http_proxy_loc_conf_t, add_x_forwarded_for),
|
|
127 NULL },
|
|
128
|
|
129 { ngx_string("proxy_header_buffer_size"),
|
|
130 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
131 ngx_conf_set_size_slot,
|
|
132 NGX_HTTP_LOC_CONF_OFFSET,
|
|
133 offsetof(ngx_http_proxy_loc_conf_t, header_buffer_size),
|
|
134 NULL },
|
|
135
|
|
136 { ngx_string("proxy_read_timeout"),
|
|
137 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
138 ngx_conf_set_msec_slot,
|
|
139 NGX_HTTP_LOC_CONF_OFFSET,
|
|
140 offsetof(ngx_http_proxy_loc_conf_t, read_timeout),
|
|
141 NULL },
|
|
142
|
|
143 { ngx_string("proxy_buffers"),
|
|
144 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
145 ngx_conf_set_bufs_slot,
|
|
146 NGX_HTTP_LOC_CONF_OFFSET,
|
|
147 offsetof(ngx_http_proxy_loc_conf_t, bufs),
|
|
148 NULL },
|
|
149
|
|
150 { ngx_string("proxy_busy_buffers_size"),
|
|
151 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
152 ngx_conf_set_size_slot,
|
|
153 NGX_HTTP_LOC_CONF_OFFSET,
|
|
154 offsetof(ngx_http_proxy_loc_conf_t, busy_buffers_size),
|
|
155 NULL },
|
|
156
|
26
|
157 #if 0
|
0
|
158
|
|
159 { ngx_string("proxy_cache_path"),
|
|
160 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
161 ngx_conf_set_path_slot,
|
|
162 NGX_HTTP_LOC_CONF_OFFSET,
|
|
163 offsetof(ngx_http_proxy_loc_conf_t, cache_path),
|
26
|
164 (void *) ngx_http_cache_cleaner_handler },
|
0
|
165
|
|
166 #endif
|
|
167
|
|
168 { ngx_string("proxy_temp_path"),
|
|
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
170 ngx_conf_set_path_slot,
|
|
171 NGX_HTTP_LOC_CONF_OFFSET,
|
|
172 offsetof(ngx_http_proxy_loc_conf_t, temp_path),
|
|
173 (void *) ngx_garbage_collector_temp_handler },
|
|
174
|
16
|
175 { ngx_string("proxy_max_temp_file_size"),
|
|
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
177 ngx_conf_set_size_slot,
|
|
178 NGX_HTTP_LOC_CONF_OFFSET,
|
|
179 offsetof(ngx_http_proxy_loc_conf_t, max_temp_file_size),
|
|
180 NULL },
|
|
181
|
0
|
182 { ngx_string("proxy_temp_file_write_size"),
|
|
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
184 ngx_conf_set_size_slot,
|
|
185 NGX_HTTP_LOC_CONF_OFFSET,
|
|
186 offsetof(ngx_http_proxy_loc_conf_t, temp_file_write_size),
|
|
187 NULL },
|
|
188
|
|
189 { ngx_string("proxy_cache"),
|
|
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
191 ngx_conf_set_flag_slot,
|
|
192 NGX_HTTP_LOC_CONF_OFFSET,
|
|
193 offsetof(ngx_http_proxy_loc_conf_t, cache),
|
|
194 NULL },
|
|
195
|
|
196
|
|
197 { ngx_string("proxy_busy_lock"),
|
|
198 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13,
|
|
199 ngx_http_set_busy_lock_slot,
|
|
200 NGX_HTTP_LOC_CONF_OFFSET,
|
|
201 offsetof(ngx_http_proxy_loc_conf_t, busy_lock),
|
|
202 NULL },
|
|
203
|
|
204
|
|
205 { ngx_string("proxy_pass_server"),
|
|
206 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
207 ngx_conf_set_flag_slot,
|
|
208 NGX_HTTP_LOC_CONF_OFFSET,
|
|
209 offsetof(ngx_http_proxy_loc_conf_t, pass_server),
|
|
210 NULL },
|
|
211
|
|
212 { ngx_string("proxy_pass_x_accel_expires"),
|
|
213 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
214 ngx_conf_set_flag_slot,
|
|
215 NGX_HTTP_LOC_CONF_OFFSET,
|
|
216 offsetof(ngx_http_proxy_loc_conf_t, pass_x_accel_expires),
|
|
217 NULL },
|
|
218
|
|
219 { ngx_string("proxy_ignore_expires"),
|
|
220 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
221 ngx_conf_set_flag_slot,
|
|
222 NGX_HTTP_LOC_CONF_OFFSET,
|
|
223 offsetof(ngx_http_proxy_loc_conf_t, ignore_expires),
|
|
224 NULL },
|
|
225
|
|
226 { ngx_string("proxy_lm_factor"),
|
|
227 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
228 ngx_conf_set_num_slot,
|
|
229 NGX_HTTP_LOC_CONF_OFFSET,
|
|
230 offsetof(ngx_http_proxy_loc_conf_t, lm_factor),
|
|
231 &ngx_http_proxy_lm_factor_bounds },
|
|
232
|
|
233 { ngx_string("proxy_default_expires"),
|
|
234 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
235 ngx_conf_set_sec_slot,
|
|
236 NGX_HTTP_LOC_CONF_OFFSET,
|
|
237 offsetof(ngx_http_proxy_loc_conf_t, default_expires),
|
|
238 NULL },
|
|
239
|
|
240 { ngx_string("proxy_next_upstream"),
|
|
241 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
242 ngx_conf_set_bitmask_slot,
|
|
243 NGX_HTTP_LOC_CONF_OFFSET,
|
|
244 offsetof(ngx_http_proxy_loc_conf_t, next_upstream),
|
|
245 &next_upstream_masks },
|
|
246
|
|
247 { ngx_string("proxy_use_stale"),
|
|
248 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
249 ngx_conf_set_bitmask_slot,
|
|
250 NGX_HTTP_LOC_CONF_OFFSET,
|
|
251 offsetof(ngx_http_proxy_loc_conf_t, use_stale),
|
|
252 &use_stale_masks },
|
|
253
|
|
254 ngx_null_command
|
|
255 };
|
|
256
|
|
257
|
|
258 ngx_http_module_t ngx_http_proxy_module_ctx = {
|
28
|
259 ngx_http_proxy_add_log_formats, /* pre conf */
|
0
|
260
|
|
261 NULL, /* create main configuration */
|
|
262 NULL, /* init main configuration */
|
|
263
|
|
264 NULL, /* create server configuration */
|
|
265 NULL, /* merge server configuration */
|
|
266
|
|
267 ngx_http_proxy_create_loc_conf, /* create location configration */
|
|
268 ngx_http_proxy_merge_loc_conf /* merge location configration */
|
|
269 };
|
|
270
|
|
271
|
|
272 ngx_module_t ngx_http_proxy_module = {
|
|
273 NGX_MODULE,
|
|
274 &ngx_http_proxy_module_ctx, /* module context */
|
|
275 ngx_http_proxy_commands, /* module directives */
|
|
276 NGX_HTTP_MODULE, /* module type */
|
|
277 NULL, /* init module */
|
28
|
278 NULL /* init process */
|
0
|
279 };
|
|
280
|
|
281
|
|
282
|
|
283 static ngx_http_log_op_name_t ngx_http_proxy_log_fmt_ops[] = {
|
28
|
284 { ngx_string("proxy"), 0, NULL,
|
|
285 ngx_http_proxy_log_proxy_state_getlen,
|
|
286 ngx_http_proxy_log_proxy_state },
|
|
287
|
|
288 #if 0
|
22
|
289 { ngx_string("proxy_cache_state"), 0, ngx_http_proxy_log_cache_state },
|
|
290 { ngx_string("proxy_reason"), 0, ngx_http_proxy_log_reason },
|
28
|
291 #endif
|
|
292
|
|
293 { ngx_null_string, 0, NULL, NULL, NULL }
|
0
|
294 };
|
|
295
|
|
296
|
|
297
|
|
298 ngx_http_header_t ngx_http_proxy_headers_in[] = {
|
|
299 { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) },
|
|
300 { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) },
|
|
301
|
|
302 { ngx_string("Expires"), offsetof(ngx_http_proxy_headers_in_t, expires) },
|
|
303 { ngx_string("Cache-Control"),
|
|
304 offsetof(ngx_http_proxy_headers_in_t, cache_control) },
|
|
305 { ngx_string("ETag"), offsetof(ngx_http_proxy_headers_in_t, etag) },
|
|
306 { ngx_string("X-Accel-Expires"),
|
|
307 offsetof(ngx_http_proxy_headers_in_t, x_accel_expires) },
|
|
308
|
|
309 { ngx_string("Connection"),
|
|
310 offsetof(ngx_http_proxy_headers_in_t, connection) },
|
|
311 { ngx_string("Content-Type"),
|
|
312 offsetof(ngx_http_proxy_headers_in_t, content_type) },
|
|
313 { ngx_string("Content-Length"),
|
|
314 offsetof(ngx_http_proxy_headers_in_t, content_length) },
|
28
|
315
|
|
316 #if (NGX_HTTP_GZIP)
|
4
|
317 { ngx_string("Content-Encoding"),
|
|
318 offsetof(ngx_http_proxy_headers_in_t, content_encoding) },
|
28
|
319 #endif
|
|
320
|
0
|
321 { ngx_string("Last-Modified"),
|
|
322 offsetof(ngx_http_proxy_headers_in_t, last_modified) },
|
|
323 { ngx_string("Location"),
|
|
324 offsetof(ngx_http_proxy_headers_in_t, location) },
|
|
325 { ngx_string("Accept-Ranges"),
|
|
326 offsetof(ngx_http_proxy_headers_in_t, accept_ranges) },
|
|
327 { ngx_string("X-Pad"), offsetof(ngx_http_proxy_headers_in_t, x_pad) },
|
|
328
|
|
329 { ngx_null_string, 0 }
|
|
330 };
|
|
331
|
|
332
|
|
333 static ngx_str_t cache_states[] = {
|
|
334 ngx_string("PASS"),
|
|
335 ngx_string("BYPASS"),
|
|
336 ngx_string("AUTH"),
|
|
337 ngx_string("PGNC"),
|
|
338 ngx_string("MISS"),
|
|
339 ngx_string("EXPR"),
|
|
340 ngx_string("AGED"),
|
|
341 ngx_string("HIT")
|
|
342 };
|
|
343
|
|
344
|
|
345 static ngx_str_t cache_reasons[] = {
|
|
346 ngx_string("BPS"),
|
|
347 ngx_string("XAE"),
|
|
348 ngx_string("CTL"),
|
|
349 ngx_string("EXP"),
|
|
350 ngx_string("MVD"),
|
|
351 ngx_string("LMF"),
|
|
352 ngx_string("PDE")
|
|
353 };
|
|
354
|
|
355
|
|
356 static ngx_int_t ngx_http_proxy_handler(ngx_http_request_t *r)
|
|
357 {
|
|
358 ngx_http_proxy_ctx_t *p;
|
|
359
|
|
360 ngx_http_create_ctx(r, p, ngx_http_proxy_module,
|
|
361 sizeof(ngx_http_proxy_ctx_t),
|
|
362 NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
363
|
|
364 p->lcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
365 p->request = r;
|
|
366
|
|
367 /* TODO: we currently support reverse proxy only */
|
|
368 p->accel = 1;
|
|
369
|
26
|
370 if (ngx_array_init(&p->states, r->pool, p->lcf->peers->number,
|
|
371 sizeof(ngx_http_proxy_state_t)) == NGX_ERROR)
|
|
372 {
|
|
373 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
374 }
|
0
|
375
|
26
|
376 if (!(p->state = ngx_array_push(&p->states))) {
|
0
|
377 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
378 }
|
|
379
|
|
380 ngx_memzero(p->state, sizeof(ngx_http_proxy_state_t));
|
|
381
|
26
|
382 #if 0
|
0
|
383
|
|
384 if (!p->lcf->cache
|
|
385 || (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD))
|
|
386 {
|
|
387 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PASS;
|
|
388
|
|
389 } else if (r->bypass_cache) {
|
|
390 p->state->cache_state = NGX_HTTP_PROXY_CACHE_BYPASS;
|
|
391
|
|
392 } else if (r->headers_in.authorization) {
|
|
393 p->state->cache_state = NGX_HTTP_PROXY_CACHE_AUTH;
|
|
394
|
|
395 } else if (r->no_cache) {
|
|
396 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PGNC;
|
|
397 p->cachable = 1;
|
|
398
|
|
399 } else {
|
|
400 p->cachable = 1;
|
|
401 }
|
|
402
|
|
403
|
|
404 if (p->state->cache_state != 0) {
|
|
405 return ngx_http_proxy_request_upstream(p);
|
|
406 }
|
|
407
|
26
|
408 return ngx_http_proxy_cache_get(p);
|
0
|
409
|
|
410 #else
|
|
411
|
|
412 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PASS;
|
|
413
|
|
414 return ngx_http_proxy_request_upstream(p);
|
|
415
|
|
416 #endif
|
|
417 }
|
|
418
|
|
419
|
26
|
420 #if 0
|
|
421
|
|
422 static ngx_int_t ngx_http_proxy_cache_get(ngx_http_proxy_ctx_t *p)
|
|
423 {
|
|
424 u_char *last;
|
|
425 ngx_http_request_t *r;
|
|
426 ngx_http_cache_ctx_t ctx;
|
|
427 ngx_http_proxy_upstream_conf_t *u;
|
|
428
|
|
429 r = p->request;
|
|
430 u = p->lcf->upstream;
|
|
431
|
|
432 ctx.key.len = u->url.len + r->uri.len - u->location->len + r->args.len;
|
|
433 if (!(ctx.key.data = ngx_palloc(r->pool, ctx.key.len))) {
|
|
434 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
435 }
|
|
436
|
|
437 last = ngx_cpymem(ctx.key.data, u->url.data, u->url.len);
|
|
438
|
|
439 last = ngx_cpymem(last, r->uri.data + u->location->len,
|
|
440 r->uri.len - u->location->len);
|
|
441
|
|
442 if (r->args.len > 0) {
|
|
443 *(last++) = '?';
|
|
444 last = ngx_cpymem(last, r->args.data, r->args.len);
|
|
445 }
|
|
446
|
|
447 p->header_in = ngx_create_temp_buf(r->pool, p->lcf->header_buffer_size);
|
|
448 if (p->header_in == NULL) {
|
|
449 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
450 }
|
|
451 p->header_in->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
452
|
|
453 ctx.buf = p->header_in;
|
|
454 ctx.path = p->lcf->cache_path;
|
|
455 ctx.file = 1;
|
|
456 ctx.primary = 1;
|
|
457
|
|
458 ngx_http_cache_get(r, &ctx);
|
|
459
|
|
460 return ngx_http_proxy_request_upstream(p);
|
|
461 }
|
|
462
|
|
463 #endif
|
|
464
|
|
465
|
0
|
466 void ngx_http_proxy_check_broken_connection(ngx_event_t *ev)
|
|
467 {
|
|
468 int n;
|
|
469 char buf[1];
|
|
470 ngx_err_t err;
|
|
471 ngx_connection_t *c;
|
|
472 ngx_http_request_t *r;
|
|
473 ngx_http_proxy_ctx_t *p;
|
|
474
|
|
475 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
476 "http proxy check client, write event:%d", ev->write);
|
|
477
|
18
|
478 #if (NGX_HAVE_KQUEUE)
|
0
|
479
|
4
|
480 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
0
|
481
|
|
482 if (!ev->pending_eof) {
|
|
483 return;
|
|
484 }
|
|
485
|
|
486 c = ev->data;
|
|
487 r = c->data;
|
|
488 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
489
|
|
490 ev->eof = 1;
|
|
491
|
|
492 if (ev->kq_errno) {
|
|
493 ev->error = 1;
|
|
494 }
|
|
495
|
|
496 if (!p->cachable && p->upstream->peer.connection) {
|
|
497 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
|
|
498 "kevent() reported that client closed "
|
|
499 "prematurely connection, "
|
|
500 "so upstream connection is closed too");
|
|
501 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
502 return;
|
|
503 }
|
|
504
|
|
505 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
|
|
506 "kevent() reported that client closed "
|
|
507 "prematurely connection");
|
|
508
|
|
509 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
|
|
510 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
511 }
|
|
512
|
|
513 return;
|
|
514 }
|
|
515
|
|
516 #endif
|
|
517
|
|
518 c = ev->data;
|
|
519 r = c->data;
|
|
520 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
521
|
|
522 n = recv(c->fd, buf, 1, MSG_PEEK);
|
|
523
|
|
524 err = ngx_socket_errno;
|
|
525
|
|
526 /*
|
|
527 * we do not need to disable the write event because
|
|
528 * that event has NGX_USE_CLEAR_EVENT type
|
|
529 */
|
|
530
|
|
531 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
|
|
532 return;
|
|
533 }
|
|
534
|
|
535 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
|
|
536 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
537 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
538 }
|
|
539 }
|
|
540
|
|
541 if (n > 0) {
|
|
542 return;
|
|
543 }
|
|
544
|
|
545 ev->eof = 1;
|
|
546
|
|
547 if (n == -1) {
|
|
548 if (err == NGX_EAGAIN) {
|
|
549 return;
|
|
550 }
|
|
551
|
|
552 ev->error = 1;
|
|
553
|
|
554 } else {
|
|
555 /* n == 0 */
|
|
556 err = 0;
|
|
557 }
|
|
558
|
|
559 if (!p->cachable && p->upstream->peer.connection) {
|
|
560 ngx_log_error(NGX_LOG_INFO, ev->log, err,
|
|
561 "client closed prematurely connection, "
|
|
562 "so upstream connection is closed too");
|
|
563 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
564 return;
|
|
565 }
|
|
566
|
|
567 ngx_log_error(NGX_LOG_INFO, ev->log, err,
|
|
568 "client closed prematurely connection");
|
|
569
|
|
570 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
|
|
571 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
572 }
|
|
573 }
|
|
574
|
|
575
|
|
576 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev)
|
|
577 {
|
|
578 ngx_connection_t *c;
|
|
579 ngx_http_request_t *r;
|
|
580 ngx_http_proxy_ctx_t *p;
|
|
581
|
|
582 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http proxy busy lock");
|
|
583
|
|
584 c = rev->data;
|
|
585 r = c->data;
|
|
586 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
587 p->action = "waiting upstream in busy lock";
|
|
588
|
|
589 if (p->request->connection->write->eof) {
|
|
590 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
591 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
592 return;
|
|
593 }
|
|
594
|
|
595 if (rev->timedout) {
|
|
596 rev->timedout = 0;
|
|
597 p->busy_lock.time++;
|
|
598 p->state->bl_time = p->busy_lock.time;
|
|
599
|
|
600 #if (NGX_HTTP_FILE_CACHE)
|
|
601
|
|
602 if (p->state->cache_state < NGX_HTTP_PROXY_CACHE_MISS) {
|
|
603 ngx_http_proxy_upstream_busy_lock(p);
|
|
604
|
|
605 } else {
|
|
606 ngx_http_proxy_cache_busy_lock(p);
|
|
607 }
|
|
608 #else
|
|
609
|
|
610 ngx_http_proxy_upstream_busy_lock(p);
|
|
611
|
|
612 #endif
|
|
613
|
|
614 return;
|
|
615 }
|
|
616
|
|
617 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
618 "http proxy: client sent while busy lock");
|
|
619
|
|
620 /*
|
|
621 * TODO: kevent() notify about error, otherwise we need to
|
|
622 * call ngx_peek(): recv(MSG_PEEK) to get errno. THINK about aio.
|
|
623 * if there's no error we need to disable event.
|
|
624 */
|
|
625
|
|
626 #if 0
|
18
|
627 #if (NGX_HAVE_KQUEUE)
|
0
|
628
|
18
|
629 if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && rev->kq_eof) {
|
0
|
630 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
631
|
|
632 ngx_del_timer(rev);
|
|
633
|
|
634 ngx_log_error(NGX_LOG_ERR, c->log, rev->kq_errno,
|
|
635 "client() closed connection");
|
|
636
|
|
637 if (ngx_del_event(rev, NGX_READ_EVENT, NGX_CLOSE_EVENT) == NGX_ERROR) {
|
|
638 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
639 return;
|
|
640 }
|
|
641
|
|
642 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
643 return;
|
|
644 }
|
|
645
|
|
646 #endif
|
|
647 #endif
|
|
648
|
|
649 }
|
|
650
|
|
651
|
|
652 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc)
|
|
653 {
|
|
654 ngx_http_request_t *r;
|
|
655
|
|
656 r = p->request;
|
|
657
|
|
658 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
659 "finalize http proxy request");
|
|
660
|
|
661 if (p->upstream && p->upstream->peer.connection) {
|
|
662 ngx_http_proxy_close_connection(p);
|
|
663 }
|
|
664
|
|
665 if (p->header_sent
|
|
666 && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
|
|
667 {
|
|
668 rc = 0;
|
|
669 }
|
|
670
|
|
671 if (p->saved_ctx) {
|
|
672 r->connection->log->data = p->saved_ctx;
|
|
673 r->connection->log->handler = p->saved_handler;
|
|
674 }
|
|
675
|
|
676 if (p->upstream && p->upstream->event_pipe) {
|
|
677 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
678 "http proxy temp fd: %d",
|
|
679 p->upstream->event_pipe->temp_file->file.fd);
|
|
680 }
|
|
681
|
|
682 if (p->cache) {
|
|
683 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
684 "http proxy cache fd: %d",
|
|
685 p->cache->ctx.file.fd);
|
|
686 }
|
|
687
|
|
688 if (p->upstream && p->upstream->event_pipe) {
|
|
689 r->file.fd = p->upstream->event_pipe->temp_file->file.fd;
|
|
690
|
|
691 } else if (p->cache) {
|
|
692 r->file.fd = p->cache->ctx.file.fd;
|
|
693 }
|
|
694
|
|
695 if (rc == 0 && r->main == NULL) {
|
|
696 rc = ngx_http_send_last(r);
|
|
697 }
|
|
698
|
|
699 ngx_http_finalize_request(r, rc);
|
|
700 }
|
|
701
|
|
702
|
|
703 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p)
|
|
704 {
|
|
705 ngx_socket_t fd;
|
|
706 ngx_connection_t *c;
|
|
707
|
|
708 c = p->upstream->peer.connection;
|
|
709 p->upstream->peer.connection = NULL;
|
|
710
|
|
711 if (p->lcf->busy_lock) {
|
|
712 p->lcf->busy_lock->busy--;
|
|
713 }
|
|
714
|
|
715 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
716 "http proxy close connection: %d", c->fd);
|
|
717
|
|
718 if (c->fd == -1) {
|
|
719 #if 0
|
|
720 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
|
|
721 #endif
|
|
722 return;
|
|
723 }
|
|
724
|
|
725 if (c->read->timer_set) {
|
|
726 ngx_del_timer(c->read);
|
|
727 }
|
|
728
|
|
729 if (c->write->timer_set) {
|
|
730 ngx_del_timer(c->write);
|
|
731 }
|
|
732
|
|
733 /* TODO: move connection to the connection pool */
|
|
734
|
|
735 if (ngx_del_conn) {
|
|
736 ngx_del_conn(c, NGX_CLOSE_EVENT);
|
|
737
|
|
738 } else {
|
|
739 if (c->read->active || c->read->disabled) {
|
|
740 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
741 }
|
|
742
|
|
743 if (c->write->active || c->read->disabled) {
|
|
744 ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
|
|
745 }
|
|
746 }
|
|
747
|
|
748 /*
|
|
749 * we have to clean the connection information before the closing
|
|
750 * because another thread may reopen the same file descriptor
|
|
751 * before we clean the connection
|
|
752 */
|
|
753
|
|
754 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
|
|
755
|
|
756 if (c->read->prev) {
|
|
757 ngx_delete_posted_event(c->read);
|
|
758 }
|
|
759
|
|
760 if (c->write->prev) {
|
|
761 ngx_delete_posted_event(c->write);
|
|
762 }
|
|
763
|
|
764 c->read->closed = 1;
|
|
765 c->write->closed = 1;
|
|
766
|
|
767 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
768 }
|
|
769
|
|
770 fd = c->fd;
|
|
771 c->fd = (ngx_socket_t) -1;
|
|
772 c->data = NULL;
|
|
773
|
|
774 if (ngx_close_socket(fd) == -1) {
|
|
775 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
|
|
776 ngx_close_socket_n " failed");
|
|
777 }
|
|
778 }
|
|
779
|
|
780
|
10
|
781 u_char *ngx_http_proxy_log_error(void *data, u_char *buf, size_t len)
|
0
|
782 {
|
|
783 ngx_http_proxy_log_ctx_t *ctx = data;
|
|
784
|
10
|
785 u_char *p;
|
|
786 ngx_int_t escape;
|
|
787 ngx_str_t uri;
|
|
788 ngx_http_request_t *r;
|
|
789 ngx_peer_connection_t *peer;
|
|
790 ngx_http_proxy_upstream_conf_t *uc;
|
0
|
791
|
|
792 r = ctx->proxy->request;
|
10
|
793 uc = ctx->proxy->lcf->upstream;
|
0
|
794 peer = &ctx->proxy->upstream->peer;
|
|
795
|
10
|
796 p = ngx_snprintf(buf, len,
|
28
|
797 " while %s, client: %V, URL: %V, upstream: http://%V%s%V",
|
10
|
798 ctx->proxy->action,
|
|
799 &r->connection->addr_text,
|
|
800 &r->unparsed_uri,
|
28
|
801 &peer->peers->peer[peer->cur_peer].name,
|
|
802 ctx->proxy->lcf->upstream->uri_separator,
|
10
|
803 &ctx->proxy->lcf->upstream->uri);
|
|
804 len -= p - buf;
|
|
805 buf = p;
|
|
806
|
|
807 if (r->quoted_uri) {
|
|
808 escape = 2 * ngx_escape_uri(NULL, r->uri.data + uc->location->len,
|
|
809 r->uri.len - uc->location->len,
|
|
810 NGX_ESCAPE_URI);
|
|
811 } else {
|
|
812 escape = 0;
|
|
813 }
|
|
814
|
|
815 if (escape) {
|
|
816 if (len >= r->uri.len - uc->location->len + escape) {
|
|
817
|
|
818 ngx_escape_uri(buf, r->uri.data + uc->location->len,
|
|
819 r->uri.len - uc->location->len, NGX_ESCAPE_URI);
|
|
820
|
|
821 buf += r->uri.len - uc->location->len + escape;
|
|
822
|
|
823 if (r->args.len == 0) {
|
|
824 return buf;
|
|
825 }
|
|
826
|
|
827 len -= r->uri.len - uc->location->len + escape;
|
|
828
|
|
829 return ngx_snprintf(buf, len, "?%V", &r->args);
|
|
830 }
|
|
831
|
|
832 p = ngx_palloc(r->pool, r->uri.len - uc->location->len + escape);
|
|
833 if (p == NULL) {
|
|
834 return buf;
|
|
835 }
|
|
836
|
|
837 ngx_escape_uri(p, r->uri.data + uc->location->len,
|
|
838 r->uri.len - uc->location->len, NGX_ESCAPE_URI);
|
|
839
|
|
840 uri.len = r->uri.len - uc->location->len + escape;
|
|
841 uri.data = p;
|
|
842
|
|
843 } else {
|
|
844 uri.len = r->uri.len - uc->location->len;
|
|
845 uri.data = r->uri.data + uc->location->len;
|
|
846
|
|
847 }
|
|
848
|
|
849 return ngx_snprintf(buf, len, "%V%s%V",
|
|
850 &uri, r->args.len ? "?" : "", &r->args);
|
0
|
851 }
|
|
852
|
|
853
|
28
|
854 static size_t ngx_http_proxy_log_proxy_state_getlen(ngx_http_request_t *r,
|
|
855 uintptr_t data)
|
|
856 {
|
|
857 ngx_http_proxy_ctx_t *p;
|
|
858
|
|
859 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
860
|
|
861 if (p == NULL) {
|
|
862 return 1;
|
|
863 }
|
|
864
|
|
865 return p->states.nelts * /* STUB */ 100;
|
|
866 }
|
|
867
|
|
868
|
0
|
869 static u_char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r,
|
28
|
870 u_char *buf,
|
|
871 ngx_http_log_op_t *op)
|
0
|
872 {
|
22
|
873 ngx_uint_t i;
|
|
874 ngx_http_proxy_ctx_t *p;
|
|
875 ngx_http_proxy_state_t *state;
|
0
|
876
|
|
877 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
878
|
|
879 if (p == NULL) {
|
|
880 *buf = '-';
|
|
881 return buf + 1;
|
|
882 }
|
|
883
|
22
|
884 i = 0;
|
|
885 state = p->states.elts;
|
0
|
886
|
22
|
887 for ( ;; ) {
|
|
888 if (state[i].cache_state == 0) {
|
|
889 *buf++ = '-';
|
0
|
890
|
22
|
891 } else {
|
|
892 buf = ngx_cpymem(buf, cache_states[state[i].cache_state - 1].data,
|
|
893 cache_states[state[i].cache_state - 1].len);
|
|
894 }
|
0
|
895
|
22
|
896 *buf++ = '/';
|
|
897
|
|
898 if (state[i].expired == 0) {
|
|
899 *buf++ = '-';
|
0
|
900
|
22
|
901 } else {
|
|
902 buf = ngx_sprintf(buf, "%T", state[i].expired);
|
|
903 }
|
|
904
|
|
905 *buf++ = '/';
|
|
906
|
|
907 if (state[i].bl_time == 0) {
|
|
908 *buf++ = '-';
|
0
|
909
|
22
|
910 } else {
|
|
911 buf = ngx_sprintf(buf, "%T", state[i].bl_time);
|
|
912 }
|
0
|
913
|
22
|
914 *buf++ = '/';
|
|
915
|
|
916 *buf++ = '*';
|
|
917
|
|
918 *buf++ = ' ';
|
0
|
919
|
22
|
920 if (state[i].status == 0) {
|
|
921 *buf++ = '-';
|
|
922
|
|
923 } else {
|
|
924 buf = ngx_sprintf(buf, "%ui", state[i].status);
|
|
925 }
|
0
|
926
|
22
|
927 *buf++ = '/';
|
|
928
|
|
929 if (state[i].reason == 0) {
|
|
930 *buf++ = '-';
|
|
931
|
|
932 } else {
|
|
933 buf = ngx_cpymem(buf, cache_reasons[state[i].reason - 1].data,
|
|
934 cache_reasons[state[i].reason - 1].len);
|
|
935 }
|
0
|
936
|
22
|
937 *buf++ = '/';
|
|
938
|
|
939 if (state[i].reason < NGX_HTTP_PROXY_CACHE_XAE) {
|
|
940 *buf++ = '-';
|
|
941
|
|
942 } else {
|
|
943 buf = ngx_sprintf(buf, "%T", state[i].expires);
|
|
944 }
|
0
|
945
|
22
|
946 *buf++ = ' ';
|
|
947 *buf++ = '*';
|
|
948
|
|
949 if (++i == p->states.nelts) {
|
|
950 return buf;
|
|
951 }
|
|
952
|
|
953 *buf++ = ',';
|
|
954 *buf++ = ' ';
|
0
|
955 }
|
|
956 }
|
|
957
|
|
958
|
28
|
959 #if 0
|
|
960
|
0
|
961 static u_char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r,
|
|
962 u_char *buf, uintptr_t data)
|
|
963 {
|
22
|
964 ngx_uint_t i;
|
|
965 ngx_http_proxy_ctx_t *p;
|
|
966 ngx_http_proxy_state_t *state;
|
0
|
967
|
|
968 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
969
|
|
970 if (p == NULL || p->state->cache_state == 0) {
|
22
|
971 if (buf == NULL) {
|
|
972 return (u_char *) 1;
|
|
973 }
|
|
974
|
0
|
975 *buf = '-';
|
|
976 return buf + 1;
|
|
977 }
|
|
978
|
22
|
979 if (buf == NULL) {
|
|
980 /* find the request line length */
|
|
981 return (u_char *) (p->states.nelts * sizeof("BYPASS") - 1);
|
|
982 }
|
|
983
|
|
984 i = 0;
|
|
985 state = p->states.elts;
|
|
986
|
|
987 for ( ;; ) {
|
|
988 buf = ngx_cpymem(buf, cache_states[state[i].cache_state - 1].data,
|
|
989 cache_states[state[i].cache_state - 1].len);
|
|
990
|
|
991 if (++i == p->states.nelts) {
|
|
992 return buf;
|
|
993 }
|
|
994
|
|
995 *buf++ = ',';
|
|
996 *buf++ = ' ';
|
|
997 }
|
0
|
998 }
|
|
999
|
|
1000
|
|
1001 static u_char *ngx_http_proxy_log_reason(ngx_http_request_t *r, u_char *buf,
|
|
1002 uintptr_t data)
|
|
1003 {
|
22
|
1004 ngx_uint_t i;
|
|
1005 ngx_http_proxy_ctx_t *p;
|
|
1006 ngx_http_proxy_state_t *state;
|
0
|
1007
|
|
1008 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
1009
|
|
1010 if (p == NULL || p->state->reason == 0) {
|
22
|
1011 if (buf == NULL) {
|
|
1012 return (u_char *) 1;
|
|
1013 }
|
|
1014
|
0
|
1015 *buf = '-';
|
|
1016 return buf + 1;
|
|
1017 }
|
|
1018
|
22
|
1019 if (buf == NULL) {
|
|
1020 /* find the request line length */
|
|
1021 return (u_char *) (p->states.nelts * sizeof("BPS") - 1);
|
|
1022 }
|
|
1023
|
|
1024 i = 0;
|
|
1025 state = p->states.elts;
|
|
1026
|
|
1027 for ( ;; ) {
|
|
1028 buf = ngx_cpymem(buf, cache_reasons[state[i].reason - 1].data,
|
|
1029 cache_reasons[state[i].reason - 1].len);
|
|
1030
|
|
1031 if (++i == p->states.nelts) {
|
|
1032 return buf;
|
|
1033 }
|
|
1034
|
|
1035 *buf++ = ',';
|
|
1036 *buf++ = ' ';
|
|
1037 }
|
0
|
1038 }
|
|
1039
|
28
|
1040 #endif
|
0
|
1041
|
28
|
1042
|
|
1043 static ngx_int_t ngx_http_proxy_add_log_formats(ngx_conf_t *cf)
|
0
|
1044 {
|
|
1045 ngx_http_log_op_name_t *op;
|
|
1046
|
|
1047 for (op = ngx_http_proxy_log_fmt_ops; op->name.len; op++) { /* void */ }
|
28
|
1048 op->run = NULL;
|
0
|
1049
|
28
|
1050 for (op = ngx_http_log_fmt_ops; op->run; op++) {
|
0
|
1051 if (op->name.len == 0) {
|
28
|
1052 op = (ngx_http_log_op_name_t *) op->run;
|
0
|
1053 }
|
|
1054 }
|
|
1055
|
28
|
1056 op->run = (ngx_http_log_op_run_pt) ngx_http_proxy_log_fmt_ops;
|
0
|
1057
|
|
1058 return NGX_OK;
|
|
1059 }
|
|
1060
|
|
1061
|
|
1062 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
1063 {
|
|
1064 ngx_http_proxy_loc_conf_t *conf;
|
|
1065
|
|
1066 ngx_test_null(conf,
|
|
1067 ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t)),
|
|
1068 NGX_CONF_ERROR);
|
|
1069
|
28
|
1070 /*
|
|
1071 * set by ngx_pcalloc():
|
|
1072 *
|
|
1073 * conf->bufs.num = 0;
|
|
1074 * conf->path = NULL;
|
|
1075 * conf->next_upstream = 0;
|
|
1076 * conf->use_stale = 0;
|
|
1077 * conf->upstreams = NULL;
|
|
1078 * conf->peers = NULL;
|
|
1079 * conf->cache_path = NULL;
|
|
1080 * conf->temp_path = NULL;
|
|
1081 * conf->busy_lock = NULL;
|
|
1082 */
|
0
|
1083
|
|
1084 conf->connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
1085 conf->send_timeout = NGX_CONF_UNSET_MSEC;
|
2
|
1086 conf->send_lowat = NGX_CONF_UNSET_SIZE;
|
0
|
1087
|
|
1088 conf->preserve_host = NGX_CONF_UNSET;
|
6
|
1089 conf->set_x_url = NGX_CONF_UNSET;
|
0
|
1090 conf->set_x_real_ip = NGX_CONF_UNSET;
|
|
1091 conf->add_x_forwarded_for = NGX_CONF_UNSET;
|
|
1092
|
|
1093 conf->header_buffer_size = NGX_CONF_UNSET_SIZE;
|
|
1094 conf->read_timeout = NGX_CONF_UNSET_MSEC;
|
|
1095 conf->busy_buffers_size = NGX_CONF_UNSET_SIZE;
|
|
1096
|
16
|
1097 conf->max_temp_file_size = NGX_CONF_UNSET_SIZE;
|
0
|
1098 conf->temp_file_write_size = NGX_CONF_UNSET_SIZE;
|
|
1099
|
|
1100 /* "proxy_cyclic_temp_file" is disabled */
|
|
1101 conf->cyclic_temp_file = 0;
|
|
1102
|
|
1103 conf->cache = NGX_CONF_UNSET;
|
|
1104
|
|
1105 conf->pass_server = NGX_CONF_UNSET;
|
|
1106 conf->pass_x_accel_expires = NGX_CONF_UNSET;
|
|
1107 conf->ignore_expires = NGX_CONF_UNSET;
|
|
1108 conf->lm_factor = NGX_CONF_UNSET;
|
|
1109 conf->default_expires = NGX_CONF_UNSET;
|
|
1110
|
|
1111 return conf;
|
|
1112 }
|
|
1113
|
|
1114
|
|
1115 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
1116 void *parent, void *child)
|
|
1117 {
|
|
1118 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
1119 ngx_http_proxy_loc_conf_t *conf = child;
|
|
1120
|
|
1121 size_t size;
|
|
1122
|
|
1123 ngx_conf_merge_msec_value(conf->connect_timeout,
|
|
1124 prev->connect_timeout, 60000);
|
|
1125 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
|
2
|
1126 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
|
0
|
1127
|
|
1128 ngx_conf_merge_value(conf->preserve_host, prev->preserve_host, 0);
|
6
|
1129 ngx_conf_merge_value(conf->set_x_url, prev->set_x_url, 0);
|
0
|
1130 ngx_conf_merge_value(conf->set_x_real_ip, prev->set_x_real_ip, 0);
|
|
1131 ngx_conf_merge_value(conf->add_x_forwarded_for,
|
|
1132 prev->add_x_forwarded_for, 0);
|
|
1133
|
|
1134 ngx_conf_merge_msec_value(conf->read_timeout, prev->read_timeout, 60000);
|
|
1135
|
|
1136 ngx_conf_merge_size_value(conf->header_buffer_size,
|
|
1137 prev->header_buffer_size, (size_t) ngx_pagesize);
|
|
1138
|
|
1139 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 8, ngx_pagesize);
|
|
1140
|
|
1141 if (conf->bufs.num < 2) {
|
|
1142 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1143 "there must be at least 2 \"proxy_buffers\"");
|
|
1144 return NGX_CONF_ERROR;
|
|
1145 }
|
|
1146
|
|
1147 size = conf->header_buffer_size;
|
|
1148 if (size < conf->bufs.size) {
|
|
1149 size = conf->bufs.size;
|
|
1150 }
|
|
1151
|
|
1152
|
|
1153 ngx_conf_merge_size_value(conf->busy_buffers_size,
|
|
1154 prev->busy_buffers_size, NGX_CONF_UNSET_SIZE);
|
|
1155
|
|
1156 if (conf->busy_buffers_size == NGX_CONF_UNSET_SIZE) {
|
|
1157 conf->busy_buffers_size = 2 * size;
|
|
1158
|
|
1159 } else if (conf->busy_buffers_size < size) {
|
|
1160 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1161 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
|
1162 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1163 "one of the \"proxy_buffers\"");
|
|
1164
|
|
1165 return NGX_CONF_ERROR;
|
|
1166
|
|
1167 } else if (conf->busy_buffers_size > (conf->bufs.num - 1) * conf->bufs.size)
|
|
1168 {
|
|
1169 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1170 "\"proxy_busy_buffers_size\" must be less than "
|
|
1171 "the size of all \"proxy_buffers\" minus one buffer");
|
|
1172
|
|
1173 return NGX_CONF_ERROR;
|
|
1174 }
|
|
1175
|
|
1176
|
|
1177 ngx_conf_merge_size_value(conf->temp_file_write_size,
|
|
1178 prev->temp_file_write_size, NGX_CONF_UNSET_SIZE);
|
|
1179
|
|
1180 if (conf->temp_file_write_size == NGX_CONF_UNSET_SIZE) {
|
|
1181 conf->temp_file_write_size = 2 * size;
|
|
1182
|
|
1183 } else if (conf->temp_file_write_size < size) {
|
|
1184 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1185 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
|
1186 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1187 "one of the \"proxy_buffers\"");
|
|
1188
|
|
1189 return NGX_CONF_ERROR;
|
|
1190 }
|
|
1191
|
|
1192
|
|
1193 ngx_conf_merge_size_value(conf->max_temp_file_size,
|
|
1194 prev->max_temp_file_size, NGX_CONF_UNSET_SIZE);
|
|
1195
|
|
1196 if (conf->max_temp_file_size == NGX_CONF_UNSET_SIZE) {
|
16
|
1197
|
|
1198 /*
|
|
1199 * "proxy_max_temp_file_size" is set to 1G for reverse proxy,
|
|
1200 * it should be much less in the generic proxy
|
|
1201 */
|
|
1202
|
|
1203 conf->max_temp_file_size = 1024 * 1024 * 1024;
|
0
|
1204
|
16
|
1205 #if 0
|
|
1206 conf->max_temp_file_size = 2 * size;
|
|
1207 #endif
|
|
1208
|
|
1209
|
|
1210 } else if (conf->max_temp_file_size != 0
|
|
1211 && conf->max_temp_file_size < size)
|
|
1212 {
|
0
|
1213 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
16
|
1214 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
|
1215 "the temporary files usage or must be equal or bigger than "
|
0
|
1216 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1217 "one of the \"proxy_buffers\"");
|
|
1218
|
|
1219 return NGX_CONF_ERROR;
|
|
1220 }
|
|
1221
|
|
1222
|
|
1223 ngx_conf_merge_bitmask_value(conf->next_upstream, prev->next_upstream,
|
|
1224 (NGX_CONF_BITMASK_SET
|
|
1225 |NGX_HTTP_PROXY_FT_ERROR
|
|
1226 |NGX_HTTP_PROXY_FT_TIMEOUT));
|
|
1227
|
|
1228 ngx_conf_merge_bitmask_value(conf->use_stale, prev->use_stale,
|
|
1229 NGX_CONF_BITMASK_SET);
|
|
1230
|
28
|
1231 #if 0
|
0
|
1232 ngx_conf_merge_path_value(conf->cache_path, prev->cache_path,
|
28
|
1233 NGX_HTTP_PROXY_CACHE_PATH, 1, 2, 0,
|
|
1234 ngx_garbage_collector_temp_handler, cf);
|
|
1235 #endif
|
0
|
1236
|
|
1237 ngx_conf_merge_path_value(conf->temp_path, prev->temp_path,
|
28
|
1238 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,
|
|
1239 ngx_garbage_collector_temp_handler, cf);
|
0
|
1240
|
|
1241 ngx_conf_merge_value(conf->cache, prev->cache, 0);
|
|
1242
|
|
1243
|
|
1244 /* conf->cache must be merged */
|
|
1245
|
|
1246 if (conf->busy_lock == NULL) {
|
|
1247 conf->busy_lock = prev->busy_lock;
|
|
1248 }
|
|
1249
|
|
1250 if (conf->busy_lock && conf->cache && conf->busy_lock->md5 == NULL) {
|
|
1251
|
|
1252 /* ngx_calloc_shared() */
|
|
1253 conf->busy_lock->md5_mask =
|
|
1254 ngx_pcalloc(cf->pool, (conf->busy_lock->max_busy + 7) / 8);
|
|
1255 if (conf->busy_lock->md5_mask == NULL) {
|
|
1256 return NGX_CONF_ERROR;
|
|
1257 }
|
|
1258
|
|
1259 /* 16 bytes are 128 bits of the md5 */
|
|
1260
|
|
1261 /* ngx_alloc_shared() */
|
|
1262 conf->busy_lock->md5 = ngx_palloc(cf->pool,
|
|
1263 16 * conf->busy_lock->max_busy);
|
|
1264 if (conf->busy_lock->md5 == NULL) {
|
|
1265 return NGX_CONF_ERROR;
|
|
1266 }
|
|
1267 }
|
|
1268
|
|
1269
|
|
1270 ngx_conf_merge_value(conf->pass_server, prev->pass_server, 0);
|
|
1271 ngx_conf_merge_value(conf->pass_x_accel_expires,
|
|
1272 prev->pass_x_accel_expires, 0);
|
|
1273 ngx_conf_merge_value(conf->ignore_expires, prev->ignore_expires, 0);
|
|
1274 ngx_conf_merge_value(conf->lm_factor, prev->lm_factor, 0);
|
|
1275 ngx_conf_merge_sec_value(conf->default_expires, prev->default_expires, 0);
|
|
1276
|
|
1277 return NULL;
|
|
1278 }
|
|
1279
|
|
1280
|
|
1281 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1282 void *conf)
|
|
1283 {
|
|
1284 ngx_http_proxy_loc_conf_t *lcf = conf;
|
|
1285
|
28
|
1286 ngx_str_t *value, *url;
|
|
1287 ngx_inet_upstream_t inet_upstream;
|
|
1288 ngx_http_core_loc_conf_t *clcf;
|
|
1289 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
1290 ngx_unix_domain_upstream_t unix_upstream;
|
|
1291 #endif
|
0
|
1292
|
|
1293 value = cf->args->elts;
|
|
1294
|
28
|
1295 url = &value[1];
|
|
1296
|
|
1297 if (ngx_strncasecmp(url->data, "http://", 7) != 0) {
|
2
|
1298 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
1299 return NGX_CONF_ERROR;
|
0
|
1300 }
|
|
1301
|
2
|
1302 lcf->upstream = ngx_pcalloc(cf->pool,
|
|
1303 sizeof(ngx_http_proxy_upstream_conf_t));
|
|
1304 if (lcf->upstream == NULL) {
|
|
1305 return NGX_CONF_ERROR;
|
|
1306 }
|
0
|
1307
|
28
|
1308 if (ngx_strncasecmp(url->data + 7, "unix:", 5) == 0) {
|
0
|
1309
|
28
|
1310 #if (NGX_HAVE_UNIX_DOMAIN)
|
0
|
1311
|
28
|
1312 ngx_memzero(&unix_upstream, sizeof(ngx_unix_domain_upstream_t));
|
2
|
1313
|
28
|
1314 unix_upstream.name = *url;
|
|
1315 unix_upstream.url.len = url->len - 7;
|
|
1316 unix_upstream.url.data = url->data + 7;
|
|
1317 unix_upstream.uri_part = 1;
|
0
|
1318
|
28
|
1319 if (!(lcf->peers = ngx_unix_upstream_parse(cf, &unix_upstream))) {
|
2
|
1320 return NGX_CONF_ERROR;
|
|
1321 }
|
0
|
1322
|
28
|
1323 lcf->upstream->host_header.len = sizeof("localhost") - 1;
|
|
1324 lcf->upstream->host_header.data = (u_char *) "localhost";
|
|
1325 lcf->upstream->uri = unix_upstream.uri;
|
|
1326 lcf->upstream->uri_separator = ":";
|
|
1327 lcf->upstream->default_port = 1;
|
2
|
1328
|
28
|
1329 #else
|
|
1330 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1331 "the unix domain sockets are not supported "
|
|
1332 "on this platform");
|
|
1333 return NGX_CONF_ERROR;
|
0
|
1334
|
28
|
1335 #endif
|
0
|
1336
|
|
1337 } else {
|
28
|
1338 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
|
0
|
1339
|
28
|
1340 inet_upstream.name = *url;
|
|
1341 inet_upstream.url.len = url->len - 7;
|
|
1342 inet_upstream.url.data = url->data + 7;
|
|
1343 inet_upstream.default_port_value = 80;
|
|
1344 inet_upstream.uri_part = 1;
|
0
|
1345
|
28
|
1346 if (!(lcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream))) {
|
2
|
1347 return NGX_CONF_ERROR;
|
|
1348 }
|
0
|
1349
|
28
|
1350 lcf->upstream->host_header = inet_upstream.host_header;
|
|
1351 lcf->upstream->port_text = inet_upstream.port_text;
|
|
1352 lcf->upstream->uri = inet_upstream.uri;
|
|
1353 lcf->upstream->uri_separator = "";
|
30
|
1354 lcf->upstream->default_port = inet_upstream.default_port;
|
0
|
1355 }
|
|
1356
|
|
1357 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
1358
|
|
1359 lcf->upstream->location = &clcf->name;
|
|
1360 clcf->handler = ngx_http_proxy_handler;
|
|
1361
|
|
1362 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
1363 clcf->auto_redirect = 1;
|
|
1364 }
|
|
1365
|
28
|
1366 return NGX_CONF_OK;
|
0
|
1367 }
|
2
|
1368
|
|
1369
|
|
1370 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
1371 {
|
22
|
1372 #if (NGX_FREEBSD)
|
4
|
1373 ssize_t *np = data;
|
2
|
1374
|
|
1375 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
|
1376 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1377 "\"proxy_send_lowat\" must be less than %d "
|
|
1378 "(sysctl net.inet.tcp.sendspace)",
|
|
1379 ngx_freebsd_net_inet_tcp_sendspace);
|
|
1380
|
|
1381 return NGX_CONF_ERROR;
|
|
1382 }
|
|
1383
|
18
|
1384 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
22
|
1385 ssize_t *np = data;
|
2
|
1386
|
|
1387 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1388 "\"proxy_send_lowat\" is not supported, ignored");
|
|
1389
|
|
1390 *np = 0;
|
|
1391
|
|
1392 #endif
|
|
1393
|
|
1394 return NGX_CONF_OK;
|
|
1395 }
|