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