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