Opened 9 years ago

Closed 9 years ago

#34 closed enhancement (fixed)

Allow rewrites in case of proxy_pass backend is configured as variable

Reported by: Owned by: somebody
Priority: minor Milestone:
Component: nginx-module Version: 1.0.x
Keywords: proxy proxy_pass dynamic variable DNS resolver backend rewrite Cc: mdounin@…
uname -a: Linux prx001.local 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 20:13:52 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx: nginx version: nginx/1.0.6
nginx: configure arguments: --with-debug --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/ --user=nginx --group=nginx --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_secure_link_module --with-http_stub_status_module --add-module=../chaoslawful-drizzle-nginx-module-1fc3af0 --add-module=../agentzh-echo-nginx-module-13dd12f --add-module=../ngx-fancyindex-0.3.1 --add-module=../agentzh-headers-more-nginx-module-137855d --add-module=../ngx_http_upstream_keepalive-2ee28064a04a --add-module=../agentzh-memc-nginx-module-4f1952c --add-module=../simpl-ngx_devel_kit-bc97eea --add-module=../chaoslawful-lua-nginx-module-dd0d4d0 --add-module=../agentzh-rds-csv-nginx-module-eb697d1 --add-module=../agentzh-rds-json-nginx-module-376db1f


As a follow up on the discussion on,215830 I would kindly ask to merge the patch provided by Maxim Dounin into Nginx

# HG changeset patch
# User Maxim Dounin <>
# Date 1317281138 -14400
# Node ID d6a0787307868c06073d92bb39eb2f73acf19d7c
# Parent  29d417d419d946dce33023c71cce0e586ef3547b
Proxy: made proxy_pass with variables more consitent.

If proxy_pass were used with variables and there were no URI component,
nginx always used unparsed URI.  This isn't consistent with "no variables"
case, where e.g. rewrites are applied even if there are no URI component.

Fix is to use same logic in both cases, i.e. only use unparsed URI if
it's valid and request is main one.

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
@@ -738,9 +738,6 @@ ngx_http_proxy_eval(ngx_http_request_t *
    = p - 1;
-    } else {
-        url.uri = r->unparsed_uri;

     ctx->vars.key_start = u->schema;
@@ -808,7 +805,7 @@ ngx_http_proxy_create_key(ngx_http_reque
         return NGX_ERROR;

-    if (plcf->proxy_lengths) {
+    if (plcf->proxy_lengths && ctx->vars.uri.len) {

         *key = ctx->vars.uri;
         u->uri = ctx->vars.uri;
@@ -918,7 +915,7 @@ ngx_http_proxy_create_request(ngx_http_r
     loc_len = 0;
     unparsed_uri = 0;

-    if (plcf->proxy_lengths) {
+    if (plcf->proxy_lengths && ctx->vars.uri.len) {
         uri_len = ctx->vars.uri.len;

     } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
@@ -1024,7 +1021,7 @@ ngx_http_proxy_create_request(ngx_http_r

     u-> = b->last;

-    if (plcf->proxy_lengths) {
+    if (plcf->proxy_lengths && ctx->vars.uri.len) {
         b->last = ngx_copy(b->last, ctx->, ctx->vars.uri.len);

     } else if (unparsed_uri) {

This makes Nginx's rewriting behavior consistent with a statically configured proxy_pass backend where rewriting of the URI gets honored. Right now if a backend is configured per variable to allow dynamic DNS lookups (in case the hostname of the backend changes during runtime), any rewriting gets ignored and the unparsed URI gets passed to the backend.

Change History (2)

comment:1 by Maxim Dounin, 9 years ago

Committed as r4378.

comment:2 by Maxim Dounin, 9 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.