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