3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx);
13 static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r);
14 static void ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r);
15 static void ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
17 static void ngx_http_upstream_connect(ngx_http_request_t *r,
18 ngx_http_upstream_t *u);
19 static ngx_int_t ngx_http_upstream_reinit(ngx_http_request_t *r,
20 ngx_http_upstream_t *u);
21 static void ngx_http_upstream_send_request(ngx_http_request_t *r,
22 ngx_http_upstream_t *u);
23 static void ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
24 ngx_http_upstream_t *u);
25 static void ngx_http_upstream_process_header(ngx_http_request_t *r,
26 ngx_http_upstream_t *u);
27 static ngx_int_t ngx_http_upstream_test_next(ngx_http_request_t *r,
28 ngx_http_upstream_t *u);
29 static ngx_int_t ngx_http_upstream_intercept_errors(ngx_http_request_t *r,
30 ngx_http_upstream_t *u);
31 static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c);
32 static void ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
33 ngx_http_upstream_t *u);
34 static void ngx_http_upstream_send_response(ngx_http_request_t *r,
35 ngx_http_upstream_t *u);
37 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r);
39 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
40 ngx_http_upstream_t *u);
42 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
44 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data);
45 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data,
47 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r);
48 static void ngx_http_upstream_process_upstream(ngx_http_request_t *r,
49 ngx_http_upstream_t *u);
50 static void ngx_http_upstream_process_request(ngx_http_request_t *r);
51 static void ngx_http_upstream_store(ngx_http_request_t *r,
52 ngx_http_upstream_t *u);
53 static void ngx_http_upstream_dummy_handler(ngx_http_request_t *r,
54 ngx_http_upstream_t *u);
55 static void ngx_http_upstream_next(ngx_http_request_t *r,
56 ngx_http_upstream_t *u, ngx_uint_t ft_type);
57 static void ngx_http_upstream_cleanup(void *data);
58 static void ngx_http_upstream_finalize_request(ngx_http_request_t *r,
59 ngx_http_upstream_t *u, ngx_int_t rc);
61 static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r,
62 ngx_table_elt_t *h, ngx_uint_t offset);
64 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
65 ngx_table_elt_t *h, ngx_uint_t offset);
66 static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r,
67 ngx_table_elt_t *h, ngx_uint_t offset);
68 static ngx_int_t ngx_http_upstream_process_limit_rate(ngx_http_request_t *r,
69 ngx_table_elt_t *h, ngx_uint_t offset);
70 static ngx_int_t ngx_http_upstream_process_buffering(ngx_http_request_t *r,
71 ngx_table_elt_t *h, ngx_uint_t offset);
72 static ngx_int_t ngx_http_upstream_process_charset(ngx_http_request_t *r,
73 ngx_table_elt_t *h, ngx_uint_t offset);
74 static ngx_int_t ngx_http_upstream_copy_header_line(ngx_http_request_t *r,
75 ngx_table_elt_t *h, ngx_uint_t offset);
77 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
78 ngx_table_elt_t *h, ngx_uint_t offset);
79 static ngx_int_t ngx_http_upstream_copy_content_type(ngx_http_request_t *r,
80 ngx_table_elt_t *h, ngx_uint_t offset);
81 static ngx_int_t ngx_http_upstream_copy_content_length(ngx_http_request_t *r,
82 ngx_table_elt_t *h, ngx_uint_t offset);
83 static ngx_int_t ngx_http_upstream_rewrite_location(ngx_http_request_t *r,
84 ngx_table_elt_t *h, ngx_uint_t offset);
85 static ngx_int_t ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r,
86 ngx_table_elt_t *h, ngx_uint_t offset);
88 static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
89 ngx_table_elt_t *h, ngx_uint_t offset);
92 static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf);
93 static ngx_int_t ngx_http_upstream_addr_variable(ngx_http_request_t *r,
94 ngx_http_variable_value_t *v, uintptr_t data);
95 static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r,
96 ngx_http_variable_value_t *v, uintptr_t data);
97 static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
98 ngx_http_variable_value_t *v, uintptr_t data);
99 static ngx_int_t ngx_http_upstream_response_length_variable(
100 ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
102 static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
103 static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
106 static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf);
107 static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf);
110 static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
111 ngx_http_upstream_t *u, ngx_connection_t *c);
112 static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c);
116 ngx_http_upstream_header_t ngx_http_upstream_headers_in[] = {
118 { ngx_string("Status"),
119 ngx_http_upstream_process_header_line,
120 offsetof(ngx_http_upstream_headers_in_t, status),
121 ngx_http_upstream_copy_header_line, 0, 0 },
123 { ngx_string("Content-Type"),
124 ngx_http_upstream_process_header_line,
125 offsetof(ngx_http_upstream_headers_in_t, content_type),
126 ngx_http_upstream_copy_content_type, 0, 1 },
128 { ngx_string("Content-Length"),
129 ngx_http_upstream_process_header_line,
130 offsetof(ngx_http_upstream_headers_in_t, content_length),
131 ngx_http_upstream_copy_content_length, 0, 0 },
133 { ngx_string("Date"),
134 ngx_http_upstream_process_header_line,
135 offsetof(ngx_http_upstream_headers_in_t, date),
136 ngx_http_upstream_copy_header_line,
137 offsetof(ngx_http_headers_out_t, date), 0 },
139 { ngx_string("Last-Modified"),
140 ngx_http_upstream_process_header_line,
141 offsetof(ngx_http_upstream_headers_in_t, last_modified),
142 ngx_http_upstream_copy_header_line,
143 offsetof(ngx_http_headers_out_t, last_modified), 0 },
145 { ngx_string("Server"),
146 ngx_http_upstream_process_header_line,
147 offsetof(ngx_http_upstream_headers_in_t, server),
148 ngx_http_upstream_copy_header_line,
149 offsetof(ngx_http_headers_out_t, server), 0 },
151 { ngx_string("WWW-Authenticate"),
152 ngx_http_upstream_process_header_line,
153 offsetof(ngx_http_upstream_headers_in_t, www_authenticate),
154 ngx_http_upstream_copy_header_line, 0, 0 },
156 { ngx_string("Location"),
157 ngx_http_upstream_process_header_line,
158 offsetof(ngx_http_upstream_headers_in_t, location),
159 ngx_http_upstream_rewrite_location, 0, 0 },
161 { ngx_string("Refresh"),
162 ngx_http_upstream_ignore_header_line, 0,
163 ngx_http_upstream_rewrite_refresh, 0, 0 },
165 { ngx_string("Set-Cookie"),
166 ngx_http_upstream_ignore_header_line, 0,
167 ngx_http_upstream_copy_header_line, 0, 1 },
169 { ngx_string("Content-Disposition"),
170 ngx_http_upstream_ignore_header_line, 0,
171 ngx_http_upstream_copy_header_line, 0, 1 },
173 { ngx_string("Cache-Control"),
174 ngx_http_upstream_process_multi_header_lines,
175 offsetof(ngx_http_upstream_headers_in_t, cache_control),
176 ngx_http_upstream_copy_multi_header_lines,
177 offsetof(ngx_http_headers_out_t, cache_control), 1 },
179 { ngx_string("Expires"),
180 ngx_http_upstream_process_header_line,
181 offsetof(ngx_http_upstream_headers_in_t, expires),
182 ngx_http_upstream_copy_header_line,
183 offsetof(ngx_http_headers_out_t, expires), 1 },
185 { ngx_string("Accept-Ranges"),
186 ngx_http_upstream_process_header_line,
187 offsetof(ngx_http_upstream_headers_in_t, accept_ranges),
188 ngx_http_upstream_copy_header_line,
189 offsetof(ngx_http_headers_out_t, accept_ranges), 1 },
191 { ngx_string("Connection"),
192 ngx_http_upstream_ignore_header_line, 0,
193 ngx_http_upstream_ignore_header_line, 0, 0 },
195 { ngx_string("Keep-Alive"),
196 ngx_http_upstream_ignore_header_line, 0,
197 ngx_http_upstream_ignore_header_line, 0, 0 },
199 { ngx_string("X-Powered-By"),
200 ngx_http_upstream_ignore_header_line, 0,
201 ngx_http_upstream_copy_header_line, 0, 0 },
203 { ngx_string("X-Accel-Expires"),
204 ngx_http_upstream_process_header_line,
205 offsetof(ngx_http_upstream_headers_in_t, x_accel_expires),
206 ngx_http_upstream_copy_header_line, 0, 0 },
208 { ngx_string("X-Accel-Redirect"),
209 ngx_http_upstream_process_header_line,
210 offsetof(ngx_http_upstream_headers_in_t, x_accel_redirect),
211 ngx_http_upstream_ignore_header_line, 0, 0 },
213 { ngx_string("X-Accel-Limit-Rate"),
214 ngx_http_upstream_process_limit_rate, 0,
215 ngx_http_upstream_ignore_header_line, 0, 0 },
217 { ngx_string("X-Accel-Buffering"),
218 ngx_http_upstream_process_buffering, 0,
219 ngx_http_upstream_ignore_header_line, 0, 0 },
221 { ngx_string("X-Accel-Charset"),
222 ngx_http_upstream_process_charset, 0,
223 ngx_http_upstream_ignore_header_line, 0, 0 },
226 { ngx_string("Content-Encoding"),
227 ngx_http_upstream_process_header_line,
228 offsetof(ngx_http_upstream_headers_in_t, content_encoding),
229 ngx_http_upstream_copy_content_encoding, 0, 0 },
232 { ngx_null_string, NULL, 0, NULL, 0, 0 }
236 static ngx_command_t ngx_http_upstream_commands[] = {
238 { ngx_string("upstream"),
239 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
245 { ngx_string("server"),
246 NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
247 ngx_http_upstream_server,
248 NGX_HTTP_SRV_CONF_OFFSET,
256 static ngx_http_module_t ngx_http_upstream_module_ctx = {
257 ngx_http_upstream_add_variables, /* preconfiguration */
258 NULL, /* postconfiguration */
260 ngx_http_upstream_create_main_conf, /* create main configuration */
261 ngx_http_upstream_init_main_conf, /* init main configuration */
263 NULL, /* create server configuration */
264 NULL, /* merge server configuration */
266 NULL, /* create location configuration */
267 NULL /* merge location configuration */
271 ngx_module_t ngx_http_upstream_module = {
273 &ngx_http_upstream_module_ctx, /* module context */
274 ngx_http_upstream_commands, /* module directives */
275 NGX_HTTP_MODULE, /* module type */
276 NULL, /* init master */
277 NULL, /* init module */
278 NULL, /* init process */
279 NULL, /* init thread */
280 NULL, /* exit thread */
281 NULL, /* exit process */
282 NULL, /* exit master */
283 NGX_MODULE_V1_PADDING
287 static ngx_http_variable_t ngx_http_upstream_vars[] = {
289 { ngx_string("upstream_addr"), NULL,
290 ngx_http_upstream_addr_variable, 0,
291 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
293 { ngx_string("upstream_status"), NULL,
294 ngx_http_upstream_status_variable, 0,
295 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
297 { ngx_string("upstream_response_time"), NULL,
298 ngx_http_upstream_response_time_variable, 0,
299 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
301 { ngx_string("upstream_response_length"), NULL,
302 ngx_http_upstream_response_length_variable, 0,
303 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 },
305 { ngx_null_string, NULL, NULL, 0, 0, 0 }
309 static ngx_http_upstream_next_t ngx_http_upstream_next_errors[] = {
310 { 500, NGX_HTTP_UPSTREAM_FT_HTTP_500 },
311 { 502, NGX_HTTP_UPSTREAM_FT_HTTP_502 },
312 { 503, NGX_HTTP_UPSTREAM_FT_HTTP_503 },
313 { 504, NGX_HTTP_UPSTREAM_FT_HTTP_504 },
314 { 404, NGX_HTTP_UPSTREAM_FT_HTTP_404 },
319 ngx_http_upstream_init(ngx_http_request_t *r)
324 ngx_resolver_ctx_t *ctx, temp;
325 ngx_http_cleanup_t *cln;
326 ngx_http_upstream_t *u;
327 ngx_http_core_loc_conf_t *clcf;
328 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
329 ngx_http_upstream_main_conf_t *umcf;
333 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
334 "http init upstream, client timer: %d", c->read->timer_set);
336 if (c->read->timer_set) {
337 ngx_del_timer(c->read);
342 if (!r->post_action && !u->conf->ignore_client_abort) {
343 r->read_event_handler = ngx_http_upstream_rd_check_broken_connection;
344 r->write_event_handler = ngx_http_upstream_wr_check_broken_connection;
347 if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
349 if (!c->write->active) {
350 if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
353 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
359 if (r->request_body) {
360 u->request_bufs = r->request_body->bufs;
363 if (u->create_request(r) != NGX_OK) {
364 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
368 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
370 u->output.pool = r->pool;
371 u->output.bufs.num = 1;
372 u->output.bufs.size = clcf->client_body_buffer_size;
373 u->output.output_filter = ngx_chain_writer;
374 u->output.filter_ctx = &u->writer;
376 u->writer.pool = r->pool;
378 if (r->upstream_states == NULL) {
380 r->upstream_states = ngx_array_create(r->pool, 1,
381 sizeof(ngx_http_upstream_state_t));
382 if (r->upstream_states == NULL) {
383 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
389 u->state = ngx_array_push(r->upstream_states);
390 if (u->state == NULL) {
391 ngx_http_upstream_finalize_request(r, u,
392 NGX_HTTP_INTERNAL_SERVER_ERROR);
396 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
399 cln = ngx_http_cleanup_add(r, 0);
401 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
405 cln->handler = ngx_http_upstream_cleanup;
407 u->cleanup = &cln->handler;
409 u->store = (u->conf->store || u->conf->store_lengths);
411 if (u->resolved == NULL) {
413 uscf = u->conf->upstream;
417 if (u->resolved->sockaddr) {
419 if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
422 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
426 ngx_http_upstream_connect(r, u);
431 host = &u->resolved->host;
433 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
435 uscfp = umcf->upstreams.elts;
437 for (i = 0; i < umcf->upstreams.nelts; i++) {
441 if (uscf->host.len == host->len
442 && ((uscf->port == 0 && u->resolved->no_port)
443 || uscf->port == u->resolved->port)
444 && ngx_memcmp(uscf->host.data, host->data, host->len) == 0)
452 ctx = ngx_resolve_start(clcf->resolver, &temp);
454 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
458 if (ctx == NGX_NO_RESOLVER) {
459 ngx_log_error(NGX_LOG_ERR, c->log, 0,
460 "no resolver defined to resolve %V", host);
462 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY);
467 ctx->type = NGX_RESOLVE_A;
468 ctx->handler = ngx_http_upstream_resolve_handler;
470 ctx->timeout = clcf->resolver_timeout;
472 u->resolved->ctx = ctx;
474 if (ngx_resolve_name(ctx) != NGX_OK) {
475 u->resolved->ctx = NULL;
476 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
485 if (uscf->peer.init(r, uscf) != NGX_OK) {
486 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
490 ngx_http_upstream_connect(r, u);
495 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
497 ngx_http_request_t *r;
498 ngx_http_upstream_resolved_t *ur;
502 r->upstream->resolved->ctx = NULL;
505 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
506 "%V could not be resolved (%i: %s)",
507 &ctx->name, ctx->state,
508 ngx_resolver_strerror(ctx->state));
510 ngx_resolve_name_done(ctx);
511 ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY);
515 ur = r->upstream->resolved;
516 ur->naddrs = ctx->naddrs;
517 ur->addrs = ctx->addrs;
524 for (i = 0; i < ctx->naddrs; i++) {
525 addr = ntohl(ur->addrs[i]);
527 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
528 "name was resolved to %ud.%ud.%ud.%ud",
529 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
530 (addr >> 8) & 0xff, addr & 0xff);
535 if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {
536 ngx_resolve_name_done(ctx);
537 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
541 ngx_resolve_name_done(ctx);
543 ngx_http_upstream_connect(r, r->upstream);
548 ngx_http_upstream_handler(ngx_event_t *ev)
551 ngx_http_request_t *r;
552 ngx_http_log_ctx_t *ctx;
553 ngx_http_upstream_t *u;
562 ctx->current_request = r;
564 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
565 "http upstream request: \"%V?%V\"", &r->uri, &r->args);
568 u->write_event_handler(r, u);
571 u->read_event_handler(r, u);
574 ngx_http_run_posted_requests(c);
579 ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r)
581 ngx_http_upstream_check_broken_connection(r, r->connection->read);
586 ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r)
588 ngx_http_upstream_check_broken_connection(r, r->connection->write);
593 ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
600 ngx_http_upstream_t *u;
602 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,
603 "http upstream check client, write event:%d, \"%V\"",
610 ngx_http_upstream_finalize_request(r, u,
611 NGX_HTTP_CLIENT_CLOSED_REQUEST);
615 if (u->peer.connection == NULL) {
619 #if (NGX_HAVE_KQUEUE)
621 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
623 if (!ev->pending_eof) {
634 if (!u->cacheable && !u->store && u->peer.connection) {
635 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
636 "kevent() reported that client closed prematurely "
637 "connection, so upstream connection is closed too");
638 ngx_http_upstream_finalize_request(r, u,
639 NGX_HTTP_CLIENT_CLOSED_REQUEST);
643 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
644 "kevent() reported that client closed "
645 "prematurely connection");
647 if (u->peer.connection == NULL) {
648 ngx_http_upstream_finalize_request(r, u,
649 NGX_HTTP_CLIENT_CLOSED_REQUEST);
658 n = recv(c->fd, buf, 1, MSG_PEEK);
660 err = ngx_socket_errno;
662 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, err,
663 "http upstream recv(): %d", n);
666 * we do not need to disable the write event because
667 * that event has NGX_USE_CLEAR_EVENT type
670 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
674 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
675 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
676 ngx_http_upstream_finalize_request(r, u,
677 NGX_HTTP_INTERNAL_SERVER_ERROR);
687 if (err == NGX_EAGAIN) {
693 } else { /* n == 0 */
700 if (!u->cacheable && !u->store && u->peer.connection) {
701 ngx_log_error(NGX_LOG_INFO, ev->log, err,
702 "client closed prematurely connection, "
703 "so upstream connection is closed too");
704 ngx_http_upstream_finalize_request(r, u,
705 NGX_HTTP_CLIENT_CLOSED_REQUEST);
709 ngx_log_error(NGX_LOG_INFO, ev->log, err,
710 "client closed prematurely connection");
712 if (u->peer.connection == NULL) {
713 ngx_http_upstream_finalize_request(r, u,
714 NGX_HTTP_CLIENT_CLOSED_REQUEST);
721 ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
727 r->connection->log->action = "connecting to upstream";
729 r->connection->single_connection = 0;
731 if (u->state && u->state->response_sec) {
732 tp = ngx_timeofday();
733 u->state->response_sec = tp->sec - u->state->response_sec;
734 u->state->response_msec = tp->msec - u->state->response_msec;
737 u->state = ngx_array_push(r->upstream_states);
738 if (u->state == NULL) {
739 ngx_http_upstream_finalize_request(r, u,
740 NGX_HTTP_INTERNAL_SERVER_ERROR);
744 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
746 tp = ngx_timeofday();
747 u->state->response_sec = tp->sec;
748 u->state->response_msec = tp->msec;
750 rc = ngx_event_connect_peer(&u->peer);
752 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
753 "http upstream connect: %i", rc);
755 if (rc == NGX_ERROR) {
756 ngx_http_upstream_finalize_request(r, u,
757 NGX_HTTP_INTERNAL_SERVER_ERROR);
761 u->state->peer = u->peer.name;
763 if (rc == NGX_BUSY) {
764 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");
765 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);
769 if (rc == NGX_DECLINED) {
770 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
774 /* rc == NGX_OK || rc == NGX_AGAIN */
776 c = u->peer.connection;
780 c->write->handler = ngx_http_upstream_handler;
781 c->read->handler = ngx_http_upstream_handler;
783 u->write_event_handler = ngx_http_upstream_send_request_handler;
784 u->read_event_handler = ngx_http_upstream_process_header;
786 c->sendfile &= r->connection->sendfile;
787 u->output.sendfile = c->sendfile;
790 c->read->log = c->write->log = c->log = r->connection->log;
792 /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
794 u->writer.out = NULL;
795 u->writer.last = &u->writer.out;
796 u->writer.connection = c;
799 if (u->request_sent) {
800 if (ngx_http_upstream_reinit(r, u) != NGX_OK) {
801 ngx_http_upstream_finalize_request(r, u,
802 NGX_HTTP_INTERNAL_SERVER_ERROR);
808 && r->request_body->buf
809 && r->request_body->temp_file
813 * the r->request_body->buf can be reused for one request only,
814 * the subrequests should allocate their own temporay bufs
817 u->output.free = ngx_alloc_chain_link(r->pool);
818 if (u->output.free == NULL) {
819 ngx_http_upstream_finalize_request(r, u,
820 NGX_HTTP_INTERNAL_SERVER_ERROR);
824 u->output.free->buf = r->request_body->buf;
825 u->output.free->next = NULL;
826 u->output.allocated = 1;
828 r->request_body->buf->pos = r->request_body->buf->start;
829 r->request_body->buf->last = r->request_body->buf->start;
830 r->request_body->buf->tag = u->output.tag;
835 if (rc == NGX_AGAIN) {
836 ngx_add_timer(c->write, u->conf->connect_timeout);
842 if (u->ssl && c->ssl == NULL) {
843 ngx_http_upstream_ssl_init_connection(r, u, c);
849 ngx_http_upstream_send_request(r, u);
856 ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r,
857 ngx_http_upstream_t *u, ngx_connection_t *c)
861 if (ngx_ssl_create_connection(u->conf->ssl, c,
862 NGX_SSL_BUFFER|NGX_SSL_CLIENT)
865 ngx_http_upstream_finalize_request(r, u,
866 NGX_HTTP_INTERNAL_SERVER_ERROR);
871 u->output.sendfile = 0;
873 if (u->conf->ssl_session_reuse) {
874 if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) {
875 ngx_http_upstream_finalize_request(r, u,
876 NGX_HTTP_INTERNAL_SERVER_ERROR);
881 r->connection->log->action = "SSL handshaking to upstream";
883 rc = ngx_ssl_handshake(c);
885 if (rc == NGX_AGAIN) {
886 c->ssl->handler = ngx_http_upstream_ssl_handshake;
890 ngx_http_upstream_ssl_handshake(c);
895 ngx_http_upstream_ssl_handshake(ngx_connection_t *c)
897 ngx_http_request_t *r;
898 ngx_http_upstream_t *u;
903 if (c->ssl->handshaked) {
905 if (u->conf->ssl_session_reuse) {
906 u->peer.save_session(&u->peer, u->peer.data);
909 c->write->handler = ngx_http_upstream_handler;
910 c->read->handler = ngx_http_upstream_handler;
912 ngx_http_upstream_send_request(r, u);
917 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
925 ngx_http_upstream_reinit(ngx_http_request_t *r, ngx_http_upstream_t *u)
929 if (u->reinit_request(r) != NGX_OK) {
933 ngx_memzero(&u->headers_in, sizeof(ngx_http_upstream_headers_in_t));
935 if (ngx_list_init(&u->headers_in.headers, r->pool, 8,
936 sizeof(ngx_table_elt_t))
942 /* reinit the request chain */
944 for (cl = u->request_bufs; cl; cl = cl->next) {
945 cl->buf->pos = cl->buf->start;
946 cl->buf->file_pos = 0;
949 /* reinit the subrequest's ngx_output_chain() context */
951 if (r->request_body && r->request_body->temp_file
952 && r != r->main && u->output.buf)
954 u->output.free = ngx_alloc_chain_link(r->pool);
955 if (u->output.free == NULL) {
959 u->output.free->buf = u->output.buf;
960 u->output.free->next = NULL;
962 u->output.buf->pos = u->output.buf->start;
963 u->output.buf->last = u->output.buf->start;
966 u->output.buf = NULL;
968 u->output.busy = NULL;
970 /* reinit u->buffer */
974 u->buffer.pos = u->buffer.start + u->cache->ctx.header_size;
975 u->buffer.last = u->buffer.pos;
978 u->buffer.pos = u->buffer.start;
979 u->buffer.last = u->buffer.start;
983 u->buffer.pos = u->buffer.start;
984 u->buffer.last = u->buffer.start;
993 ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u)
998 c = u->peer.connection;
1000 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1001 "http upstream send request");
1003 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
1004 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1008 c->log->action = "sending request to upstream";
1010 rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs);
1012 u->request_sent = 1;
1014 if (rc == NGX_ERROR) {
1015 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1019 if (c->write->timer_set) {
1020 ngx_del_timer(c->write);
1023 if (rc == NGX_AGAIN) {
1024 ngx_add_timer(c->write, u->conf->send_timeout);
1026 if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) {
1027 ngx_http_upstream_finalize_request(r, u,
1028 NGX_HTTP_INTERNAL_SERVER_ERROR);
1037 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
1038 if (ngx_tcp_push(c->fd) == NGX_ERROR) {
1039 ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
1040 ngx_tcp_push_n " failed");
1041 ngx_http_upstream_finalize_request(r, u,
1042 NGX_HTTP_INTERNAL_SERVER_ERROR);
1046 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
1049 ngx_add_timer(c->read, u->conf->read_timeout);
1052 if (c->read->ready) {
1054 /* post aio operation */
1058 * although we can post aio operation just in the end
1059 * of ngx_http_upstream_connect() CHECK IT !!!
1060 * it's better to do here because we postpone header buffer allocation
1063 ngx_http_upstream_process_header(r, u);
1068 u->write_event_handler = ngx_http_upstream_dummy_handler;
1070 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
1071 ngx_http_upstream_finalize_request(r, u,
1072 NGX_HTTP_INTERNAL_SERVER_ERROR);
1079 ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
1080 ngx_http_upstream_t *u)
1082 ngx_connection_t *c;
1084 c = u->peer.connection;
1086 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1087 "http upstream send request handler");
1089 if (c->write->timedout) {
1090 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
1096 if (u->ssl && c->ssl == NULL) {
1097 ngx_http_upstream_ssl_init_connection(r, u, c);
1103 if (u->header_sent) {
1104 u->write_event_handler = ngx_http_upstream_dummy_handler;
1106 (void) ngx_handle_write_event(c->write, 0);
1111 ngx_http_upstream_send_request(r, u);
1116 ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)
1120 ngx_str_t *uri, args;
1121 ngx_uint_t i, flags;
1122 ngx_list_part_t *part;
1124 ngx_connection_t *c;
1125 ngx_http_upstream_header_t *hh;
1126 ngx_http_upstream_main_conf_t *umcf;
1128 c = u->peer.connection;
1130 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1131 "http upstream process header");
1133 c->log->action = "reading response header from upstream";
1135 if (c->read->timedout) {
1136 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
1140 if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
1141 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1145 if (u->buffer.start == NULL) {
1146 u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);
1147 if (u->buffer.start == NULL) {
1148 ngx_http_upstream_finalize_request(r, u,
1149 NGX_HTTP_INTERNAL_SERVER_ERROR);
1153 u->buffer.pos = u->buffer.start;
1154 u->buffer.last = u->buffer.start;
1155 u->buffer.end = u->buffer.start + u->conf->buffer_size;
1156 u->buffer.temporary = 1;
1158 u->buffer.tag = u->output.tag;
1160 if (ngx_list_init(&u->headers_in.headers, r->pool, 8,
1161 sizeof(ngx_table_elt_t))
1164 ngx_http_upstream_finalize_request(r, u,
1165 NGX_HTTP_INTERNAL_SERVER_ERROR);
1171 u->buffer.pos += u->cache->ctx.header_size;
1172 u->buffer.last = u->buffer.pos;
1179 n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);
1181 if (n == NGX_AGAIN) {
1183 ngx_add_timer(rev, u->read_timeout);
1186 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
1187 ngx_http_upstream_finalize_request(r, u,
1188 NGX_HTTP_INTERNAL_SERVER_ERROR);
1196 ngx_log_error(NGX_LOG_ERR, c->log, 0,
1197 "upstream prematurely closed connection");
1200 if (n == NGX_ERROR || n == 0) {
1201 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
1205 u->buffer.last += n;
1208 u->valid_header_in = 0;
1213 rc = u->process_header(r);
1215 if (rc == NGX_AGAIN) {
1217 if (u->buffer.pos == u->buffer.end) {
1218 ngx_log_error(NGX_LOG_ERR, c->log, 0,
1219 "upstream sent too big header");
1221 ngx_http_upstream_next(r, u,
1222 NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1232 if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {
1233 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);
1237 if (rc == NGX_ERROR) {
1238 ngx_http_upstream_finalize_request(r, u,
1239 NGX_HTTP_INTERNAL_SERVER_ERROR);
1245 if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST) {
1247 if (r->subrequest_in_memory) {
1248 u->buffer.last = u->buffer.pos;
1251 if (ngx_http_upstream_test_next(r, u) == NGX_OK) {
1255 if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) {
1260 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1262 if (u->headers_in.x_accel_redirect) {
1264 ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);
1266 part = &u->headers_in.headers.part;
1269 for (i = 0; /* void */; i++) {
1271 if (i >= part->nelts) {
1272 if (part->next == NULL) {
1281 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1282 h[i].lowcase_key, h[i].key.len);
1284 if (hh && hh->redirect) {
1285 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
1286 ngx_http_finalize_request(r,
1287 NGX_HTTP_INTERNAL_SERVER_ERROR);
1293 uri = &u->headers_in.x_accel_redirect->value;
1298 if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
1299 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1303 if (flags & NGX_HTTP_ZERO_IN_URI) {
1307 if (r->method != NGX_HTTP_HEAD) {
1308 r->method = NGX_HTTP_GET;
1311 r->valid_unparsed_uri = 0;
1313 ngx_http_internal_redirect(r, uri, &args);
1317 part = &u->headers_in.headers.part;
1320 for (i = 0; /* void */; i++) {
1322 if (i >= part->nelts) {
1323 if (part->next == NULL) {
1332 if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,
1333 h[i].lowcase_key, h[i].key.len))
1338 hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,
1339 h[i].lowcase_key, h[i].key.len);
1342 if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {
1343 ngx_http_upstream_finalize_request(r, u,
1344 NGX_HTTP_INTERNAL_SERVER_ERROR);
1351 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {
1352 ngx_http_upstream_finalize_request(r, u,
1353 NGX_HTTP_INTERNAL_SERVER_ERROR);
1358 if (r->headers_out.server && r->headers_out.server->value.data == NULL) {
1359 r->headers_out.server->hash = 0;
1362 if (r->headers_out.date && r->headers_out.date->value.data == NULL) {
1363 r->headers_out.date->hash = 0;
1366 r->headers_out.status = u->headers_in.status_n;
1367 r->headers_out.status_line = u->headers_in.status_line;
1369 u->headers_in.content_length_n = r->headers_out.content_length_n;
1371 if (r->headers_out.content_length_n != -1) {
1372 u->length = (size_t) r->headers_out.content_length_n;
1375 u->length = NGX_MAX_SIZE_T_VALUE;
1378 if (!r->subrequest_in_memory) {
1379 ngx_http_upstream_send_response(r, u);
1383 /* subrequest content in memory */
1385 if (u->input_filter == NULL) {
1386 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1387 u->input_filter = ngx_http_upstream_non_buffered_filter;
1388 u->input_filter_ctx = r;
1391 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1392 ngx_http_upstream_finalize_request(r, u,
1393 NGX_HTTP_INTERNAL_SERVER_ERROR);
1397 n = u->buffer.last - u->buffer.pos;
1400 u->buffer.last -= n;
1402 u->state->response_length += n;
1404 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1405 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1409 if (u->length == 0) {
1410 ngx_http_upstream_finalize_request(r, u, 0);
1415 u->read_event_handler = ngx_http_upstream_process_body_in_memory;
1417 ngx_http_upstream_process_body_in_memory(r, u);
1422 ngx_http_upstream_test_next(ngx_http_request_t *r, ngx_http_upstream_t *u)
1425 ngx_http_upstream_next_t *un;
1427 if (!(u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_STATUS)) {
1428 return NGX_DECLINED;
1431 status = u->headers_in.status_n;
1433 for (un = ngx_http_upstream_next_errors; un->status; un++) {
1435 if (status != un->status) {
1439 if (u->peer.tries > 1 && (u->conf->next_upstream & un->mask)) {
1440 ngx_http_upstream_next(r, u, un->mask);
1444 #if (NGX_HTTP_CACHE)
1446 if (u->peer.tries == 0 && u->stale && (u->conf->use_stale & un->mask)) {
1447 ngx_http_upstream_finalize_request(r, u,
1448 ngx_http_send_cached_response(r));
1455 return NGX_DECLINED;
1460 ngx_http_upstream_intercept_errors(ngx_http_request_t *r,
1461 ngx_http_upstream_t *u)
1466 ngx_http_err_page_t *err_page;
1467 ngx_http_core_loc_conf_t *clcf;
1469 status = u->headers_in.status_n;
1471 if (status == NGX_HTTP_NOT_FOUND && u->conf->intercept_404) {
1472 ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND);
1476 if (!u->conf->intercept_errors) {
1477 return NGX_DECLINED;
1480 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1482 if (clcf->error_pages == NULL) {
1483 return NGX_DECLINED;
1486 err_page = clcf->error_pages->elts;
1487 for (i = 0; i < clcf->error_pages->nelts; i++) {
1489 if (err_page[i].status == status) {
1491 if (status == NGX_HTTP_UNAUTHORIZED) {
1493 h = ngx_list_push(&r->headers_out.headers);
1496 ngx_http_upstream_finalize_request(r, u,
1497 NGX_HTTP_INTERNAL_SERVER_ERROR);
1501 *h = *u->headers_in.www_authenticate;
1503 r->headers_out.www_authenticate = h;
1506 ngx_http_upstream_finalize_request(r, u, status);
1512 return NGX_DECLINED;
1517 ngx_http_upstream_test_connect(ngx_connection_t *c)
1522 #if (NGX_HAVE_KQUEUE)
1524 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
1525 if (c->write->pending_eof) {
1526 c->log->action = "connecting to upstream";
1527 (void) ngx_connection_error(c, c->write->kq_errno,
1528 "kevent() reported that connect() failed");
1539 * BSDs and Linux return 0 and set a pending error in err
1540 * Solaris returns -1 and sets errno
1543 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
1550 c->log->action = "connecting to upstream";
1551 (void) ngx_connection_error(c, err, "connect() failed");
1561 ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r,
1562 ngx_http_upstream_t *u)
1568 ngx_connection_t *c;
1570 c = u->peer.connection;
1573 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1574 "http upstream process body on memory");
1576 if (rev->timedout) {
1577 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1578 ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT);
1586 size = b->end - b->last;
1589 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
1590 "upstream buffer is too small to read repsonse");
1591 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1595 n = c->recv(c, b->last, size);
1597 if (n == NGX_AGAIN) {
1601 if (n == 0 || n == NGX_ERROR) {
1602 ngx_http_upstream_finalize_request(r, u, n);
1606 u->state->response_length += n;
1608 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1609 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1618 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1619 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
1624 ngx_add_timer(rev, u->conf->read_timeout);
1626 } else if (rev->timer_set) {
1633 ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
1638 ngx_event_pipe_t *p;
1639 ngx_connection_t *c;
1640 ngx_pool_cleanup_t *cl;
1641 ngx_pool_cleanup_file_t *clf;
1642 ngx_http_core_loc_conf_t *clcf;
1644 rc = ngx_http_send_header(r);
1646 if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) {
1647 ngx_http_upstream_finalize_request(r, u, rc);
1653 if (r->request_body && r->request_body->temp_file) {
1654 for (cl = r->pool->cleanup; cl; cl = cl->next) {
1655 if (cl->handler == ngx_pool_cleanup_file) {
1658 if (clf->fd == r->request_body->temp_file->file.fd) {
1661 r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
1670 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1672 if (!u->buffering) {
1674 if (u->input_filter == NULL) {
1675 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1676 u->input_filter = ngx_http_upstream_non_buffered_filter;
1677 u->input_filter_ctx = r;
1680 u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream;
1681 r->write_event_handler =
1682 ngx_http_upstream_process_non_buffered_downstream;
1686 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
1687 ngx_http_upstream_finalize_request(r, u, 0);
1691 if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
1692 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
1696 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
1697 (const void *) &tcp_nodelay, sizeof(int)) == -1)
1699 ngx_connection_error(c, ngx_socket_errno,
1700 "setsockopt(TCP_NODELAY) failed");
1701 ngx_http_upstream_finalize_request(r, u, 0);
1705 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
1708 n = u->buffer.last - u->buffer.pos;
1711 u->buffer.last = u->buffer.pos;
1713 u->state->response_length += n;
1715 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1716 ngx_http_upstream_finalize_request(r, u, 0);
1720 ngx_http_upstream_process_non_buffered_downstream(r);
1723 u->buffer.pos = u->buffer.start;
1724 u->buffer.last = u->buffer.start;
1726 if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {
1727 ngx_http_upstream_finalize_request(r, u, 0);
1731 if (u->peer.connection->read->ready) {
1732 ngx_http_upstream_process_non_buffered_upstream(r, u);
1739 /* TODO: preallocate event_pipe bufs, look "Content-Length" */
1743 if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) {
1744 if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) {
1745 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
1746 ngx_close_file_n " \"%s\" failed",
1747 u->cache->ctx.file.name.data);
1752 header = (ngx_http_cache_header_t *) u->buffer->start;
1754 header->expires = u->cache->ctx.expires;
1755 header->last_modified = u->cache->ctx.last_modified;
1756 header->date = u->cache->ctx.date;
1757 header->length = r->headers_out.content_length_n;
1758 u->cache->ctx.length = r->headers_out.content_length_n;
1760 header->key_len = u->cache->ctx.key0.len;
1761 ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len);
1762 header->key[header->key_len] = LF;
1769 p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;
1771 p->tag = u->output.tag;
1772 p->bufs = u->conf->bufs;
1773 p->busy_size = u->conf->busy_buffers_size;
1774 p->upstream = u->peer.connection;
1779 p->cacheable = u->cacheable || u->store;
1781 p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
1782 if (p->temp_file == NULL) {
1783 ngx_http_upstream_finalize_request(r, u, 0);
1787 p->temp_file->file.fd = NGX_INVALID_FILE;
1788 p->temp_file->file.log = c->log;
1789 p->temp_file->path = u->conf->temp_path;
1790 p->temp_file->pool = r->pool;
1792 if (u->cacheable || u->store) {
1793 p->temp_file->persistent = 1;
1796 p->temp_file->log_level = NGX_LOG_WARN;
1797 p->temp_file->warn = "an upstream response is buffered "
1798 "to a temporary file";
1801 p->max_temp_file_size = u->conf->max_temp_file_size;
1802 p->temp_file_write_size = u->conf->temp_file_write_size;
1804 p->preread_bufs = ngx_alloc_chain_link(r->pool);
1805 if (p->preread_bufs == NULL) {
1806 ngx_http_upstream_finalize_request(r, u, 0);
1810 p->preread_bufs->buf = &u->buffer;
1811 p->preread_bufs->next = NULL;
1812 u->buffer.recycled = 1;
1814 p->preread_size = u->buffer.last - u->buffer.pos;
1818 p->buf_to_file = ngx_calloc_buf(r->pool);
1819 if (p->buf_to_file == NULL) {
1820 ngx_http_upstream_finalize_request(r, u, 0);
1824 p->buf_to_file->pos = u->buffer.start;
1825 p->buf_to_file->last = u->buffer.pos;
1826 p->buf_to_file->temporary = 1;
1829 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
1830 /* the posted aio operation may currupt a shadow buffer */
1834 /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */
1838 * event_pipe would do u->buffer.last += p->preread_size
1839 * as though these bytes were read
1841 u->buffer.last = u->buffer.pos;
1843 if (u->conf->cyclic_temp_file) {
1846 * we need to disable the use of sendfile() if we use cyclic temp file
1847 * because the writing a new data may interfere with sendfile()
1848 * that uses the same kernel file pages (at least on FreeBSD)
1851 p->cyclic_temp_file = 1;
1855 p->cyclic_temp_file = 0;
1858 p->read_timeout = u->conf->read_timeout;
1859 p->send_timeout = clcf->send_timeout;
1860 p->send_lowat = clcf->send_lowat;
1862 u->read_event_handler = ngx_http_upstream_process_upstream;
1863 r->write_event_handler = ngx_http_upstream_process_downstream;
1865 ngx_http_upstream_process_upstream(r, u);
1870 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
1873 ngx_connection_t *c;
1874 ngx_http_upstream_t *u;
1880 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1881 "http upstream process non buffered downstream");
1883 c->log->action = "sending to client";
1885 if (wev->timedout) {
1887 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1888 ngx_http_upstream_finalize_request(r, u, 0);
1892 ngx_http_upstream_process_non_buffered_request(r, 1);
1897 ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r,
1898 ngx_http_upstream_t *u)
1900 ngx_connection_t *c;
1902 c = u->peer.connection;
1904 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1905 "http upstream process non buffered upstream");
1907 c->log->action = "reading upstream";
1909 if (c->read->timedout) {
1910 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1911 ngx_http_upstream_finalize_request(r, u, 0);
1915 ngx_http_upstream_process_non_buffered_request(r, 0);
1920 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
1921 ngx_uint_t do_write)
1927 ngx_connection_t *downstream, *upstream;
1928 ngx_http_upstream_t *u;
1929 ngx_http_core_loc_conf_t *clcf;
1932 downstream = r->connection;
1933 upstream = u->peer.connection;
1937 do_write = do_write || u->length == 0;
1943 if (u->out_bufs || u->busy_bufs) {
1944 rc = ngx_http_output_filter(r, u->out_bufs);
1946 if (downstream->destroyed) {
1950 if (rc == NGX_ERROR) {
1951 ngx_http_upstream_finalize_request(r, u, 0);
1955 ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs,
1956 &u->out_bufs, u->output.tag);
1959 if (u->busy_bufs == NULL) {
1962 || upstream->read->eof
1963 || upstream->read->error)
1965 ngx_http_upstream_finalize_request(r, u, 0);
1974 size = b->end - b->last;
1976 if (size > u->length) {
1980 if (size && upstream->read->ready) {
1982 n = upstream->recv(upstream, b->last, size);
1984 if (n == NGX_AGAIN) {
1989 u->state->response_length += n;
1991 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
1992 ngx_http_upstream_finalize_request(r, u, 0);
2005 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2007 if (downstream->data == r) {
2008 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
2011 ngx_http_upstream_finalize_request(r, u, 0);
2016 if (downstream->write->active && !downstream->write->ready) {
2017 ngx_add_timer(downstream->write, clcf->send_timeout);
2019 } else if (downstream->write->timer_set) {
2020 ngx_del_timer(downstream->write);
2023 if (ngx_handle_read_event(upstream->read, 0) != NGX_OK) {
2024 ngx_http_upstream_finalize_request(r, u, 0);
2028 if (upstream->read->active && !upstream->read->ready) {
2029 ngx_add_timer(upstream->read, u->conf->read_timeout);
2031 } else if (upstream->read->timer_set) {
2032 ngx_del_timer(upstream->read);
2038 ngx_http_upstream_non_buffered_filter_init(void *data)
2045 ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes)
2047 ngx_http_request_t *r = data;
2050 ngx_chain_t *cl, **ll;
2051 ngx_http_upstream_t *u;
2055 for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
2059 cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
2067 cl->buf->memory = 1;
2071 cl->buf->pos = b->last;
2073 cl->buf->last = b->last;
2074 cl->buf->tag = u->output.tag;
2076 if (u->length == NGX_MAX_SIZE_T_VALUE) {
2087 ngx_http_upstream_process_downstream(ngx_http_request_t *r)
2090 ngx_connection_t *c;
2091 ngx_event_pipe_t *p;
2092 ngx_http_upstream_t *u;
2099 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2100 "http upstream process downstream");
2102 c->log->action = "sending to client";
2104 if (wev->timedout) {
2112 ngx_add_timer(wev, p->send_timeout);
2114 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
2115 ngx_http_upstream_finalize_request(r, u, 0);
2121 if (ngx_event_pipe(p, wev->write) == NGX_ABORT) {
2127 ngx_http_upstream_finalize_request(r, u, 0);
2132 p->downstream_error = 1;
2134 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
2141 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2142 "http downstream delayed");
2144 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
2145 ngx_http_upstream_finalize_request(r, u, 0);
2151 if (ngx_event_pipe(p, 1) == NGX_ABORT) {
2157 ngx_http_upstream_finalize_request(r, u, 0);
2162 ngx_http_upstream_process_request(r);
2167 ngx_http_upstream_process_upstream(ngx_http_request_t *r,
2168 ngx_http_upstream_t *u)
2170 ngx_connection_t *c;
2172 c = u->peer.connection;
2174 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2175 "http upstream process upstream");
2177 c->log->action = "reading upstream";
2179 if (c->read->timedout) {
2180 u->pipe->upstream_error = 1;
2181 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
2186 if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) {
2192 ngx_http_upstream_finalize_request(r, u, 0);
2197 ngx_http_upstream_process_request(r);
2202 ngx_http_upstream_process_request(ngx_http_request_t *r)
2205 ngx_temp_file_t *tf;
2206 ngx_event_pipe_t *p;
2207 ngx_http_upstream_t *u;
2212 if (u->peer.connection) {
2216 del = p->upstream_error;
2218 tf = u->pipe->temp_file;
2220 if (p->upstream_eof || p->upstream_done) {
2222 if (u->headers_in.status_n == NGX_HTTP_OK
2223 && (u->headers_in.content_length_n == -1
2224 || (u->headers_in.content_length_n == tf->offset)))
2226 ngx_http_upstream_store(r, u);
2233 if (del && tf->file.fd != NGX_INVALID_FILE) {
2235 if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
2237 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
2238 ngx_delete_file_n " \"%s\" failed",
2239 u->pipe->temp_file->file.name.data);
2244 #if (NGX_HTTP_FILE_CACHE)
2246 if (p->upstream_done && u->cacheable) {
2247 if (ngx_http_cache_update(r) == NGX_ERROR) {
2248 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2249 ngx_http_upstream_finalize_request(r, u, 0);
2253 } else if (p->upstream_eof && u->cacheable) {
2255 /* TODO: check length & update cache */
2257 if (ngx_http_cache_update(r) == NGX_ERROR) {
2258 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2259 ngx_http_upstream_finalize_request(r, u, 0);
2266 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
2267 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2268 "http upstream exit: %p", p->out);
2270 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2272 ngx_http_upstream_finalize_request(r, u, 0);
2277 if (p->downstream_error) {
2278 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2279 "http upstream downstream error");
2281 if (!u->cacheable && u->peer.connection) {
2282 ngx_http_upstream_finalize_request(r, u, 0);
2289 ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u)
2294 ngx_temp_file_t *tf;
2295 ngx_ext_rename_file_t ext;
2297 tf = u->pipe->temp_file;
2299 if (tf->file.fd == NGX_INVALID_FILE) {
2301 /* create file for empty 200 response */
2303 tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));
2308 tf->file.fd = NGX_INVALID_FILE;
2309 tf->file.log = r->connection->log;
2310 tf->path = u->conf->temp_path;
2314 if (ngx_create_temp_file(&tf->file, tf->path, tf->pool,
2315 tf->persistent, tf->clean, tf->access)
2321 u->pipe->temp_file = tf;
2324 ext.access = u->conf->store_access;
2325 ext.path_access = u->conf->store_access;
2327 ext.create_path = 1;
2328 ext.delete_file = 1;
2329 ext.log_rename_error = 1;
2330 ext.log = r->connection->log;
2332 if (u->headers_in.last_modified) {
2334 lm = ngx_http_parse_time(u->headers_in.last_modified->value.data,
2335 u->headers_in.last_modified->value.len);
2337 if (lm != NGX_ERROR) {
2339 ext.fd = tf->file.fd;
2343 if (u->conf->store_lengths == NULL) {
2345 ngx_http_map_uri_to_path(r, &path, &root, 0);
2348 if (ngx_http_script_run(r, &path, u->conf->store_lengths->elts, 0,
2349 u->conf->store_values->elts)
2356 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2357 "upstream stores \"%s\" to \"%s\"",
2358 tf->file.name.data, path.data);
2360 (void) ngx_ext_rename_file(&tf->file.name, &path, &ext);
2365 ngx_http_upstream_dummy_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
2367 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2368 "http upstream dummy handler");
2373 ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
2376 ngx_uint_t status, state;
2378 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2379 "http next upstream, %xi", ft_type);
2382 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2385 if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
2386 state = NGX_PEER_NEXT;
2388 state = NGX_PEER_FAILED;
2391 if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
2392 u->peer.free(&u->peer, u->peer.data, state);
2395 if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
2396 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
2397 "upstream timed out");
2400 if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
2406 case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
2407 status = NGX_HTTP_GATEWAY_TIME_OUT;
2410 case NGX_HTTP_UPSTREAM_FT_HTTP_500:
2411 status = NGX_HTTP_INTERNAL_SERVER_ERROR;
2414 case NGX_HTTP_UPSTREAM_FT_HTTP_404:
2415 status = NGX_HTTP_NOT_FOUND;
2419 * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
2424 status = NGX_HTTP_BAD_GATEWAY;
2428 if (r->connection->error) {
2429 ngx_http_upstream_finalize_request(r, u,
2430 NGX_HTTP_CLIENT_CLOSED_REQUEST);
2435 u->state->status = status;
2437 if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
2439 #if (NGX_HTTP_CACHE)
2441 if (u->stale && (u->conf->use_stale & ft_type)) {
2442 ngx_http_upstream_finalize_request(r, u,
2443 ngx_http_send_cached_response(r));
2449 ngx_http_upstream_finalize_request(r, u, status);
2454 if (u->peer.connection) {
2455 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2456 "close http upstream connection: %d",
2457 u->peer.connection->fd);
2460 if (u->peer.connection->ssl) {
2461 u->peer.connection->ssl->no_wait_shutdown = 1;
2462 u->peer.connection->ssl->no_send_shutdown = 1;
2464 (void) ngx_ssl_shutdown(u->peer.connection);
2468 ngx_close_connection(u->peer.connection);
2472 if (u->conf->busy_lock && !u->busy_locked) {
2473 ngx_http_upstream_busy_lock(p);
2478 ngx_http_upstream_connect(r, u);
2483 ngx_http_upstream_cleanup(void *data)
2485 ngx_http_request_t *r = data;
2487 ngx_http_upstream_t *u;
2489 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2490 "cleanup http upstream request: \"%V\"", &r->uri);
2494 if (u->resolved && u->resolved->ctx) {
2495 ngx_resolve_name_done(u->resolved->ctx);
2498 ngx_http_upstream_finalize_request(r, u, NGX_DONE);
2503 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
2504 ngx_http_upstream_t *u, ngx_int_t rc)
2508 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2509 "finalize http upstream request: %i", rc);
2515 if (u->state && u->state->response_sec) {
2516 tp = ngx_timeofday();
2517 u->state->response_sec = tp->sec - u->state->response_sec;
2518 u->state->response_msec = tp->msec - u->state->response_msec;
2521 u->state->response_length = u->pipe->read_length;
2525 u->finalize_request(r, rc);
2528 u->peer.free(&u->peer, u->peer.data, 0);
2531 if (u->peer.connection) {
2535 /* TODO: do not shutdown persistent connection */
2537 if (u->peer.connection->ssl) {
2540 * We send the "close notify" shutdown alert to the upstream only
2541 * and do not wait its "close notify" shutdown alert.
2542 * It is acceptable according to the TLS standard.
2545 u->peer.connection->ssl->no_wait_shutdown = 1;
2547 (void) ngx_ssl_shutdown(u->peer.connection);
2551 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2552 "close http upstream connection: %d",
2553 u->peer.connection->fd);
2555 ngx_close_connection(u->peer.connection);
2558 u->peer.connection = NULL;
2560 if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
2565 if (u->pipe && u->pipe->temp_file) {
2566 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2567 "http upstream temp fd: %d",
2568 u->pipe->temp_file->file.fd);
2573 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2574 "http upstream cache fd: %d",
2575 u->cache->ctx.file.fd);
2579 if (rc == NGX_DECLINED) {
2583 r->connection->log->action = "sending to client";
2585 if (rc == 0 && r == r->main && !r->post_action) {
2586 rc = ngx_http_send_special(r, NGX_HTTP_LAST);
2589 ngx_http_finalize_request(r, rc);
2594 ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2597 ngx_table_elt_t **ph;
2599 ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset);
2610 ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r,
2611 ngx_table_elt_t *h, ngx_uint_t offset)
2614 ngx_table_elt_t **ph;
2616 pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset);
2618 if (pa->elts == NULL) {
2619 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2625 ph = ngx_array_push(pa);
2637 ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2645 ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h,
2650 r->upstream->headers_in.x_accel_limit_rate = h;
2652 n = ngx_atoi(h->value.data, h->value.len);
2654 if (n != NGX_ERROR) {
2655 r->limit_rate = (size_t) n;
2663 ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h,
2668 if (r->upstream->conf->change_buffering) {
2670 if (h->value.len == 2) {
2671 c0 = ngx_tolower(h->value.data[0]);
2672 c1 = ngx_tolower(h->value.data[1]);
2674 if (c0 == 'n' && c1 == 'o') {
2675 r->upstream->buffering = 0;
2678 } else if (h->value.len == 3) {
2679 c0 = ngx_tolower(h->value.data[0]);
2680 c1 = ngx_tolower(h->value.data[1]);
2681 c2 = ngx_tolower(h->value.data[2]);
2683 if (c0 == 'y' && c1 == 'e' && c2 == 's') {
2684 r->upstream->buffering = 1;
2694 ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h,
2697 r->headers_out.override_charset = &h->value;
2704 ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
2707 ngx_table_elt_t *ho, **ph;
2709 ho = ngx_list_push(&r->headers_out.headers);
2717 ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset);
2726 ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r,
2727 ngx_table_elt_t *h, ngx_uint_t offset)
2730 ngx_table_elt_t *ho, **ph;
2732 pa = (ngx_array_t *) ((char *) &r->headers_out + offset);
2734 if (pa->elts == NULL) {
2735 if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK)
2741 ph = ngx_array_push(pa);
2746 ho = ngx_list_push(&r->headers_out.headers);
2759 ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
2764 r->headers_out.content_type_len = h->value.len;
2765 r->headers_out.content_type = h->value;
2767 for (p = h->value.data; *p; p++) {
2775 while (*++p == ' ') { /* void */ }
2781 if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) {
2787 r->headers_out.content_type_len = last - h->value.data;
2793 last = h->value.data + h->value.len;
2795 if (*(last - 1) == '"') {
2799 r->headers_out.charset.len = last - p;
2800 r->headers_out.charset.data = p;
2810 ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h,
2813 ngx_table_elt_t *ho;
2815 ho = ngx_list_push(&r->headers_out.headers);
2822 r->headers_out.content_length = ho;
2823 r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len);
2830 ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h,
2834 ngx_table_elt_t *ho;
2836 ho = ngx_list_push(&r->headers_out.headers);
2843 if (r->upstream->rewrite_redirect) {
2844 rc = r->upstream->rewrite_redirect(r, ho, 0);
2846 if (rc == NGX_DECLINED) {
2851 r->headers_out.location = ho;
2853 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2854 "rewritten location: \"%V\"", &ho->value);
2860 if (ho->value.data[0] != '/') {
2861 r->headers_out.location = ho;
2865 * we do not set r->headers_out.location here to avoid the handling
2866 * the local redirects without a host name by ngx_http_header_filter()
2874 ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h,
2879 ngx_table_elt_t *ho;
2881 ho = ngx_list_push(&r->headers_out.headers);
2888 if (r->upstream->rewrite_redirect) {
2890 p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1);
2893 rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data);
2899 if (rc == NGX_DECLINED) {
2904 r->headers_out.refresh = ho;
2906 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2907 "rewritten refresh: \"%V\"", &ho->value);
2913 r->headers_out.refresh = ho;
2922 ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
2923 ngx_table_elt_t *h, ngx_uint_t offset)
2925 ngx_table_elt_t *ho;
2927 ho = ngx_list_push(&r->headers_out.headers);
2934 r->headers_out.content_encoding = ho;
2943 ngx_http_upstream_add_variables(ngx_conf_t *cf)
2945 ngx_http_variable_t *var, *v;
2947 for (v = ngx_http_upstream_vars; v->name.len; v++) {
2948 var = ngx_http_add_variable(cf, &v->name, v->flags);
2953 var->get_handler = v->get_handler;
2954 var->data = v->data;
2962 ngx_http_upstream_addr_variable(ngx_http_request_t *r,
2963 ngx_http_variable_value_t *v, uintptr_t data)
2968 ngx_http_upstream_state_t *state;
2971 v->no_cacheable = 0;
2974 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2980 state = r->upstream_states->elts;
2982 for (i = 0; i < r->upstream_states->nelts; i++) {
2983 if (state[i].peer) {
2984 len += state[i].peer->len + 2;
2991 p = ngx_pnalloc(r->pool, len);
3001 if (state[i].peer) {
3002 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
3005 if (++i == r->upstream_states->nelts) {
3009 if (state[i].peer) {
3018 if (++i == r->upstream_states->nelts) {
3026 v->len = p - v->data;
3033 ngx_http_upstream_status_variable(ngx_http_request_t *r,
3034 ngx_http_variable_value_t *v, uintptr_t data)
3039 ngx_http_upstream_state_t *state;
3042 v->no_cacheable = 0;
3045 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
3050 len = r->upstream_states->nelts * (3 + 2);
3052 p = ngx_pnalloc(r->pool, len);
3060 state = r->upstream_states->elts;
3063 if (state[i].status) {
3064 p = ngx_sprintf(p, "%ui", state[i].status);
3070 if (++i == r->upstream_states->nelts) {
3074 if (state[i].peer) {
3083 if (++i == r->upstream_states->nelts) {
3091 v->len = p - v->data;
3098 ngx_http_upstream_response_time_variable(ngx_http_request_t *r,
3099 ngx_http_variable_value_t *v, uintptr_t data)
3105 ngx_http_upstream_state_t *state;
3108 v->no_cacheable = 0;
3111 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
3116 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
3118 p = ngx_pnalloc(r->pool, len);
3126 state = r->upstream_states->elts;
3129 if (state[i].status) {
3130 ms = (ngx_msec_int_t)
3131 (state[i].response_sec * 1000 + state[i].response_msec);
3132 ms = (ms >= 0) ? ms : 0;
3133 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
3139 if (++i == r->upstream_states->nelts) {
3143 if (state[i].peer) {
3152 if (++i == r->upstream_states->nelts) {
3160 v->len = p - v->data;
3167 ngx_http_upstream_response_length_variable(ngx_http_request_t *r,
3168 ngx_http_variable_value_t *v, uintptr_t data)
3173 ngx_http_upstream_state_t *state;
3176 v->no_cacheable = 0;
3179 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
3184 len = r->upstream_states->nelts * (NGX_OFF_T_LEN + 2);
3186 p = ngx_pnalloc(r->pool, len);
3194 state = r->upstream_states->elts;
3197 p = ngx_sprintf(p, "%O", state[i].response_length);
3199 if (++i == r->upstream_states->nelts) {
3203 if (state[i].peer) {
3212 if (++i == r->upstream_states->nelts) {
3220 v->len = p - v->data;
3227 ngx_http_upstream_header_variable(ngx_http_request_t *r,
3228 ngx_http_variable_value_t *v, uintptr_t data)
3230 if (r->upstream == NULL) {
3235 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
3236 &r->upstream->headers_in.headers.part,
3237 sizeof("upstream_http_") - 1);
3242 ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
3250 ngx_http_module_t *module;
3251 ngx_http_conf_ctx_t *ctx, *http_ctx;
3252 ngx_http_upstream_srv_conf_t *uscf;
3254 ngx_memzero(&u, sizeof(ngx_url_t));
3256 value = cf->args->elts;
3260 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
3261 |NGX_HTTP_UPSTREAM_WEIGHT
3262 |NGX_HTTP_UPSTREAM_MAX_FAILS
3263 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
3264 |NGX_HTTP_UPSTREAM_DOWN
3265 |NGX_HTTP_UPSTREAM_BACKUP);
3267 return NGX_CONF_ERROR;
3271 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
3273 return NGX_CONF_ERROR;
3277 ctx->main_conf = http_ctx->main_conf;
3279 /* the upstream{}'s srv_conf */
3281 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3282 if (ctx->srv_conf == NULL) {
3283 return NGX_CONF_ERROR;
3286 ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
3288 uscf->srv_conf = ctx->srv_conf;
3291 /* the upstream{}'s loc_conf */
3293 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3294 if (ctx->loc_conf == NULL) {
3295 return NGX_CONF_ERROR;
3298 for (m = 0; ngx_modules[m]; m++) {
3299 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
3303 module = ngx_modules[m]->ctx;
3305 if (module->create_srv_conf) {
3306 mconf = module->create_srv_conf(cf);
3307 if (mconf == NULL) {
3308 return NGX_CONF_ERROR;
3311 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
3314 if (module->create_loc_conf) {
3315 mconf = module->create_loc_conf(cf);
3316 if (mconf == NULL) {
3317 return NGX_CONF_ERROR;
3320 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
3325 /* parse inside upstream{} */
3329 cf->cmd_type = NGX_HTTP_UPS_CONF;
3331 rv = ngx_conf_parse(cf, NULL);
3335 if (rv != NGX_CONF_OK) {
3339 if (uscf->servers == NULL) {
3340 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3341 "no servers are inside upstream");
3342 return NGX_CONF_ERROR;
3350 ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3352 ngx_http_upstream_srv_conf_t *uscf = conf;
3354 time_t fail_timeout;
3355 ngx_str_t *value, s;
3357 ngx_int_t weight, max_fails;
3359 ngx_http_upstream_server_t *us;
3361 if (uscf->servers == NULL) {
3362 uscf->servers = ngx_array_create(cf->pool, 4,
3363 sizeof(ngx_http_upstream_server_t));
3364 if (uscf->servers == NULL) {
3365 return NGX_CONF_ERROR;
3369 us = ngx_array_push(uscf->servers);
3371 return NGX_CONF_ERROR;
3374 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
3376 value = cf->args->elts;
3378 ngx_memzero(&u, sizeof(ngx_url_t));
3381 u.default_port = 80;
3383 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
3385 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3386 "%s in upstream \"%V\"", u.err, &u.url);
3389 return NGX_CONF_ERROR;
3396 for (i = 2; i < cf->args->nelts; i++) {
3398 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) {
3400 if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) {
3404 weight = ngx_atoi(&value[i].data[7], value[i].len - 7);
3406 if (weight == NGX_ERROR || weight == 0) {
3413 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
3415 if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) {
3419 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10);
3421 if (max_fails == NGX_ERROR) {
3428 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) {
3430 if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) {
3434 s.len = value[i].len - 13;
3435 s.data = &value[i].data[13];
3437 fail_timeout = ngx_parse_time(&s, 1);
3439 if (fail_timeout == NGX_ERROR) {
3446 if (ngx_strncmp(value[i].data, "backup", 6) == 0) {
3448 if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) {
3457 if (ngx_strncmp(value[i].data, "down", 4) == 0) {
3459 if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) {
3471 us->addrs = u.addrs;
3472 us->naddrs = u.naddrs;
3473 us->weight = weight;
3474 us->max_fails = max_fails;
3475 us->fail_timeout = fail_timeout;
3481 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3482 "invalid parameter \"%V\"", &value[i]);
3484 return NGX_CONF_ERROR;
3488 ngx_http_upstream_srv_conf_t *
3489 ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
3492 ngx_http_upstream_server_t *us;
3493 ngx_http_upstream_srv_conf_t *uscf, **uscfp;
3494 ngx_http_upstream_main_conf_t *umcf;
3496 if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) {
3498 if (ngx_parse_url(cf->pool, u) != NGX_OK) {
3500 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3501 "%s in upstream \"%V\"", u->err, &u->url);
3508 umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
3510 uscfp = umcf->upstreams.elts;
3512 for (i = 0; i < umcf->upstreams.nelts; i++) {
3514 if (uscfp[i]->host.len != u->host.len
3515 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
3521 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
3522 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
3524 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3525 "duplicate upstream \"%V\"", &u->host);
3529 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
3530 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3531 "upstream \"%V\" may not have port %d",
3536 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
3537 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
3538 "upstream \"%V\" may not have port %d in %s:%ui",
3539 &u->host, uscfp[i]->port,
3540 uscfp[i]->file_name, uscfp[i]->line);
3544 if (uscfp[i]->port != u->port) {
3548 if (uscfp[i]->default_port && u->default_port
3549 && uscfp[i]->default_port != u->default_port)
3557 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t));
3562 uscf->flags = flags;
3563 uscf->host = u->host;
3564 uscf->file_name = cf->conf_file->file.name.data;
3565 uscf->line = cf->conf_file->line;
3566 uscf->port = u->port;
3567 uscf->default_port = u->default_port;
3569 if (u->naddrs == 1) {
3570 uscf->servers = ngx_array_create(cf->pool, 1,
3571 sizeof(ngx_http_upstream_server_t));
3572 if (uscf->servers == NULL) {
3573 return NGX_CONF_ERROR;
3576 us = ngx_array_push(uscf->servers);
3578 return NGX_CONF_ERROR;
3581 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
3583 us->addrs = u->addrs;
3584 us->naddrs = u->naddrs;
3587 uscfp = ngx_array_push(&umcf->upstreams);
3588 if (uscfp == NULL) {
3599 ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
3600 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
3601 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash)
3605 ngx_array_t hide_headers;
3608 if (conf->hide_headers == NGX_CONF_UNSET_PTR
3609 && conf->pass_headers == NGX_CONF_UNSET_PTR)
3611 conf->hide_headers_hash = prev->hide_headers_hash;
3613 if (conf->hide_headers_hash.buckets) {
3617 conf->hide_headers = prev->hide_headers;
3618 conf->pass_headers = prev->pass_headers;
3621 if (conf->hide_headers == NGX_CONF_UNSET_PTR) {
3622 conf->hide_headers = prev->hide_headers;
3625 if (conf->pass_headers == NGX_CONF_UNSET_PTR) {
3626 conf->pass_headers = prev->pass_headers;
3630 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
3636 for (h = default_hide_headers; h->len; h++) {
3637 hk = ngx_array_push(&hide_headers);
3643 hk->key_hash = ngx_hash_key_lc(h->data, h->len);
3644 hk->value = (void *) 1;
3647 if (conf->hide_headers != NGX_CONF_UNSET_PTR) {
3649 h = conf->hide_headers->elts;
3651 for (i = 0; i < conf->hide_headers->nelts; i++) {
3653 hk = hide_headers.elts;
3655 for (j = 0; j < hide_headers.nelts; j++) {
3656 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
3661 hk = ngx_array_push(&hide_headers);
3667 hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len);
3668 hk->value = (void *) 1;
3676 if (conf->pass_headers != NGX_CONF_UNSET_PTR) {
3678 h = conf->pass_headers->elts;
3679 hk = hide_headers.elts;
3681 for (i = 0; i < conf->pass_headers->nelts; i++) {
3682 for (j = 0; j < hide_headers.nelts; j++) {
3684 if (hk[j].key.data == NULL) {
3688 if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) {
3689 hk[j].key.data = NULL;
3696 hash->hash = &conf->hide_headers_hash;
3697 hash->key = ngx_hash_key_lc;
3698 hash->pool = cf->pool;
3699 hash->temp_pool = NULL;
3701 return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts);
3706 ngx_http_upstream_create_main_conf(ngx_conf_t *cf)
3708 ngx_http_upstream_main_conf_t *umcf;
3710 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t));
3715 if (ngx_array_init(&umcf->upstreams, cf->pool, 4,
3716 sizeof(ngx_http_upstream_srv_conf_t *))
3719 return NGX_CONF_ERROR;
3727 ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf)
3729 ngx_http_upstream_main_conf_t *umcf = conf;
3732 ngx_array_t headers_in;
3734 ngx_hash_init_t hash;
3735 ngx_http_upstream_init_pt init;
3736 ngx_http_upstream_header_t *header;
3737 ngx_http_upstream_srv_conf_t **uscfp;
3739 uscfp = umcf->upstreams.elts;
3741 for (i = 0; i < umcf->upstreams.nelts; i++) {
3743 init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream:
3744 ngx_http_upstream_init_round_robin;
3746 if (init(cf, uscfp[i]) != NGX_OK) {
3747 return NGX_CONF_ERROR;
3752 /* upstream_headers_in_hash */
3754 if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t))
3757 return NGX_CONF_ERROR;
3760 for (header = ngx_http_upstream_headers_in; header->name.len; header++) {
3761 hk = ngx_array_push(&headers_in);
3763 return NGX_CONF_ERROR;
3766 hk->key = header->name;
3767 hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len);
3771 hash.hash = &umcf->headers_in_hash;
3772 hash.key = ngx_hash_key_lc;
3773 hash.max_size = 512;
3774 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
3775 hash.name = "upstream_headers_in_hash";
3776 hash.pool = cf->pool;
3777 hash.temp_pool = NULL;
3779 if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) {
3780 return NGX_CONF_ERROR;