67 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(
void *data);
68 static ngx_int_t ngx_http_upstream_non_buffered_filter(
void *data,
80 static void ngx_http_upstream_cleanup(
void *data);
140 static ngx_int_t ngx_http_upstream_response_length_variable(
150 static void *ngx_http_upstream_create_main_conf(
ngx_conf_t *cf);
151 static char *ngx_http_upstream_init_main_conf(
ngx_conf_t *cf,
void *conf);
163 ngx_http_upstream_process_header_line,
165 ngx_http_upstream_copy_header_line, 0, 0 },
168 ngx_http_upstream_process_header_line,
170 ngx_http_upstream_copy_content_type, 0, 1 },
173 ngx_http_upstream_process_content_length,
175 ngx_http_upstream_ignore_header_line, 0, 0 },
178 ngx_http_upstream_process_header_line,
180 ngx_http_upstream_copy_header_line,
184 ngx_http_upstream_process_header_line,
186 ngx_http_upstream_copy_last_modified, 0, 0 },
189 ngx_http_upstream_process_header_line,
191 ngx_http_upstream_copy_header_line,
195 ngx_http_upstream_process_header_line,
197 ngx_http_upstream_copy_header_line,
201 ngx_http_upstream_process_header_line,
203 ngx_http_upstream_copy_header_line, 0, 0 },
206 ngx_http_upstream_process_header_line,
208 ngx_http_upstream_rewrite_location, 0, 0 },
211 ngx_http_upstream_ignore_header_line, 0,
212 ngx_http_upstream_rewrite_refresh, 0, 0 },
215 ngx_http_upstream_process_set_cookie, 0,
216 ngx_http_upstream_rewrite_set_cookie, 0, 1 },
219 ngx_http_upstream_ignore_header_line, 0,
220 ngx_http_upstream_copy_header_line, 0, 1 },
223 ngx_http_upstream_process_cache_control, 0,
224 ngx_http_upstream_copy_multi_header_lines,
228 ngx_http_upstream_process_expires, 0,
229 ngx_http_upstream_copy_header_line,
233 ngx_http_upstream_process_header_line,
235 ngx_http_upstream_copy_allow_ranges,
239 ngx_http_upstream_process_connection, 0,
240 ngx_http_upstream_ignore_header_line, 0, 0 },
243 ngx_http_upstream_ignore_header_line, 0,
244 ngx_http_upstream_ignore_header_line, 0, 0 },
247 ngx_http_upstream_ignore_header_line, 0,
248 ngx_http_upstream_copy_header_line, 0, 0 },
251 ngx_http_upstream_process_accel_expires, 0,
252 ngx_http_upstream_copy_header_line, 0, 0 },
255 ngx_http_upstream_process_header_line,
257 ngx_http_upstream_copy_header_line, 0, 0 },
260 ngx_http_upstream_process_limit_rate, 0,
261 ngx_http_upstream_copy_header_line, 0, 0 },
264 ngx_http_upstream_process_buffering, 0,
265 ngx_http_upstream_copy_header_line, 0, 0 },
268 ngx_http_upstream_process_charset, 0,
269 ngx_http_upstream_copy_header_line, 0, 0 },
272 ngx_http_upstream_process_transfer_encoding, 0,
273 ngx_http_upstream_ignore_header_line, 0, 0 },
277 ngx_http_upstream_process_header_line,
279 ngx_http_upstream_copy_content_encoding, 0, 0 },
297 ngx_http_upstream_server,
307 ngx_http_upstream_add_variables,
310 ngx_http_upstream_create_main_conf,
311 ngx_http_upstream_init_main_conf,
323 &ngx_http_upstream_module_ctx,
324 ngx_http_upstream_commands,
340 ngx_http_upstream_addr_variable, 0,
344 ngx_http_upstream_status_variable, 0,
348 ngx_http_upstream_response_time_variable, 0,
351 {
ngx_string(
"upstream_response_length"), NULL,
352 ngx_http_upstream_response_length_variable, 0,
358 ngx_http_upstream_cache_status, 0,
407 ngx_http_upstream_cleanup(r);
444 if (r->spdy_stream) {
445 ngx_http_upstream_init_request(r);
466 ngx_http_upstream_init_request(r);
490 if (u->
conf->cache) {
493 rc = ngx_http_upstream_cache(r, u);
555 if (u->
state == NULL) {
556 ngx_http_upstream_finalize_request(r, u,
570 cln->
handler = ngx_http_upstream_cleanup;
585 ngx_http_upstream_finalize_request(r, u,
590 ngx_http_upstream_connect(r, u);
616 "no port in upstream \"%V\"", host);
617 ngx_http_upstream_finalize_request(r, u,
626 ngx_http_upstream_finalize_request(r, u,
633 "no resolver defined to resolve %V", host);
641 ctx->
handler = ngx_http_upstream_resolve_handler;
649 ngx_http_upstream_finalize_request(r, u,
661 "no upstream configuration");
662 ngx_http_upstream_finalize_request(r, u,
668 ngx_http_upstream_finalize_request(r, u,
673 ngx_http_upstream_connect(r, u);
701 if (u->create_key(r) !=
NGX_OK) {
711 "%V_buffer_size %uz is not enough for cache key, "
712 "it should increased at least to %uz",
750 "http upstream cache: %i", rc);
757 u->cache_status = rc;
774 rc = ngx_http_upstream_cache_send(r, u);
864 if (ngx_http_upstream_process_headers(r, u) !=
NGX_OK) {
901 "%V could not be resolved (%i: %s)",
917 for (i = 0; i < ctx->
naddrs; i++) {
918 addr = ntohl(ur->
addrs[i]);
921 "name was resolved to %ud.%ud.%ud.%ud",
922 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
923 (addr >> 8) & 0xff, addr & 0xff);
929 ngx_http_upstream_finalize_request(r, u,
937 ngx_http_upstream_connect(r, u);
963 "http upstream request: \"%V?%V\"", &r->
uri, &r->
args);
979 ngx_http_upstream_check_broken_connection(r, r->
connection->
read);
1002 "http upstream check client, write event:%d, \"%V\"",
1014 ngx_http_upstream_finalize_request(r, u,
1021 ngx_http_upstream_finalize_request(r, u,
1029 if (r->spdy_stream) {
1034 #if (NGX_HAVE_KQUEUE)
1051 "kevent() reported that client prematurely closed "
1052 "connection, so upstream connection is closed too");
1053 ngx_http_upstream_finalize_request(r, u,
1059 "kevent() reported that client prematurely closed "
1063 ngx_http_upstream_finalize_request(r, u,
1072 n = recv(c->
fd, buf, 1, MSG_PEEK);
1077 "http upstream recv(): %d", n);
1088 ngx_http_upstream_finalize_request(r, u,
1114 "client prematurely closed connection, "
1115 "so upstream connection is closed too");
1116 ngx_http_upstream_finalize_request(r, u,
1122 "client prematurely closed connection");
1125 ngx_http_upstream_finalize_request(r, u,
1147 if (u->
state == NULL) {
1148 ngx_http_upstream_finalize_request(r, u,
1162 "http upstream connect: %i", rc);
1165 ngx_http_upstream_finalize_request(r, u,
1198 if (c->
pool == NULL) {
1203 if (c->
pool == NULL) {
1204 ngx_http_upstream_finalize_request(r, u,
1223 if (ngx_http_upstream_reinit(r, u) !=
NGX_OK) {
1224 ngx_http_upstream_finalize_request(r, u,
1242 ngx_http_upstream_finalize_request(r, u,
1265 if (u->
ssl && c->ssl == NULL) {
1266 ngx_http_upstream_ssl_init_connection(r, u, c);
1272 ngx_http_upstream_send_request(r, u);
1288 ngx_http_upstream_finalize_request(r, u,
1296 if (u->
conf->ssl_session_reuse) {
1298 ngx_http_upstream_finalize_request(r, u,
1309 c->ssl->handler = ngx_http_upstream_ssl_handshake;
1313 ngx_http_upstream_ssl_handshake(c);
1326 if (c->ssl->handshaked) {
1328 if (u->
conf->ssl_session_reuse) {
1335 ngx_http_upstream_send_request(r, u);
1401 #if (NGX_HTTP_CACHE)
1424 "http upstream send request");
1431 c->
log->
action =
"sending request to upstream";
1450 ngx_http_upstream_finalize_request(r, u,
1464 ngx_http_upstream_finalize_request(r, u,
1486 ngx_http_upstream_process_header(r, u);
1494 ngx_http_upstream_finalize_request(r, u,
1510 "http upstream send request handler");
1519 if (u->
ssl && c->ssl == NULL) {
1520 ngx_http_upstream_ssl_init_connection(r, u, c);
1534 ngx_http_upstream_send_request(r, u);
1548 "http upstream process header");
1550 c->
log->
action =
"reading response header from upstream";
1565 ngx_http_upstream_finalize_request(r, u,
1581 ngx_http_upstream_finalize_request(r, u,
1586 #if (NGX_HTTP_CACHE)
1605 ngx_http_upstream_finalize_request(r, u,
1615 "upstream prematurely closed connection");
1626 u->valid_header_in = 0;
1637 "upstream sent too big header");
1639 ngx_http_upstream_next(r, u,
1656 ngx_http_upstream_finalize_request(r, u,
1669 if (ngx_http_upstream_test_next(r, u) ==
NGX_OK) {
1673 if (ngx_http_upstream_intercept_errors(r, u) ==
NGX_OK) {
1678 if (ngx_http_upstream_process_headers(r, u) !=
NGX_OK) {
1683 ngx_http_upstream_send_response(r, u);
1691 u->
input_filter = ngx_http_upstream_non_buffered_filter;
1696 ngx_http_upstream_finalize_request(r, u,
1709 ngx_http_upstream_finalize_request(r, u,
NGX_ERROR);
1714 ngx_http_upstream_finalize_request(r, u, 0);
1721 ngx_http_upstream_process_body_in_memory(r, u);
1733 for (un = ngx_http_upstream_next_errors; un->
status; un++) {
1735 if (status != un->
status) {
1740 ngx_http_upstream_next(r, u, un->
mask);
1744 #if (NGX_HTTP_CACHE)
1747 && (u->
conf->cache_use_stale & un->
mask))
1755 rc = ngx_http_upstream_cache_send(r, u);
1758 ngx_http_upstream_finalize_request(r, u, rc);
1799 if (err_page[i].status == status) {
1807 ngx_http_upstream_finalize_request(r, u,
1817 #if (NGX_HTTP_CACHE)
1825 r->cache->valid_sec =
ngx_time() + valid;
1826 r->cache->error = status;
1832 ngx_http_upstream_finalize_request(r, u, status);
1848 #if (NGX_HAVE_KQUEUE)
1853 err = c->
write->kq_errno;
1856 err = c->
read->kq_errno;
1859 c->
log->
action =
"connecting to upstream";
1861 "kevent() reported that connect() failed");
1876 if (getsockopt(c->
fd, SOL_SOCKET, SO_ERROR, (
void *) &err, &len)
1883 c->
log->
action =
"connecting to upstream";
1908 ngx_http_upstream_finalize_request(r, u,
NGX_DECLINED);
1913 for (i = 0; ; i++) {
1915 if (i >= part->
nelts) {
1916 if (part->
next == NULL) {
1958 for (i = 0; ; i++) {
1960 if (i >= part->
nelts) {
1961 if (part->
next == NULL) {
1981 ngx_http_upstream_finalize_request(r, u,
1989 if (ngx_http_upstream_copy_header_line(r, &h[i], 0) !=
NGX_OK) {
1990 ngx_http_upstream_finalize_request(r, u,
2029 "http upstream process body on memory");
2045 "upstream buffer is too small to read response");
2046 ngx_http_upstream_finalize_request(r, u,
NGX_ERROR);
2057 ngx_http_upstream_finalize_request(r, u, n);
2064 ngx_http_upstream_finalize_request(r, u,
NGX_ERROR);
2074 ngx_http_upstream_finalize_request(r, u, 0);
2079 ngx_http_upstream_finalize_request(r, u,
NGX_ERROR);
2105 ngx_http_upstream_finalize_request(r, u, rc);
2110 ngx_http_upstream_upgrade(r, u);
2130 ngx_http_upstream_finalize_request(r, u, rc);
2148 u->
input_filter = ngx_http_upstream_non_buffered_filter;
2154 ngx_http_upstream_process_non_buffered_downstream;
2159 ngx_http_upstream_finalize_request(r, u, 0);
2168 if (setsockopt(c->
fd, IPPROTO_TCP, TCP_NODELAY,
2169 (
const void *) &tcp_nodelay,
sizeof(
int)) == -1)
2172 "setsockopt(TCP_NODELAY) failed");
2173 ngx_http_upstream_finalize_request(r, u, 0);
2188 ngx_http_upstream_finalize_request(r, u, 0);
2192 ngx_http_upstream_process_non_buffered_downstream(r);
2199 ngx_http_upstream_finalize_request(r, u, 0);
2204 ngx_http_upstream_process_non_buffered_upstream(r, u);
2213 #if (NGX_HTTP_CACHE)
2223 ngx_http_upstream_finalize_request(r, u, 0);
2234 r->cache->min_uses = u->
conf->cache_min_uses;
2236 r->cache->file_cache = u->
conf->cache->data;
2239 ngx_http_upstream_finalize_request(r, u, 0);
2252 valid = r->cache->valid_sec;
2258 r->cache->valid_sec = now + valid;
2264 r->cache->date = now;
2300 ngx_http_upstream_finalize_request(r, u, 0);
2315 "to a temporary file";
2323 ngx_http_upstream_finalize_request(r, u, 0);
2337 ngx_http_upstream_finalize_request(r, u, 0);
2385 ngx_http_upstream_finalize_request(r, u, 0);
2392 ngx_http_upstream_process_upstream(r, u);
2409 c->
log->
action =
"proxying upgraded connection";
2422 if (setsockopt(c->
fd, IPPROTO_TCP, TCP_NODELAY,
2423 (
const void *) &tcp_nodelay,
sizeof(
int)) == -1)
2426 "setsockopt(TCP_NODELAY) failed");
2427 ngx_http_upstream_finalize_request(r, u, 0);
2439 (
const void *) &tcp_nodelay,
sizeof(
int)) == -1)
2442 "setsockopt(TCP_NODELAY) failed");
2443 ngx_http_upstream_finalize_request(r, u, 0);
2452 ngx_http_upstream_finalize_request(r, u, 0);
2459 ngx_http_upstream_process_upgraded(r, 1, 1);
2465 ngx_http_upstream_process_upgraded(r, 0, 1);
2473 ngx_http_upstream_process_upgraded(r, 0, 0);
2480 ngx_http_upstream_process_upgraded(r, 1, 1);
2488 ngx_http_upstream_process_upgraded(r, 1, 0);
2496 ngx_http_upstream_process_upgraded(r, 0, 1);
2515 "http upstream process upgraded, fu:%ui", from_upstream);
2529 ngx_http_upstream_finalize_request(r, u, 0);
2533 if (from_upstream) {
2549 if (b->
start == NULL) {
2551 if (b->
start == NULL) {
2552 ngx_http_upstream_finalize_request(r, u, 0);
2572 n = dst->
send(dst, b->
pos, size);
2575 ngx_http_upstream_finalize_request(r, u, 0);
2620 "http upstream upgraded done");
2621 ngx_http_upstream_finalize_request(r, u, 0);
2630 ngx_http_upstream_finalize_request(r, u, 0);
2642 ngx_http_upstream_finalize_request(r, u, 0);
2656 ngx_http_upstream_finalize_request(r, u, 0);
2661 ngx_http_upstream_finalize_request(r, u, 0);
2686 "http upstream process non buffered downstream");
2697 ngx_http_upstream_process_non_buffered_request(r, 1);
2710 "http upstream process non buffered upstream");
2716 ngx_http_upstream_finalize_request(r, u, 0);
2720 ngx_http_upstream_process_non_buffered_request(r, 0);
2742 do_write = do_write || u->
length == 0;
2752 ngx_http_upstream_finalize_request(r, u, 0);
2766 ngx_http_upstream_finalize_request(r, u, 0);
2779 n = upstream->
recv(upstream, b->
last, size);
2789 ngx_http_upstream_finalize_request(r, u, 0);
2804 if (downstream->
data == r) {
2808 ngx_http_upstream_finalize_request(r, u, 0);
2821 ngx_http_upstream_finalize_request(r, u, 0);
2835 ngx_http_upstream_non_buffered_filter_init(
void *data)
2842 ngx_http_upstream_non_buffered_filter(
void *data, ssize_t bytes)
2897 "http upstream process downstream");
2912 ngx_http_upstream_finalize_request(r, u, 0);
2919 ngx_http_upstream_finalize_request(r, u, 0);
2934 "http downstream delayed");
2937 ngx_http_upstream_finalize_request(r, u, 0);
2944 ngx_http_upstream_finalize_request(r, u, 0);
2949 ngx_http_upstream_process_request(r);
2962 "http upstream process upstream");
2972 ngx_http_upstream_finalize_request(r, u, 0);
2977 ngx_http_upstream_process_request(r);
3003 ngx_http_upstream_store(r, u);
3009 #if (NGX_HTTP_CACHE)
3022 == tf->
offset - (off_t) r->cache->body_start)
3039 "http upstream exit: %p", p->
out);
3043 ngx_http_upstream_finalize_request(r, u, 0);
3050 "http upstream downstream error");
3053 ngx_http_upstream_finalize_request(r, u, 0);
3129 "upstream stores \"%s\" to \"%s\"",
3140 "http upstream dummy handler");
3151 "http next upstream, %xi", ft_type);
3171 "upstream timed out");
3207 ngx_http_upstream_finalize_request(r, u,
3217 #if (NGX_HTTP_CACHE)
3220 && (u->
conf->cache_use_stale & ft_type))
3228 rc = ngx_http_upstream_cache_send(r, u);
3231 ngx_http_upstream_finalize_request(r, u, rc);
3236 ngx_http_upstream_finalize_request(r, u, status);
3243 "close http upstream connection: %d",
3264 if (u->
conf->busy_lock && !u->busy_locked) {
3265 ngx_http_upstream_busy_lock(p);
3270 ngx_http_upstream_connect(r, u);
3275 ngx_http_upstream_cleanup(
void *data)
3280 "cleanup http upstream request: \"%V\"", &r->
uri);
3293 "finalize http upstream request: %i", rc);
3343 "close http upstream connection: %d",
3357 "http upstream temp fd: %d",
3373 #if (NGX_HTTP_CACHE)
3385 r->cache->valid_sec =
ngx_time() + valid;
3386 r->cache->error = rc;
3466 #if (NGX_HTTP_CACHE)
3491 if (pa->
elts == NULL) {
3505 #if (NGX_HTTP_CACHE)
3514 if (r->cache == NULL) {
3518 if (r->cache->valid_sec != 0) {
3541 for (p += 8; p < last; p++) {
3542 if (*p ==
',' || *p ==
';' || *p ==
' ') {
3546 if (*p >=
'0' && *p <=
'9') {
3547 n = n * 10 + *p -
'0';
3560 r->cache->valid_sec =
ngx_time() + n;
3577 #if (NGX_HTTP_CACHE)
3585 if (r->cache == NULL) {
3589 if (r->cache->valid_sec != 0) {
3600 r->cache->valid_sec = expires;
3617 #if (NGX_HTTP_CACHE)
3627 if (r->cache == NULL) {
3646 r->cache->valid_sec =
ngx_time() + n;
3657 r->cache->valid_sec = n;
3709 if (c0 ==
'n' && c1 ==
'o') {
3718 if (c0 ==
'y' && c1 ==
'e' && c2 ==
's') {
3749 (u_char *)
"close", 5 - 1)
3766 (u_char *)
"chunked", 7 - 1)
3807 if (pa->
elts == NULL) {
3849 while (*++p ==
' ') { }
3869 if (*(last - 1) ==
'"') {
3898 #if (NGX_HTTP_CACHE)
3936 "rewritten location: \"%V\"", &ho->
value);
3989 "rewritten refresh: \"%V\"", &ho->
value);
4025 "rewritten cookie: \"%V\"", &ho->
value);
4042 #if (NGX_HTTP_CACHE)
4089 ngx_http_upstream_add_variables(
ngx_conf_t *cf)
4093 for (v = ngx_http_upstream_vars; v->
name.
len; v++) {
4129 if (state[i].peer) {
4147 if (state[i].peer) {
4155 if (state[i].peer) {
4209 if (state[i].status) {
4220 if (state[i].peer) {
4275 if (state[i].status) {
4277 (state[i].response_sec * 1000 + state[i].response_msec);
4279 p =
ngx_sprintf(p,
"%d.%03d", ms / 1000, ms % 1000);
4289 if (state[i].peer) {
4343 p =
ngx_sprintf(p,
"%O", state[i].response_length);
4349 if (state[i].peer) {
4383 sizeof(
"upstream_http_") - 1);
4387 #if (NGX_HTTP_CACHE)
4481 if (mconf == NULL) {
4490 if (mconf == NULL) {
4515 "no servers are inside upstream");
4528 time_t fail_timeout;
4560 "%s in upstream \"%V\"", u.
err, &u.
url);
4572 if (
ngx_strncmp(value[i].data,
"weight=", 7) == 0) {
4578 weight =
ngx_atoi(&value[i].data[7], value[i].len - 7);
4580 if (weight ==
NGX_ERROR || weight == 0) {
4587 if (
ngx_strncmp(value[i].data,
"max_fails=", 10) == 0) {
4593 max_fails =
ngx_atoi(&value[i].data[10], value[i].len - 10);
4602 if (
ngx_strncmp(value[i].data,
"fail_timeout=", 13) == 0) {
4613 if (fail_timeout == (time_t)
NGX_ERROR) {
4620 if (
ngx_strncmp(value[i].data,
"backup", 6) == 0) {
4656 "invalid parameter \"%V\"", &value[i]);
4675 "%s in upstream \"%V\"", u->
err, &u->
url);
4695 if ((flags & NGX_HTTP_UPSTREAM_CREATE)
4696 && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE))
4699 "duplicate upstream \"%V\"", &u->
host);
4703 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && !u->
no_port) {
4705 "upstream \"%V\" may not have port %d",
4710 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && !uscfp[i]->no_port) {
4712 "upstream \"%V\" may not have port %d in %s:%ui",
4718 if (uscfp[i]->port && u->
port
4730 if (flags & NGX_HTTP_UPSTREAM_CREATE) {
4742 uscf->
flags = flags;
4769 if (uscfp == NULL) {
4794 return "is duplicate";
4807 ccv.
value = &value[1];
4815 if (local == NULL) {
4823 if (local->
value == NULL) {
4833 if (local->
addr == NULL) {
4846 "invalid address \"%V\"", &value[1]);
4863 if (local == NULL) {
4867 if (local->
value == NULL) {
4893 "invalid local address \"%V\"", &val);
4922 if (param == NULL) {
4928 param->
key = value[1];
4929 param->
value = value[2];
4933 if (
ngx_strcmp(value[3].data,
"if_not_empty") != 0) {
4935 "invalid parameter \"%V\"", &value[3]);
4966 && ((conf->cache == NULL) == (prev->cache == NULL))
4989 for (h = default_hide_headers; h->
len; h++) {
4997 hk->
value = (
void *) 1;
5006 hk = hide_headers.
elts;
5008 for (j = 0; j < hide_headers.
nelts; j++) {
5021 hk->
value = (
void *) 1;
5032 hk = hide_headers.
elts;
5035 for (j = 0; j < hide_headers.
nelts; j++) {
5037 if (hk[j].key.data == NULL) {
5059 ngx_http_upstream_create_main_conf(
ngx_conf_t *cf)
5080 ngx_http_upstream_init_main_conf(
ngx_conf_t *cf,
void *conf)
5099 if (init(cf, uscfp[i]) !=
NGX_OK) {
5113 for (header = ngx_http_upstream_headers_in; header->
name.
len; header++) {
5128 hash.
name =
"upstream_headers_in_hash";