Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid nginx decoding query parameters on proxy_pass (equivalent to AllowEncodedSlashes NoDecode)

I use nginx as a load balencer in front of several tomcats. In my incoming requests, I have encoded query parameters. But when the request arrives to tomcat, parameters are decoded :

incoming request to nginx:

curl -i "http://server/1.1/json/T;cID=1234;pID=1200;rF=http%3A%2F%2Fwww.google.com%2F" 

incoming request to tomcat:

curl -i "http://server/1.1/json/T;cID=1234;pID=1200;rF=http:/www.google.com/" 

I don't want my request parameters to be transformed, because in that case my tomcat throws a 405 error.

My nginx configuration is the following :

upstream tracking  {     server front-01.server.com:8080;     server front-02.server.com:8080;     server front-03.server.com:8080;     server front-04.server.com:8080; }  server {     listen 80;     server_name tracking.server.com;     access_log /var/log/nginx/tracking-access.log;     error_log  /var/log/nginx/tracking-error.log;      location / {         proxy_pass  http://tracking/webapp;     } } 

In my current apache load balancer configuration, I have the AllowEncodedSlashes directive that preserves my encoded parameters:

AllowEncodedSlashes NoDecode 

I need to move from apache to nginx.

My question is quite the opposite from this question : Avoid nginx escaping query parameters on proxy_pass

like image 1000
Jean-Philippe Caruana Avatar asked Dec 10 '13 14:12

Jean-Philippe Caruana


2 Answers

I finally found the solution: I need to pass $request_uri parameter :

location / {     proxy_pass  http://tracking/webapp$request_uri; } 

That way, characters that were encoded in the original request will not be decoded, i.e. will be passed as-is to the proxied server.

like image 136
Jean-Philippe Caruana Avatar answered Sep 23 '22 06:09

Jean-Philippe Caruana


Jean's answer is good, but it does not work with sublocations. In that case, the more generic answer is:

location /path/ {   if ($request_uri ~* "/path/(.*)") {     proxy_pass http://tracking/webapp/$1;   } } 
like image 21
user1338062 Avatar answered Sep 22 '22 06:09

user1338062