Nginx - encoding (normalization) of the URI part

I have a nginx location directive whose purpose is to "remove" the localization prefix from the URI for the proxy_pass directive.

For example, to make the URI http://example.com/en/lalala , use proxy_pass http://example.com/lalala

 location ~ '^/(?<locale>[\w]{2})(/(?<rest>.*))?$' { ... proxy_pass http://example/$rest; ... } 

Thus, the rest variable will be decoded when passed to directeve proxy_pass. This seems to be the expected behavior .

The problem is when my URI contains the encoded space %20 passed from the client

 http://example.com/lala%20lala 

nginx decodes the URI for

 http://example.com/lala lala 

I see this in my error.log.

Question: is it possible to use the encoded variable rest somehow because it is passed from the client? If I am doing something completely wrong, please suggest the right way.

Thanks.

+5
source share
2 answers

Yes, this behavior is expected, although the docs also say:

If proxy_pass is specified without a URI, the request URI is transmitted to the server in the same form as that sent by the client when processing the original request, or the full URI with the normalized request is transmitted when processing the changed URI:

 location /some/path/ { proxy_pass http://127.0.0.1; } 

Nginx engineers say the same thing: https://serverfault.com/questions/459369/disabling-url-decoding-in-nginx-proxy

However, if you add $ request_uri to proxy_pass (and pre-draw the locale, it may work as a Nginx engineer said ):

 set $modified_uri $request_uri; if ($modified_uri ~ "^/([\w]{2})(/.*)") { set $modified_uri $1; } proxy_pass http://example$modified_uri; 
+6
source

I have had some success using the following with Confluence and other Atlassian apps for nginx, where special characters like () <> [] cause problems.

 location /path { # [... other proxy options ...] # set proxy path with regex if ($request_uri ~* "/path(/.*)") { proxy_pass http://server:port/path$1; break; } # fallback (probably not needed) proxy_pass http://server:port/path; } 
+4
source

All Articles