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