Nginx add location details to URL when we stop decoding URL

Maxim Dounin mdounin at
Fri Feb 13 12:59:39 UTC 2015


On Fri, Feb 13, 2015 at 01:07:15AM -0500, malintha wrote:

> I am accessing a URL which has encode characters
> http:....../malintha/tel%3A%2B6281808147137
>    location /gateway/ {
>     proxy_set_header X-Forwarded-Host $host;
>     proxy_set_header X-Forwarded-Server $host;
>     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
>     proxy_set_header Host $http_host;
>     proxy_read_timeout 5m;
>     proxy_send_timeout 5m;
>     proxy_pass$request_uri/;
>     proxy_set_header Upgrade $http_upgrade;
>     proxy_set_header Connection "upgrade";
>     proxy_http_version 1.1;
> }
> I added $request_uri at then end of the proxy_pass URL as I have to stop
> decoding by nginx.
> When I configure like this nginx resolve it to (stop decoding but incorrect
> URL - adding /gateway/)
> /gateway/malintha/tel%3A%2B6281808147137

What makes you think that nginx is _adding_ "/gateway/"?  As per 
the location specification, the $request_uri is expected to 
contain "/gateway/" in it.

> but When I remove $request_uri it resolve to correct URL (but with
> decoding)

With $request_uri in proxy_pass nginx will assume you've specified 
full URI yourself and it shouldn't be changed.  When you remove 
$request_uri nginx will follow it's normal logic to replace 
matching part of $uri with the URI part specified in the 
proxy_pass directive, and will encode the result as appropriate.  
See for details.

If you want nginx to preserve the encoding as it was in the 
original client request, and want to strip "/gateway/" part from 
the URI at the same time, you may do so by manually removing the 
"/gateway/" from the $request_uri variable, like this (untested):

    set $modified_uri $request_uri;

    if ($modified_uri ~ "^/gateway(/.*)") {
        set $modified_uri $1;

    proxy_pass http://upstream$modified_uri;

Note though, that this will not work in some characters in the 
"/gateway/" are escaped by the client.

Maxim Dounin

More information about the nginx mailing list