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