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