TomPAP
TomPAP

Reputation: 259

how to avoid nginx to replace %20 by whitespace when using as a proxy (proxy_pass) ?

I am using a nginx as a proxy for an apache server.

Here is my config:

location ~ ^/subsite/(.*)$ {
        proxy_pass http://127.0.0.1/subsite/$1?$query_string;
    }

the problem is that if I send a request with %20 like mywebsite.com/subsite/variable/value/title/Access%20denied/another/example

the %20 is replaced by a whitespace, and apache don't care about all the end of the request after Access /title/Access

Any Idea ?

Upvotes: 12

Views: 8557

Answers (1)

steamedcotton
steamedcotton

Reputation: 441

I was able to solve a similar issue -- we have an api that requires the search terms to be part of the URL path. Passing the output directly to the proxy_pass directive caused it to throw a 502 even though the request was properly url encoded.

Here's the solution we came up with:

location ~ /api/search(/.*) {
        set $query $1;
        proxy_pass http://127.0.0.1:3003$query;

    }

The "set" directive seems to keep the url encoding intact (or re-encodes from what the regex is passing back in $1).

Upvotes: 43

Related Questions