Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nginx

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 ?

like image 979
TomPAP Avatar asked Jan 12 '12 16:01

TomPAP


People also ask

Can you use NGINX as a proxy?

Nginx can proxy requests to servers that communicate using the http(s), FastCGI, SCGI, and uwsgi, or memcached protocols through separate sets of directives for each type of proxy.

What is proxy buffering in NGINX?

Proxy buffering is enabled by default in NGINX (the proxy_buffering directive is set to on ). Proxy buffering means that NGINX stores the response from a server in internal buffers as it comes in, and doesn't start sending data to the client until the entire response is buffered.

Why use NGINX reverse proxy for Apache?

Using Nginx as a reverse proxy for Apache will allow both servers to work together and allow you to take advantage of the benefits of both servers. You can easily monitor what traffic goes in and out through the reverse proxy.

What is proxy buffer size?

Default: proxy_buffers 8 4k|8k; Context: http, server, location. Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.


1 Answers

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).

like image 189
steamedcotton Avatar answered Sep 24 '22 05:09

steamedcotton