# HG changeset patch # User Igor Sysoev # Date 1196110800 -10800 # Node ID e10168d6e371c8c2bf687eda4beba01ce6ae3684 # Parent d53199b68e178e7b0805e66b69e8e2cb8a7f1568 nginx 0.6.18 *) Change: now the ngx_http_userid_module adds start time microseconds to the cookie field contains a pid value. *) Change: now the full request line instead of URI only is written to error_log. *) Feature: variables support in the "proxy_pass" directive. *) Feature: the "resolver" and "resolver_timeout" directives. *) Feature: now the directive "add_header last-modified ''" deletes a "Last-Modified" response header line. *) Bugfix: the "limit_rate" directive did not allow to use full throughput, even if limit value was very high. diff --git a/CHANGES b/CHANGES --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,23 @@ +Changes with nginx 0.6.18 27 Nov 2007 + + *) Change: now the ngx_http_userid_module adds start time microseconds + to the cookie field contains a pid value. + + *) Change: now the full request line instead of URI only is written to + error_log. + + *) Feature: variables support in the "proxy_pass" directive. + + *) Feature: the "resolver" and "resolver_timeout" directives. + + *) Feature: now the directive "add_header last-modified ''" deletes a + "Last-Modified" response header line. + + *) Bugfix: the "limit_rate" directive did not allow to use full + throughput, even if limit value was very high. + + Changes with nginx 0.6.17 15 Nov 2007 *) Feature: the "If-Range" request header line support. diff --git a/CHANGES.ru b/CHANGES.ru --- a/CHANGES.ru +++ b/CHANGES.ru @@ -1,4 +1,23 @@ +Изменения в nginx 0.6.18 27.11.2007 + + *) Изменение: теперь модуль ngx_http_userid_module в поле куки с + номером процесса добавляет микросекунды на время старта. + + *) Изменение: в error_log теперь записывается полная строка запроса + вместо только URI. + + *) Добавление: директива proxy_pass поддерживает переменные. + + *) Добавление: директива resolver и resolver_timeout. + + *) Добавление: теперь директива "add_header last-modified ''" удаляет в + заголовке ответа строку "Last-Modified". + + *) Исправление: директива limit_rate не позволяла передавать на полной + скорости, даже если был указан очень большой лимит. + + Изменения в nginx 0.6.17 15.11.2007 *) Добавление: поддержка строки "If-Range" в заголовке запроса. diff --git a/auto/sources b/auto/sources --- a/auto/sources +++ b/auto/sources @@ -15,6 +15,7 @@ CORE_DEPS="src/core/nginx.h \ src/core/ngx_list.h \ src/core/ngx_hash.h \ src/core/ngx_buf.h \ + src/core/ngx_queue.h \ src/core/ngx_string.h \ src/core/ngx_parse.h \ src/core/ngx_inet.h \ @@ -31,6 +32,7 @@ CORE_DEPS="src/core/nginx.h \ src/core/ngx_connection.h \ src/core/ngx_cycle.h \ src/core/ngx_conf_file.h \ + src/core/ngx_resolver.h \ src/core/ngx_open_file_cache.h \ src/core/ngx_garbage_collector.h" @@ -58,6 +60,7 @@ CORE_SRCS="src/core/nginx.c \ src/core/ngx_spinlock.c \ src/core/ngx_cpuinfo.c \ src/core/ngx_conf_file.c \ + src/core/ngx_resolver.c \ src/core/ngx_open_file_cache.c \ src/core/ngx_garbage_collector.c" diff --git a/src/core/nginx.h b/src/core/nginx.h --- a/src/core/nginx.h +++ b/src/core/nginx.h @@ -8,7 +8,7 @@ #define _NGINX_H_INCLUDED_ -#define NGINX_VERSION "0.6.17" +#define NGINX_VERSION "0.6.18" #define NGINX_VER "nginx/" NGINX_VERSION #define NGINX_VAR "NGINX" diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h --- a/src/core/ngx_core.h +++ b/src/core/ngx_core.h @@ -50,6 +50,7 @@ typedef void (*ngx_connection_handler_pt #include #include #include +#include #include #include #include @@ -71,6 +72,7 @@ typedef void (*ngx_connection_handler_pt #endif #include #include +#include #include #include #include diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c --- a/src/core/ngx_inet.c +++ b/src/core/ngx_inet.c @@ -8,6 +8,51 @@ #include +/* AF_INET only */ + +in_addr_t +ngx_inet_addr(u_char *text, size_t len) +{ + u_char *p, c; + in_addr_t addr; + ngx_uint_t octet, n; + + addr = 0; + octet = 0; + n = 0; + + for (p = text; p < text + len; p++) { + + c = *p; + + if (c >= '0' && c <= '9') { + octet = octet * 10 + (c - '0'); + continue; + } + + if (c == '.' && octet < 256) { + addr = (addr << 8) + octet; + octet = 0; + n++; + continue; + } + + return INADDR_NONE; + } + + if (n != 3) { + return INADDR_NONE; + } + + if (octet < 256) { + addr = (addr << 8) + octet; + return htonl(addr); + } + + return INADDR_NONE; +} + + /* * ngx_sock_ntop() and ngx_inet_ntop() may be implemented as * "ngx_sprintf(text, "%ud.%ud.%ud.%ud", p[0], p[1], p[2], p[3])", however, diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h --- a/src/core/ngx_inet.h +++ b/src/core/ngx_inet.h @@ -58,6 +58,7 @@ typedef struct { } ngx_url_t; +in_addr_t ngx_inet_addr(u_char *text, size_t len); size_t ngx_sock_ntop(int family, struct sockaddr *sa, u_char *text, size_t len); size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len); ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr); diff --git a/src/core/ngx_queue.h b/src/core/ngx_queue.h new file mode 100644 --- /dev/null +++ b/src/core/ngx_queue.h @@ -0,0 +1,63 @@ + +/* + * Copyright (C) Igor Sysoev + */ + + +#include +#include + + +#ifndef _NGX_QUEUE_H_INCLUDED_ +#define _NGX_QUEUE_H_INCLUDED_ + + +typedef struct ngx_queue_s ngx_queue_t; + +struct ngx_queue_s { + ngx_queue_t *prev; + ngx_queue_t *next; +}; + + +#define ngx_queue_empty(h) \ + (h == (h)->prev) + + +#define ngx_queue_insert_head(h, x) \ + (x)->next = (h)->next; \ + (x)->next->prev = x; \ + (x)->prev = h; \ + (h)->next = x + + +#define ngx_queue_head(h) \ + (h)->next + + +#define ngx_queue_last(h) \ + (h)->prev + + +#if (NGX_DEBUG) + +#define ngx_queue_remove(x) \ + (x)->next->prev = (x)->prev; \ + (x)->prev->next = (x)->next; \ + (x)->prev = NULL; \ + (x)->next = NULL + +#else + +#define ngx_queue_remove(x) \ + (x)->next->prev = (x)->prev; \ + (x)->prev->next = (x)->next + +#endif + + +#define ngx_queue_data(q, type, link) \ + (type *) ((u_char *) q - offsetof(type, link)) + + +#endif /* _NGX_QUEUE_H_INCLUDED_ */ diff --git a/src/core/ngx_slab.c b/src/core/ngx_slab.c --- a/src/core/ngx_slab.c +++ b/src/core/ngx_slab.c @@ -111,10 +111,7 @@ ngx_slab_init(ngx_slab_pool_t *pool) p += n * sizeof(ngx_slab_page_t); - /* STUB: possible overflow on 64-bit platform */ - pages = (ngx_uint_t) ((uint64_t) size * ngx_pagesize - / (ngx_pagesize + sizeof(ngx_slab_page_t)) - / ngx_pagesize); + pages = (ngx_uint_t) (size / (ngx_pagesize + sizeof(ngx_slab_page_t))); ngx_memzero(p, pages * sizeof(ngx_slab_page_t)); diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h --- a/src/core/ngx_string.h +++ b/src/core/ngx_string.h @@ -114,7 +114,7 @@ ngx_copy(u_char *dst, u_char *src, size_ /* msvc and icc7 compile memcmp() to the inline loop */ -#define ngx_memcmp memcmp +#define ngx_memcmp(s1, s2, n) memcmp((const char *) s1, (const char *) s2, n) u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n); diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c --- a/src/http/modules/ngx_http_fastcgi_module.c +++ b/src/http/modules/ngx_http_fastcgi_module.c @@ -443,6 +443,8 @@ ngx_http_fastcgi_handler(ngx_http_reques return NGX_HTTP_INTERNAL_SERVER_ERROR; } + u->schema = flcf->upstream.schema; + u->peer.log = r->connection->log; u->peer.log_error = NGX_ERROR_ERR; #if (NGX_THREADS) @@ -2171,8 +2173,6 @@ ngx_http_fastcgi_pass(ngx_conf_t *cf, ng clcf->handler = ngx_http_fastcgi_handler; - lcf->upstream.location = clcf->name; - if (clcf->name.data[clcf->name.len - 1] == '/') { clcf->auto_redirect = 1; } diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c --- a/src/http/modules/ngx_http_headers_filter_module.c +++ b/src/http/modules/ngx_http_headers_filter_module.c @@ -369,7 +369,14 @@ ngx_http_set_last_modified(ngx_http_requ old = NULL; } + r->headers_out.last_modified_time = -1; + if (old == NULL || *old == NULL) { + + if (value->len == 0) { + return NGX_OK; + } + h = ngx_list_push(&r->headers_out.headers); if (h == NULL) { return NGX_ERROR; @@ -377,14 +384,17 @@ ngx_http_set_last_modified(ngx_http_requ } else { h = *old; + + if (value->len == 0) { + h->hash = 0; + return NGX_OK; + } } h->hash = hv->value.hash; h->key = hv->value.key; h->value = *value; - r->headers_out.last_modified_time = -1; - return NGX_OK; } diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c --- a/src/http/modules/ngx_http_memcached_module.c +++ b/src/http/modules/ngx_http_memcached_module.c @@ -184,6 +184,8 @@ ngx_http_memcached_handler(ngx_http_requ return NGX_HTTP_INTERNAL_SERVER_ERROR; } + u->schema = mlcf->upstream.schema; + u->peer.log = r->connection->log; u->peer.log_error = NGX_ERROR_ERR; #if (NGX_THREADS) @@ -616,8 +618,6 @@ ngx_http_memcached_pass(ngx_conf_t *cf, clcf->handler = ngx_http_memcached_handler; - lcf->upstream.location = clcf->name; - if (clcf->name.data[clcf->name.len - 1] == '/') { clcf->auto_redirect = 1; } diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c --- a/src/http/modules/ngx_http_proxy_module.c +++ b/src/http/modules/ngx_http_proxy_module.c @@ -33,6 +33,13 @@ struct ngx_http_proxy_redirect_s { typedef struct { + ngx_str_t host_header; + ngx_str_t port; + ngx_str_t uri; +} ngx_http_proxy_vars_t; + + +typedef struct { ngx_http_upstream_conf_t upstream; ngx_array_t *flushes; @@ -45,13 +52,18 @@ typedef struct { ngx_array_t *headers_source; ngx_array_t *headers_names; + ngx_array_t *proxy_lengths; + ngx_array_t *proxy_values; + ngx_array_t *redirects; ngx_str_t body_source; ngx_str_t method; - ngx_str_t host_header; - ngx_str_t port; + ngx_str_t location; + ngx_str_t url; + + ngx_http_proxy_vars_t vars; ngx_flag_t redirect; @@ -66,6 +78,8 @@ typedef struct { u_char *status_start; u_char *status_end; + ngx_http_proxy_vars_t vars; + size_t internal_body_length; } ngx_http_proxy_ctx_t; @@ -73,11 +87,13 @@ typedef struct { #define NGX_HTTP_PROXY_PARSE_NO_HEADER 20 +static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r, + ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf); static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r); static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r); static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r); static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r, - ngx_http_proxy_ctx_t *p); + ngx_http_proxy_ctx_t *ctx); static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r); static void ngx_http_proxy_abort_request(ngx_http_request_t *r); static void ngx_http_proxy_finalize_request(ngx_http_request_t *r, @@ -115,6 +131,13 @@ static char *ngx_http_proxy_upstream_max static char *ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +#if (NGX_HTTP_SSL) +static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf, + ngx_http_proxy_loc_conf_t *plcf); +#endif +static ngx_int_t ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u, + ngx_http_proxy_vars_t *v); + static ngx_conf_post_t ngx_http_proxy_lowat_post = { ngx_http_proxy_lowat_check }; @@ -444,15 +467,38 @@ ngx_http_proxy_handler(ngx_http_request_ { ngx_int_t rc; ngx_http_upstream_t *u; + ngx_http_proxy_ctx_t *ctx; ngx_http_proxy_loc_conf_t *plcf; - plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); - u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); if (u == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } + r->upstream = u; + + ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t)); + if (ctx == NULL) { + return NGX_ERROR; + } + + ngx_http_set_ctx(r, ctx, ngx_http_proxy_module); + + plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); + + if (plcf->proxy_lengths == 0) { + ctx->vars = plcf->vars; + u->schema = plcf->upstream.schema; +#if (NGX_HTTP_SSL) + u->ssl = (plcf->upstream.ssl != NULL); +#endif + + } else { + if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } + } + u->peer.log = r->connection->log; u->peer.log_error = NGX_ERROR_ERR; #if (NGX_THREADS) @@ -484,8 +530,6 @@ ngx_http_proxy_handler(ngx_http_request_ u->accel = 1; - r->upstream = u; - rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { @@ -497,6 +541,81 @@ ngx_http_proxy_handler(ngx_http_request_ static ngx_int_t +ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx, + ngx_http_proxy_loc_conf_t *plcf) +{ + size_t add; + u_short port; + ngx_str_t proxy; + ngx_url_t u; + + if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0, + plcf->proxy_values->elts) + == NULL) + { + return NGX_ERROR; + } + + if (ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0) { + + add = 7; + port = 80; + +#if (NGX_HTTP_SSL) + + } else if (ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0) { + + add = 8; + port = 443; + r->upstream->ssl = 1; + +#endif + + } else { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "invalid URL prefix in \"%V\"", &proxy); + return NGX_ERROR; + } + + r->upstream->schema.len = add; + r->upstream->schema.data = proxy.data; + + ngx_memzero(&u, sizeof(ngx_url_t)); + + u.url.len = proxy.len - add; + u.url.data = proxy.data + add; + u.default_port = port; + u.uri_part = 1; + u.no_resolve = 1; + + if (ngx_parse_url(r->pool, &u) != NGX_OK) { + if (u.err) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "%s in upstream \"%V\"", u.err, &u.url); + } + + return NGX_ERROR; + } + + if (ngx_http_proxy_set_vars(r->pool, &u, &ctx->vars) != NGX_OK) { + return NGX_ERROR; + } + + r->upstream->resolved = ngx_pcalloc(r->pool, + sizeof(ngx_http_upstream_resolved_t)); + if (r->upstream->resolved == NULL) { + return NGX_ERROR; + } + + r->upstream->resolved->host = u.host; + r->upstream->resolved->port = (in_port_t) (u.no_port ? u.default_port: + u.port); + + return NGX_OK; +} + + +static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r) { size_t len, loc_len, body_len; @@ -508,7 +627,7 @@ ngx_http_proxy_create_request(ngx_http_r ngx_list_part_t *part; ngx_table_elt_t *header; ngx_http_upstream_t *u; - ngx_http_proxy_ctx_t *p; + ngx_http_proxy_ctx_t *ctx; ngx_http_script_code_pt code; ngx_http_script_engine_t e, le; ngx_http_proxy_loc_conf_t *plcf; @@ -518,13 +637,6 @@ ngx_http_proxy_create_request(ngx_http_r plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); - p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t)); - if (p == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - ngx_http_set_ctx(r, p, ngx_http_proxy_module); - len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1; if (u->method.len) { @@ -540,19 +652,24 @@ ngx_http_proxy_create_request(ngx_http_r method.len++; } - len += method.len + u->conf->uri.len; - escape = 0; - - loc_len = (r->valid_location && u->conf->uri.len) ? u->conf->location.len: - 0; - - if (u->conf->uri.len == 0 && r->valid_unparsed_uri && r == r->main) { + loc_len = 0; + unparsed_uri = 0; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (plcf->proxy_lengths) { + len += method.len + ctx->vars.uri.len; + + } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main) + { unparsed_uri = 1; - len += r->unparsed_uri.len; + len += method.len + r->unparsed_uri.len; } else { - unparsed_uri = 0; + loc_len = (r->valid_location && ctx->vars.uri.len) ? + plcf->location.len : 0; + if (r->quoted_uri || r->internal) { escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len, r->uri.len - loc_len, NGX_ESCAPE_URI); @@ -574,7 +691,7 @@ ngx_http_proxy_create_request(ngx_http_r body_len += lcode(&le); } - p->internal_body_length = body_len; + ctx->internal_body_length = body_len; len += body_len; } @@ -638,12 +755,15 @@ ngx_http_proxy_create_request(ngx_http_r u->uri.data = b->last; - if (unparsed_uri) { + if (plcf->proxy_lengths) { + b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len); + + } else if (unparsed_uri) { b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len); } else { if (r->valid_location) { - b->last = ngx_copy(b->last, u->conf->uri.data, u->conf->uri.len); + b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len); } if (escape) { @@ -809,18 +929,18 @@ ngx_http_proxy_create_request(ngx_http_r static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r) { - ngx_http_proxy_ctx_t *p; - - p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); - - if (p == NULL) { + ngx_http_proxy_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (ctx == NULL) { return NGX_OK; } - p->status = 0; - p->status_count = 0; - p->status_start = NULL; - p->status_end = NULL; + ctx->status = 0; + ctx->status_count = 0; + ctx->status_start = NULL; + ctx->status_end = NULL; r->upstream->process_header = ngx_http_proxy_process_status_line; @@ -833,15 +953,15 @@ ngx_http_proxy_process_status_line(ngx_h { ngx_int_t rc; ngx_http_upstream_t *u; - ngx_http_proxy_ctx_t *p; - - p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); - - if (p == NULL) { + ngx_http_proxy_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (ctx == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } - rc = ngx_http_proxy_parse_status_line(r, p); + rc = ngx_http_proxy_parse_status_line(r, ctx); if (rc == NGX_AGAIN) { return rc; @@ -866,17 +986,17 @@ ngx_http_proxy_process_status_line(ngx_h return NGX_OK; } - u->headers_in.status_n = p->status; - u->state->status = p->status; - - u->headers_in.status_line.len = p->status_end - p->status_start; + u->headers_in.status_n = ctx->status; + u->state->status = ctx->status; + + u->headers_in.status_line.len = ctx->status_end - ctx->status_start; u->headers_in.status_line.data = ngx_palloc(r->pool, u->headers_in.status_line.len); if (u->headers_in.status_line.data == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } - ngx_memcpy(u->headers_in.status_line.data, p->status_start, + ngx_memcpy(u->headers_in.status_line.data, ctx->status_start, u->headers_in.status_line.len); ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, @@ -890,10 +1010,11 @@ ngx_http_proxy_process_status_line(ngx_h static ngx_int_t -ngx_http_proxy_parse_status_line(ngx_http_request_t *r, ngx_http_proxy_ctx_t *p) +ngx_http_proxy_parse_status_line(ngx_http_request_t *r, + ngx_http_proxy_ctx_t *ctx) { u_char ch; - u_char *pos; + u_char *p; ngx_http_upstream_t *u; enum { sw_start = 0, @@ -915,8 +1036,8 @@ ngx_http_proxy_parse_status_line(ngx_htt state = r->state; - for (pos = u->buffer.pos; pos < u->buffer.last; pos++) { - ch = *pos; + for (p = u->buffer.pos; p < u->buffer.last; p++) { + ch = *p; switch (state) { @@ -1025,11 +1146,11 @@ ngx_http_proxy_parse_status_line(ngx_htt return NGX_HTTP_PROXY_PARSE_NO_HEADER; } - p->status = p->status * 10 + ch - '0'; - - if (++p->status_count == 3) { + ctx->status = ctx->status * 10 + ch - '0'; + + if (++ctx->status_count == 3) { state = sw_space_after_status; - p->status_start = pos - 2; + ctx->status_start = p - 2; } break; @@ -1067,7 +1188,7 @@ ngx_http_proxy_parse_status_line(ngx_htt /* end of status line */ case sw_almost_done: - p->status_end = pos - 1; + ctx->status_end = p - 1; switch (ch) { case LF: goto done; @@ -1077,17 +1198,17 @@ ngx_http_proxy_parse_status_line(ngx_htt } } - u->buffer.pos = pos; + u->buffer.pos = p; r->state = state; return NGX_AGAIN; done: - u->buffer.pos = pos + 1; - - if (p->status_end == NULL) { - p->status_end = pos; + u->buffer.pos = p + 1; + + if (ctx->status_end == NULL) { + ctx->status_end = p; } r->state = sw_start; @@ -1244,15 +1365,20 @@ static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_http_proxy_loc_conf_t *plcf; - - plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); - - v->len = plcf->host_header.len; + ngx_http_proxy_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (ctx == NULL) { + v->not_found = 1; + return NGX_OK; + } + + v->len = ctx->vars.host_header.len; v->valid = 1; v->no_cacheable = 0; v->not_found = 0; - v->data = plcf->host_header.data; + v->data = ctx->vars.host_header.data; return NGX_OK; } @@ -1262,15 +1388,20 @@ static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_http_proxy_loc_conf_t *plcf; - - plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module); - - v->len = plcf->port.len; + ngx_http_proxy_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (ctx == NULL) { + v->not_found = 1; + return NGX_OK; + } + + v->len = ctx->vars.port.len; v->valid = 1; v->no_cacheable = 0; v->not_found = 0; - v->data = plcf->port.data; + v->data = ctx->vars.port.data; return NGX_OK; } @@ -1317,11 +1448,11 @@ static ngx_int_t ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { - ngx_http_proxy_ctx_t *p; - - p = ngx_http_get_module_ctx(r, ngx_http_proxy_module); - - if (p == NULL) { + ngx_http_proxy_ctx_t *ctx; + + ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module); + + if (ctx == NULL) { v->not_found = 1; return NGX_OK; } @@ -1336,7 +1467,7 @@ ngx_http_proxy_internal_body_length_vari return NGX_ERROR; } - v->len = ngx_sprintf(v->data, "%uz", p->internal_body_length) - v->data; + v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data; return NGX_OK; } @@ -1740,7 +1871,7 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t conf->redirects = prev->redirects; } - if (conf->redirects == NULL && conf->upstream.url.data) { + if (conf->redirects == NULL && conf->url.data) { conf->redirects = ngx_array_create(cf->pool, 1, sizeof(ngx_http_proxy_redirect_t)); @@ -1754,10 +1885,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t } pr->handler = ngx_http_proxy_rewrite_redirect_text; - pr->redirect = conf->upstream.url; - - if (conf->upstream.uri.len) { - pr->replacement.text = conf->upstream.location; + pr->redirect = conf->url; + + if (conf->vars.uri.len) { + pr->replacement.text = conf->location; } else { pr->replacement.text.len = 0; @@ -1766,6 +1897,12 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t } } + /* STUB */ + if (prev->proxy_lengths) { + conf->proxy_lengths = prev->proxy_lengths; + conf->proxy_values = prev->proxy_values; + } + ngx_conf_merge_uint_value(conf->headers_hash_max_size, prev->headers_hash_max_size, 512); @@ -1879,8 +2016,7 @@ peers: if (conf->upstream.upstream == NULL) { conf->upstream.upstream = prev->upstream.upstream; - conf->host_header = prev->host_header; - conf->port = prev->port; + conf->vars = prev->vars; conf->upstream.schema = prev->upstream.schema; } @@ -2156,24 +2292,53 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ { ngx_http_proxy_loc_conf_t *plcf = conf; - u_char *p; - size_t add; - u_short port; - ngx_str_t *value, *url; - ngx_url_t u; - ngx_http_core_loc_conf_t *clcf; -#if (NGX_HTTP_SSL) - ngx_pool_cleanup_t *cln; -#endif + size_t add; + u_short port; + ngx_str_t *value, *url; + ngx_url_t u; + ngx_uint_t n; + ngx_http_core_loc_conf_t *clcf; + ngx_http_script_compile_t sc; if (plcf->upstream.schema.len) { return "is duplicate"; } + clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); + value = cf->args->elts; url = &value[1]; + n = ngx_http_script_variables_count(url); + + if (n) { + + ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); + + sc.cf = cf; + sc.source = url; + sc.lengths = &plcf->proxy_lengths; + sc.values = &plcf->proxy_values; + sc.variables = n; + sc.complete_lengths = 1; + sc.complete_values = 1; + + if (ngx_http_script_compile(&sc) != NGX_OK) { + return NGX_CONF_ERROR; + } + +#if (NGX_HTTP_SSL) + if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) { + return NGX_CONF_ERROR; + } +#endif + + clcf->handler = ngx_http_proxy_handler; + + return NGX_CONF_OK; + } + if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) { add = 7; port = 80; @@ -2181,32 +2346,12 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) { #if (NGX_HTTP_SSL) + if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) { + return NGX_CONF_ERROR; + } add = 8; port = 443; - - plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); - if (plcf->upstream.ssl == NULL) { - return NGX_CONF_ERROR; - } - - plcf->upstream.ssl->log = cf->log; - - if (ngx_ssl_create(plcf->upstream.ssl, - NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL) - != NGX_OK) - { - return NGX_CONF_ERROR; - } - - cln = ngx_pool_cleanup_add(cf->pool, 0); - if (cln == NULL) { - return NGX_CONF_ERROR; - } - - cln->handler = ngx_ssl_cleanup_ctx; - cln->data = plcf->upstream.ssl; - #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "https protocol requires SSL support"); @@ -2231,59 +2376,23 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ return NGX_CONF_ERROR; } - if (!u.unix_socket) { - if (u.no_port || u.port == port) { - plcf->host_header = u.host; - - if (port == 80) { - plcf->port.len = sizeof("80") - 1; - plcf->port.data = (u_char *) "80"; - - } else { - plcf->port.len = sizeof("443") - 1; - plcf->port.data = (u_char *) "443"; - } - - } else { - p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1); - if (p == NULL) { - return NGX_CONF_ERROR; - } - - plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port) - - p; - plcf->host_header.data = p; - - plcf->port.len = plcf->host_header.len - u.host.len - 1; - plcf->port.data = p + u.host.len + 1; - } - - - } else { - plcf->host_header.len = sizeof("localhost") - 1; - plcf->host_header.data = (u_char *) "localhost"; - plcf->port.len = 0; - plcf->port.data = (u_char *) ""; + if (ngx_http_proxy_set_vars(cf->pool, &u, &plcf->vars) != NGX_OK) { + return NGX_CONF_ERROR; } - plcf->upstream.uri = u.uri; - plcf->upstream.schema.len = add; plcf->upstream.schema.data = url->data; - - clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); + plcf->location = clcf->name; clcf->handler = ngx_http_proxy_handler; - plcf->upstream.location = clcf->name; - if (clcf->named #if (NGX_PCRE) || clcf->regex #endif || clcf->noname) { - if (plcf->upstream.uri.len) { + if (plcf->vars.uri.len) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"proxy_pass\" may not have URI part in " "location given by regular expression, " @@ -2293,10 +2402,10 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_ return NGX_CONF_ERROR; } - plcf->upstream.location.len = 0; + plcf->location.len = 0; } - plcf->upstream.url = *url; + plcf->url = *url; if (clcf->name.data[clcf->name.len - 1] == '/') { clcf->auto_redirect = 1; @@ -2342,7 +2451,7 @@ ngx_http_proxy_redirect(ngx_conf_t *cf, } if (cf->args->nelts == 2 && ngx_strcmp(value[1].data, "default") == 0) { - if (plcf->upstream.url.data == NULL) { + if (plcf->url.data == NULL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"proxy_rewrite_location default\" must go " "after the \"proxy_pass\" directive"); @@ -2350,10 +2459,10 @@ ngx_http_proxy_redirect(ngx_conf_t *cf, } pr->handler = ngx_http_proxy_rewrite_redirect_text; - pr->redirect = plcf->upstream.url; - - if (plcf->upstream.uri.len) { - pr->replacement.text = plcf->upstream.location; + pr->redirect = plcf->url; + + if (plcf->vars.uri.len) { + pr->replacement.text = plcf->location; } else { pr->replacement.text.len = 0; @@ -2495,3 +2604,83 @@ ngx_http_proxy_upstream_fail_timeout_uns return NGX_CONF_ERROR; } + + +#if (NGX_HTTP_SSL) + +static ngx_int_t +ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf) +{ + ngx_pool_cleanup_t *cln; + + plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); + if (plcf->upstream.ssl == NULL) { + return NGX_ERROR; + } + + plcf->upstream.ssl->log = cf->log; + + if (ngx_ssl_create(plcf->upstream.ssl, + NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL) + != NGX_OK) + { + return NGX_ERROR; + } + + cln = ngx_pool_cleanup_add(cf->pool, 0); + if (cln == NULL) { + return NGX_ERROR; + } + + cln->handler = ngx_ssl_cleanup_ctx; + cln->data = plcf->upstream.ssl; + + return NGX_OK; +} + +#endif + + +static ngx_int_t +ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u, + ngx_http_proxy_vars_t *v) +{ + u_char *p; + + if (!u->unix_socket) { + if (u->no_port || u->port == u->default_port) { + v->host_header = u->host; + + if (u->default_port == 80) { + v->port.len = sizeof("80") - 1; + v->port.data = (u_char *) "80"; + + } else { + v->port.len = sizeof("443") - 1; + v->port.data = (u_char *) "443"; + } + + } else { + p = ngx_palloc(pool, u->host.len + sizeof(":65536") - 1); + if (p == NULL) { + return NGX_ERROR; + } + + v->host_header.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p; + v->host_header.data = p; + + v->port.len = v->host_header.len - u->host.len - 1; + v->port.data = p + u->host.len + 1; + } + + } else { + v->host_header.len = sizeof("localhost") - 1; + v->host_header.data = (u_char *) "localhost"; + v->port.len = 0; + v->port.data = (u_char *) ""; + } + + v->uri = u->uri; + + return NGX_OK; +} diff --git a/src/http/modules/ngx_http_userid_filter_module.c b/src/http/modules/ngx_http_userid_filter_module.c --- a/src/http/modules/ngx_http_userid_filter_module.c +++ b/src/http/modules/ngx_http_userid_filter_module.c @@ -61,9 +61,11 @@ static char *ngx_http_userid_expires(ngx static char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data); static char *ngx_http_userid_mark(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static ngx_int_t ngx_http_userid_init_worker(ngx_cycle_t *cycle); +static uint32_t start_value; static uint32_t sequencer_v1 = 1; static uint32_t sequencer_v2 = 0x03030302; @@ -173,7 +175,7 @@ ngx_module_t ngx_http_userid_filter_mod NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ - NULL, /* init process */ + ngx_http_userid_init_worker, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ @@ -319,7 +321,7 @@ ngx_http_userid_set_uid(ngx_http_request ctx->uid_set[0] = conf->service; } ctx->uid_set[1] = (uint32_t) ngx_time(); - ctx->uid_set[2] = ngx_pid; + ctx->uid_set[2] = start_value; ctx->uid_set[3] = sequencer_v1; sequencer_v1 += 0x100; @@ -346,7 +348,7 @@ ngx_http_userid_set_uid(ngx_http_request } ctx->uid_set[1] = htonl((uint32_t) ngx_time()); - ctx->uid_set[2] = htonl(ngx_pid); + ctx->uid_set[2] = htonl(start_value); ctx->uid_set[3] = htonl(sequencer_v2); sequencer_v2 += 0x100; if (sequencer_v2 < 0x03030302) { @@ -706,3 +708,18 @@ ngx_http_userid_mark(ngx_conf_t *cf, ngx return NGX_CONF_OK; } + + +static ngx_int_t +ngx_http_userid_init_worker(ngx_cycle_t *cycle) +{ + struct timeval tp; + + ngx_gettimeofday(&tp); + + /* use the most significant usec part that fits to 16 bits */ + start_value = ((tp.tv_usec / 20) << 16) | ngx_pid; + + return NGX_OK; +} + diff --git a/src/http/modules/perl/nginx.pm b/src/http/modules/perl/nginx.pm --- a/src/http/modules/perl/nginx.pm +++ b/src/http/modules/perl/nginx.pm @@ -47,7 +47,7 @@ our @EXPORT = qw( HTTP_INSUFFICIENT_STORAGE ); -our $VERSION = '0.6.17'; +our $VERSION = '0.6.18'; require XSLoader; XSLoader::load('nginx', $VERSION); diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -69,6 +69,8 @@ static char *ngx_http_core_keepalive(ngx void *conf); static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char * ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data); static char *ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data); @@ -492,6 +494,20 @@ static ngx_command_t ngx_http_core_comm offsetof(ngx_http_core_loc_conf_t, open_file_cache_events), NULL }, + { ngx_string("resolver"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_http_core_resolver, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + NULL }, + + { ngx_string("resolver_timeout"), + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_msec_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_core_loc_conf_t, resolver_timeout), + NULL }, + ngx_null_command }; @@ -2391,6 +2407,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t lcf->keepalive_header = NGX_CONF_UNSET; lcf->lingering_time = NGX_CONF_UNSET_MSEC; lcf->lingering_timeout = NGX_CONF_UNSET_MSEC; + lcf->resolver_timeout = NGX_CONF_UNSET_MSEC; lcf->reset_timedout_connection = NGX_CONF_UNSET; lcf->port_in_redirect = NGX_CONF_UNSET; lcf->msie_padding = NGX_CONF_UNSET; @@ -2572,6 +2589,12 @@ ngx_http_core_merge_loc_conf(ngx_conf_t prev->lingering_time, 30000); ngx_conf_merge_msec_value(conf->lingering_timeout, prev->lingering_timeout, 5000); + ngx_conf_merge_msec_value(conf->resolver_timeout, + prev->resolver_timeout, 30000); + + if (conf->resolver == NULL) { + conf->resolver = prev->resolver; + } ngx_conf_merge_path_value(conf->client_body_temp_path, prev->client_body_temp_path, @@ -3361,6 +3384,33 @@ ngx_http_core_internal(ngx_conf_t *cf, n static char * +ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_core_loc_conf_t *clcf = conf; + + ngx_url_t u; + ngx_str_t *value; + + value = cf->args->elts; + + u.host = value[1]; + u.port = 53; + + if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err); + return NGX_CONF_ERROR; + } + + clcf->resolver = ngx_resolver_create(&u.addrs[0], cf->cycle->new_log); + if (clcf->resolver == NULL) { + return NGX_OK; + } + + return NGX_CONF_OK; +} + + +static char * ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data) { #if (NGX_FREEBSD) diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h --- a/src/http/ngx_http_core_module.h +++ b/src/http/ngx_http_core_module.h @@ -270,6 +270,9 @@ struct ngx_http_core_loc_conf_s { ngx_msec_t keepalive_timeout; /* keepalive_timeout */ ngx_msec_t lingering_time; /* lingering_time */ ngx_msec_t lingering_timeout; /* lingering_timeout */ + ngx_msec_t resolver_timeout; /* resolver_timeout */ + + ngx_resolver_t *resolver; /* resolver */ time_t keepalive_header; /* keepalive_timeout */ diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -2613,28 +2613,21 @@ ngx_http_log_error_handler(ngx_http_requ buf = p; } - if (r->unparsed_uri.data) { - p = ngx_snprintf(buf, len, ", URL: \"%V\"", &r->unparsed_uri); + if (r->request_line.data == NULL && r->request_start) { + for (p = r->request_start; p < r->header_in->last; p++) { + if (*p == CR || *p == LF) { + break; + } + } + + r->request_line.len = p - r->request_start; + r->request_line.data = r->request_start; + } + + if (r->request_line.len) { + p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line); len -= p - buf; buf = p; - - } else { - if (r->request_line.data == NULL && r->request_start) { - for (p = r->request_start; p < r->header_in->last; p++) { - if (*p == CR || *p == LF) { - break; - } - } - - r->request_line.len = p - r->request_start; - r->request_line.data = r->request_start; - } - - if (r->request_line.len) { - p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line); - len -= p - buf; - buf = p; - } } if (r != sr) { @@ -2656,7 +2649,7 @@ ngx_http_log_error_handler(ngx_http_requ #endif p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"", - &u->conf->schema, u->peer.name, + &u->schema, u->peer.name, uri_separator, &u->uri); len -= p - buf; buf = p; diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h --- a/src/http/ngx_http_request.h +++ b/src/http/ngx_http_request.h @@ -230,6 +230,7 @@ typedef struct { ngx_table_elt_t *content_length; ngx_table_elt_t *content_encoding; ngx_table_elt_t *location; + ngx_table_elt_t *refresh; ngx_table_elt_t *last_modified; ngx_table_elt_t *content_range; ngx_table_elt_t *accept_ranges; diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c --- a/src/http/ngx_http_upstream.c +++ b/src/http/ngx_http_upstream.c @@ -9,6 +9,7 @@ #include +static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx); static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r); static void ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r); static void ngx_http_upstream_check_broken_connection(ngx_http_request_t *r, @@ -283,10 +284,15 @@ static ngx_http_variable_t ngx_http_ups void ngx_http_upstream_init(ngx_http_request_t *r) { - ngx_connection_t *c; - ngx_http_cleanup_t *cln; - ngx_http_upstream_t *u; - ngx_http_core_loc_conf_t *clcf; + ngx_str_t *host; + ngx_uint_t i; + ngx_connection_t *c; + ngx_resolver_ctx_t *ctx, temp; + ngx_http_cleanup_t *cln; + ngx_http_upstream_t *u; + ngx_http_core_loc_conf_t *clcf; + ngx_http_upstream_srv_conf_t *uscf, **uscfp; + ngx_http_upstream_main_conf_t *umcf; c = r->connection; @@ -320,11 +326,6 @@ ngx_http_upstream_init(ngx_http_request_ u->request_bufs = r->request_body->bufs; } - if (u->conf->upstream->peer.init(r, u->conf->upstream) != NGX_OK) { - ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); - return; - } - if (u->create_request(r) != NGX_OK) { ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; @@ -332,7 +333,7 @@ ngx_http_upstream_init(ngx_http_request_ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); - u->output.sendfile = r->connection->sendfile; + u->output.sendfile = c->sendfile; u->output.pool = r->pool; u->output.bufs.num = 1; u->output.bufs.size = clcf->client_body_buffer_size; @@ -374,11 +375,125 @@ ngx_http_upstream_init(ngx_http_request_ u->store = (u->conf->store || u->conf->store_lengths); + if (u->resolved == NULL) { + + uscf = u->conf->upstream; + + } else { + + host = &r->upstream->resolved->host; + + umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); + + uscfp = umcf->upstreams.elts; + + for (i = 0; i < umcf->upstreams.nelts; i++) { + + uscf = uscfp[i]; + + if (uscf->host.len == host->len + && ngx_memcmp(uscf->host.data, host->data, host->len) == 0) + { + goto found; + } + } + + if (clcf->resolver == NULL) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "no resolver defined to resolve %V", host); + ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); + return; + } + + temp.name = *host; + + ctx = ngx_resolve_start(clcf->resolver, &temp); + if (ctx == NULL) { + ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); + return; + } + + ctx->name = *host; + ctx->type = NGX_RESOLVE_A; + ctx->handler = ngx_http_upstream_resolve_handler; + ctx->data = r; + ctx->timeout = clcf->resolver_timeout; + + u->resolved->ctx = ctx; + + if (ngx_resolve_name(ctx) != NGX_OK) { + ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); + return; + } + + return; + } + +found: + + if (uscf->peer.init(r, uscf) != NGX_OK) { + ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); + return; + } + ngx_http_upstream_connect(r, u); } static void +ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx) +{ + ngx_http_request_t *r; + ngx_http_upstream_resolved_t *ur; + + r = ctx->data; + + r->upstream->resolved->ctx = NULL; + + if (ctx->state) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "%V could not be resolved (%i: %s)", + &ctx->name, ctx->state, + ngx_resolver_strerror(ctx->state)); + + ngx_resolve_name_done(ctx); + ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); + return; + } + + ur = r->upstream->resolved; + ur->naddrs = ctx->naddrs; + ur->addrs = ctx->addrs; + +#if (NGX_DEBUG) + { + in_addr_t addr; + ngx_uint_t i; + + for (i = 0; i < ctx->naddrs; i++) { + addr = ntohl(ur->addrs[i]); + + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "name was resolved to %ud.%ud.%ud.%ud", + (addr >> 24) & 0xff, (addr >> 16) & 0xff, + (addr >> 8) & 0xff, addr & 0xff); + } + } +#endif + + if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) { + ngx_resolve_name_done(ctx); + ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); + return; + } + + ngx_resolve_name_done(ctx); + + ngx_http_upstream_connect(r, r->upstream); +} + + +static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r) { ngx_http_upstream_check_broken_connection(r, r->connection->read); @@ -549,6 +664,7 @@ ngx_http_upstream_connect(ngx_http_reque tp = ngx_timeofday(); u->state->response_sec = tp->sec; u->state->response_msec = tp->msec; + u->state->peer = u->peer.name; rc = ngx_event_connect_peer(&u->peer); @@ -561,8 +677,6 @@ ngx_http_upstream_connect(ngx_http_reque return; } - u->state->peer = u->peer.name; - if (rc == NGX_BUSY) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams"); ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE); @@ -638,7 +752,7 @@ ngx_http_upstream_connect(ngx_http_reque #if (NGX_HTTP_SSL) - if (u->conf->ssl && c->ssl == NULL) { + if (u->ssl && c->ssl == NULL) { ngx_http_upstream_ssl_init_connection(r, u, c); return; } @@ -893,7 +1007,7 @@ ngx_http_upstream_send_request_handler(n #if (NGX_HTTP_SSL) - if (u->conf->ssl && c->ssl == NULL) { + if (u->ssl && c->ssl == NULL) { ngx_http_upstream_ssl_init_connection(r, u, c); return; } @@ -2260,6 +2374,10 @@ ngx_http_upstream_cleanup(void *data) ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "cleanup http upstream request: \"%V\"", &r->uri); + if (r->upstream->resolved && r->upstream->resolved->ctx) { + ngx_resolve_name_done(r->upstream->resolved->ctx); + } + ngx_http_upstream_finalize_request(r, r->upstream, NGX_DONE); } @@ -2275,7 +2393,7 @@ ngx_http_upstream_finalize_request(ngx_h *u->cleanup = NULL; - if (u->state->response_sec) { + if (u->state && u->state->response_sec) { tp = ngx_timeofday(); u->state->response_sec = tp->sec - u->state->response_sec; u->state->response_msec = tp->msec - u->state->response_msec; @@ -2283,7 +2401,9 @@ ngx_http_upstream_finalize_request(ngx_h u->finalize_request(r, rc); - u->peer.free(&u->peer, u->peer.data, 0); + if (u->peer.free) { + u->peer.free(&u->peer, u->peer.data, 0); + } if (u->peer.connection) { @@ -2609,6 +2729,10 @@ ngx_http_upstream_rewrite_location(ngx_h return rc; } + if (ho->value.data[0] != '/') { + r->headers_out.location = ho; + } + /* * we do not set r->headers_out.location here to avoid the handling * the local redirects without a host name by ngx_http_header_filter() @@ -2648,16 +2772,18 @@ ngx_http_upstream_rewrite_refresh(ngx_ht return NGX_OK; } -#if (NGX_DEBUG) if (rc == NGX_OK) { + r->headers_out.refresh = ho; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "rewritten refresh: \"%V\"", &ho->value); } -#endif return rc; } + r->headers_out.refresh = ho; + return NGX_OK; } diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h --- a/src/http/ngx_http_upstream.h +++ b/src/http/ngx_http_upstream.h @@ -138,9 +138,6 @@ typedef struct { ngx_array_t *pass_headers; ngx_str_t schema; - ngx_str_t uri; - ngx_str_t location; - ngx_str_t url; /* used in proxy_rewrite_location */ ngx_array_t *store_lengths; ngx_array_t *store_values; @@ -199,6 +196,15 @@ typedef struct { } ngx_http_upstream_headers_in_t; +typedef struct { + ngx_str_t host; + in_port_t port; + ngx_uint_t naddrs; + in_addr_t *addrs; + ngx_resolver_ctx_t *ctx; +} ngx_http_upstream_resolved_t; + + struct ngx_http_upstream_s { ngx_peer_connection_t peer; @@ -213,6 +219,8 @@ struct ngx_http_upstream_s { ngx_http_upstream_headers_in_t headers_in; + ngx_http_upstream_resolved_t *resolved; + ngx_buf_t buffer; size_t length; @@ -235,10 +243,10 @@ struct ngx_http_upstream_s { ngx_msec_t timeout; - ngx_str_t method; - ngx_http_upstream_state_t *state; + ngx_str_t method; + ngx_str_t schema; ngx_str_t uri; ngx_http_cleanup_pt *cleanup; @@ -246,6 +254,7 @@ struct ngx_http_upstream_s { unsigned store:1; unsigned cacheable:1; unsigned accel:1; + unsigned ssl:1; unsigned buffering:1; diff --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c --- a/src/http/ngx_http_upstream_round_robin.c +++ b/src/http/ngx_http_upstream_round_robin.c @@ -247,6 +247,100 @@ ngx_http_upstream_init_round_robin_peer( ngx_int_t +ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, + ngx_http_upstream_resolved_t *ur) +{ + u_char *p; + size_t len; + ngx_uint_t i, n; + struct sockaddr_in *sin; + ngx_http_upstream_rr_peers_t *peers; + ngx_http_upstream_rr_peer_data_t *rrp; + + rrp = r->upstream->peer.data; + + if (rrp == NULL) { + rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); + if (rrp == NULL) { + return NGX_ERROR; + } + + r->upstream->peer.data = rrp; + } + + peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t) + + sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1)); + if (peers == NULL) { + return NGX_ERROR; + } + + peers->single = (ur->naddrs == 1); + peers->number = ur->naddrs; + peers->name = &ur->host; + + for (i = 0; i < ur->naddrs; i++) { + + len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1; + + p = ngx_palloc(r->pool, len); + if (p == NULL) { + return NGX_ERROR; + } + + len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, INET_ADDRSTRLEN); + len = ngx_sprintf(&p[len], ":%d", ur->port) - p; + + sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in)); + if (sin == NULL) { + return NGX_ERROR; + } + + sin->sin_family = AF_INET; + sin->sin_port = htons(ur->port); + sin->sin_addr.s_addr = ur->addrs[i]; + + peers->peer[i].sockaddr = (struct sockaddr *) sin; + peers->peer[i].socklen = sizeof(struct sockaddr_in); + peers->peer[i].name.len = len; + peers->peer[i].name.data = p; + peers->peer[i].weight = 1; + peers->peer[i].current_weight = 1; + peers->peer[i].max_fails = 1; + peers->peer[i].fail_timeout = 10; + } + + rrp->peers = peers; + rrp->current = 0; + + if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { + rrp->tried = &rrp->data; + rrp->data = 0; + + } else { + n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) + / (8 * sizeof(uintptr_t)); + + rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); + if (rrp->tried == NULL) { + return NGX_ERROR; + } + } + + r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; + r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; + r->upstream->peer.tries = rrp->peers->number; +#if (NGX_HTTP_SSL) + r->upstream->peer.set_session = + ngx_http_upstream_set_round_robin_peer_session; + r->upstream->peer.save_session = + ngx_http_upstream_save_round_robin_peer_session; +#endif + + return NGX_OK; +} + + +ngx_int_t ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) { ngx_http_upstream_rr_peer_data_t *rrp = data; diff --git a/src/http/ngx_http_upstream_round_robin.h b/src/http/ngx_http_upstream_round_robin.h --- a/src/http/ngx_http_upstream_round_robin.h +++ b/src/http/ngx_http_upstream_round_robin.h @@ -65,6 +65,8 @@ ngx_int_t ngx_http_upstream_init_round_r ngx_http_upstream_srv_conf_t *us); ngx_int_t ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us); +ngx_int_t ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, + ngx_http_upstream_resolved_t *ur); ngx_int_t ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data); void ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, diff --git a/src/http/ngx_http_write_filter_module.c b/src/http/ngx_http_write_filter_module.c --- a/src/http/ngx_http_write_filter_module.c +++ b/src/http/ngx_http_write_filter_module.c @@ -49,6 +49,7 @@ ngx_http_write_filter(ngx_http_request_t { off_t size, sent, limit; ngx_uint_t last, flush; + ngx_msec_t delay; ngx_chain_t *cl, *ln, **ll, *chain; ngx_connection_t *c; ngx_http_core_loc_conf_t *clcf; @@ -245,14 +246,17 @@ ngx_http_write_filter(ngx_http_request_t } if (r->limit_rate) { - sent = c->sent - sent; - c->write->delayed = 1; - ngx_add_timer(c->write, (ngx_msec_t) (sent * 1000 / r->limit_rate + 1)); + delay = (ngx_msec_t) ((c->sent - sent) * 1000 / r->limit_rate + 1); + + if (delay > 0) { + c->write->delayed = 1; + ngx_add_timer(c->write, delay); + } } else if (c->write->ready && clcf->sendfile_max_chunk && (size_t) (c->sent - sent) - >= clcf->sendfile_max_chunk - 2 * ngx_pagesize) + >= clcf->sendfile_max_chunk - 2 * ngx_pagesize) { c->write->delayed = 1; ngx_add_timer(c->write, 1);