[nginx] Core: relative redirects (closes #1000).

Ruslan Ermilov ru at nginx.com
Thu Dec 22 07:00:43 UTC 2016


details:   http://hg.nginx.org/nginx/rev/d15172ebb400
branches:  
changeset: 6852:d15172ebb400
user:      Ruslan Ermilov <ru at nginx.com>
date:      Wed Dec 21 23:10:51 2016 +0300
description:
Core: relative redirects (closes #1000).

The current version of HTTP/1.1 standard allows relative references in
redirects (https://tools.ietf.org/html/rfc7231#section-7.1.2).

Allow this form for redirects generated by nginx by introducing the new
directive absolute_redirect.

diffstat:

 src/http/ngx_http_core_module.c          |  10 ++++++++++
 src/http/ngx_http_core_module.h          |   1 +
 src/http/ngx_http_header_filter_module.c |   3 ++-
 src/http/v2/ngx_http_v2_filter_module.c  |   4 +++-
 4 files changed, 16 insertions(+), 2 deletions(-)

diffs (72 lines):

diff -r 8cd97c14b0e2 -r d15172ebb400 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c	Wed Dec 21 22:01:24 2016 +0300
+++ b/src/http/ngx_http_core_module.c	Wed Dec 21 23:10:51 2016 +0300
@@ -542,6 +542,13 @@ static ngx_command_t  ngx_http_core_comm
       offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
       NULL },
 
+    { ngx_string("absolute_redirect"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_core_loc_conf_t, absolute_redirect),
+      NULL },
+
     { ngx_string("server_name_in_redirect"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -3563,6 +3570,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t
     clcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
     clcf->resolver_timeout = NGX_CONF_UNSET_MSEC;
     clcf->reset_timedout_connection = NGX_CONF_UNSET;
+    clcf->absolute_redirect = NGX_CONF_UNSET;
     clcf->server_name_in_redirect = NGX_CONF_UNSET;
     clcf->port_in_redirect = NGX_CONF_UNSET;
     clcf->msie_padding = NGX_CONF_UNSET;
@@ -3825,6 +3833,8 @@ ngx_http_core_merge_loc_conf(ngx_conf_t 
 
     ngx_conf_merge_value(conf->reset_timedout_connection,
                               prev->reset_timedout_connection, 0);
+    ngx_conf_merge_value(conf->absolute_redirect,
+                              prev->absolute_redirect, 1);
     ngx_conf_merge_value(conf->server_name_in_redirect,
                               prev->server_name_in_redirect, 0);
     ngx_conf_merge_value(conf->port_in_redirect, prev->port_in_redirect, 1);
diff -r 8cd97c14b0e2 -r d15172ebb400 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h	Wed Dec 21 22:01:24 2016 +0300
+++ b/src/http/ngx_http_core_module.h	Wed Dec 21 23:10:51 2016 +0300
@@ -385,6 +385,7 @@ struct ngx_http_core_loc_conf_s {
     ngx_flag_t    tcp_nopush;              /* tcp_nopush */
     ngx_flag_t    tcp_nodelay;             /* tcp_nodelay */
     ngx_flag_t    reset_timedout_connection; /* reset_timedout_connection */
+    ngx_flag_t    absolute_redirect;       /* absolute_redirect */
     ngx_flag_t    server_name_in_redirect; /* server_name_in_redirect */
     ngx_flag_t    port_in_redirect;        /* port_in_redirect */
     ngx_flag_t    msie_padding;            /* msie_padding */
diff -r 8cd97c14b0e2 -r d15172ebb400 src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c	Wed Dec 21 22:01:24 2016 +0300
+++ b/src/http/ngx_http_header_filter_module.c	Wed Dec 21 23:10:51 2016 +0300
@@ -309,7 +309,8 @@ ngx_http_header_filter(ngx_http_request_
 
     if (r->headers_out.location
         && r->headers_out.location->value.len
-        && r->headers_out.location->value.data[0] == '/')
+        && r->headers_out.location->value.data[0] == '/'
+        && clcf->absolute_redirect)
     {
         r->headers_out.location->hash = 0;
 
diff -r 8cd97c14b0e2 -r d15172ebb400 src/http/v2/ngx_http_v2_filter_module.c
--- a/src/http/v2/ngx_http_v2_filter_module.c	Wed Dec 21 22:01:24 2016 +0300
+++ b/src/http/v2/ngx_http_v2_filter_module.c	Wed Dec 21 23:10:51 2016 +0300
@@ -263,7 +263,9 @@ ngx_http_v2_header_filter(ngx_http_reque
 
     if (r->headers_out.location && r->headers_out.location->value.len) {
 
-        if (r->headers_out.location->value.data[0] == '/') {
+        if (r->headers_out.location->value.data[0] == '/'
+            && clcf->absolute_redirect)
+        {
             if (clcf->server_name_in_redirect) {
                 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
                 host = cscf->server_name;


More information about the nginx-devel mailing list