Skip to content

Instantly share code, notes, and snippets.

@nojima
Last active February 18, 2016 12:36
Show Gist options
  • Save nojima/7640ea74be52d35de2ec to your computer and use it in GitHub Desktop.
Save nojima/7640ea74be52d35de2ec to your computer and use it in GitHub Desktop.
This patch adds "before_send" to proxy_next_upstream directive. When "before_send" is specified, a request is passed to next upstream if an error or a timeout has occurred before sending the first byte of the request.
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index a869e74..78b5305 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -220,6 +220,7 @@ static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
{ ngx_string("http_403"), NGX_HTTP_UPSTREAM_FT_HTTP_403 },
{ ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
{ ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
+ { ngx_string("before_send"), NGX_HTTP_UPSTREAM_FT_BEFORE_SEND },
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
{ ngx_null_string, 0 }
};
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index d5720f4..c45050e 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -3919,11 +3919,15 @@ ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,
}
if (status) {
+ int match_condition = u->conf->next_upstream & ft_type;
+ if (u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_BEFORE_SEND)
+ match_condition |= !u->request_sent;
+
u->state->status = status;
timeout = u->conf->next_upstream_timeout;
if (u->peer.tries == 0
- || !(u->conf->next_upstream & ft_type)
+ || !match_condition
|| (u->request_sent && r->request_body_no_buffering)
|| (timeout && ngx_current_msec - u->peer.start_time >= timeout))
{
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 7ecbba4..564201e 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -29,6 +29,7 @@
#define NGX_HTTP_UPSTREAM_FT_UPDATING 0x00000400
#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK 0x00000800
#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING 0x00001000
+#define NGX_HTTP_UPSTREAM_FT_BEFORE_SEND 0x00002000
#define NGX_HTTP_UPSTREAM_FT_NOLIVE 0x40000000
#define NGX_HTTP_UPSTREAM_FT_OFF 0x80000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment