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