Welcome! Log In Create A New Profile

Advanced

proxy_pass and trailing / decode uri

Posted by max 
max
proxy_pass and trailing / decode uri
March 08, 2018 10:40PM
Hi,

Sorry if it was already asked but I'd like to know if the only way to
prevent Nginx from decoding uri while using proxy_pass is:
https://stackoverflow.com/a/37584656/3515745

Here is my (simplified) conf:

server {
server_name domain1.com;

location / {
proxy_pass http://127.0.0.1:81;

}

location /api {
proxy_pass http://127.0.0.1:82/;

}
}

Location "/" is perfectly working. My problem is that "/api" location will
decode special character.
To illustrate my problem:

http://domain1.com/image1.png
=> HTTP 200
http://domain1.com/*api*/resource1.png
=> HTTP 200

http://domain1.com/image1+2.png
=> HTTP 200
http://domain1.com/*api*/resource1+2.png
=> HTTP 404

http://domain1.com/image1 2.png
=> HTTP 200
http://domain1.com/*api*/resource1 2.png
=> HTTP 404

I would like to know how to make my "/api" location respond like "/"
without decoding %?
This solution https://stackoverflow.com/a/37584656/3515745 seems to be
just a workaround.

Thanks for any hints !

Max
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: proxy_pass and trailing / decode uri
March 11, 2018 11:30PM
Hello!

On Thu, Mar 08, 2018 at 10:34:36PM +0100, max wrote:

> Sorry if it was already asked but I'd like to know if the only way to
> prevent Nginx from decoding uri while using proxy_pass is:
> https://stackoverflow.com/a/37584656/3515745
>
> Here is my (simplified) conf:
>
> server {
> server_name domain1.com;
>
> location / {
> proxy_pass http://127.0.0.1:81;
>
> }
>
> location /api {
> proxy_pass http://127.0.0.1:82/;
>
> }
> }
>
> Location "/" is perfectly working. My problem is that "/api" location will
> decode special character.

When you want nginx to replace matching part of the URI with "/",
it will do so on the decoded/normalized URI, and will re-encode
special characters in what's left.

If you want nginx to preserve original URI as sent by the client,
consider using proxy_pass without the URI part. That is,
instead of

proxy_pass http://127.0.0.1:82/;

use

proxy_pass http://127.0.0.1:82;

Note no trailing "/". This way the original URI as sent by the
client will be preserved without any modifications.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
max
Re: proxy_pass and trailing / decode uri
March 12, 2018 10:00AM
Hi,

When you want nginx to replace matching part of the URI with "/",
> it will do so on the decoded/normalized URI, and will re-encode
> special characters in what's left.
>
> If you want nginx to preserve original URI as sent by the client,
> consider using proxy_pass without the URI part. That is,
> instead of
>
> proxy_pass http://127.0.0.1:82/;
>
> use
>
> proxy_pass http://127.0.0.1:82;
>
> Note no trailing "/". This way the original URI as sent by the
> client will be preserved without any modifications.
>


Thank you for your answer but it is not correct for location different than
'/'. With your proposal, targeting http://domain1.com/api/foo/bar, socket
on port 82 receives: /api/foo/bar. I guess the only way to remove the /api
part is "rewrite" and involves re-encoding...

Max.
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
max
Re: proxy_pass and trailing / decode uri
March 12, 2018 10:00AM
Sorry for double post:
>
> I guess the only way to remove the /api part is "rewrite" and involves
> re-encoding...

=> I guess the only way to remove the /api without re-encoding URI is
"rewrite" ...

Max

2018-03-12 9:55 GMT+01:00 max <[email protected]>:

> Hi,
>
> When you want nginx to replace matching part of the URI with "/",
>> it will do so on the decoded/normalized URI, and will re-encode
>> special characters in what's left.
>>
>> If you want nginx to preserve original URI as sent by the client,
>> consider using proxy_pass without the URI part. That is,
>> instead of
>>
>> proxy_pass http://127.0.0.1:82/;
>>
>> use
>>
>> proxy_pass http://127.0.0.1:82;
>>
>> Note no trailing "/". This way the original URI as sent by the
>> client will be preserved without any modifications.
>>
>
>
> Thank you for your answer but it is not correct for location different
> than '/'. With your proposal, targeting http://domain1.com/api/foo/bar,
> socket on port 82 receives: /api/foo/bar. I guess the only way to remove
> the /api part is "rewrite" and involves re-encoding...
>
> Max.
>
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: proxy_pass and trailing / decode uri
March 12, 2018 01:30PM
Hello!

On Mon, Mar 12, 2018 at 09:55:15AM +0100, max wrote:

> > When you want nginx to replace matching part of the URI with "/",
> > it will do so on the decoded/normalized URI, and will re-encode
> > special characters in what's left.
> >
> > If you want nginx to preserve original URI as sent by the client,
> > consider using proxy_pass without the URI part. That is,
> > instead of
> >
> > proxy_pass http://127.0.0.1:82/;
> >
> > use
> >
> > proxy_pass http://127.0.0.1:82;
> >
> > Note no trailing "/". This way the original URI as sent by the
> > client will be preserved without any modifications.
> >
>
>
> Thank you for your answer but it is not correct for location different than
> '/'. With your proposal, targeting http://domain1.com/api/foo/bar, socket
> on port 82 receives: /api/foo/bar. I guess the only way to remove the /api
> part is "rewrite" and involves re-encoding...

Whether this is correct or no depends on the particular setup - in
particular, it depends on what your backend expects as an URI. If
your backend is picky about specific forms of encoding, preserving
full original URI might be much better option than trying to
invent hacky workarounds like the one you've linked. Obviously
enough, this might either envolve re-configuring the backend to
accept full original URIs, or hosting things on a dedicated
domain.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
max
Re: proxy_pass and trailing / decode uri
March 12, 2018 02:00PM
>
> Whether this is correct or no depends on the particular setup - in
> particular, it depends on what your backend expects as an URI. If
> your backend is picky about specific forms of encoding, preserving
> full original URI might be much better option than trying to
> invent hacky workarounds like the one you've linked. Obviously
> enough, this might either envolve re-configuring the backend to
> accept full original URIs, or hosting things on a dedicated
> domain.
>
Yes sub-domains would be the best option but I'm already listening on
sub-domain and do not want to make an other sub-level.
I cannot configure my backends (several backends) to be able to listen on
/api, this is exactly why I needed to use Nginx as reverse proxy.

2018-03-12 13:28 GMT+01:00 Maxim Dounin <[email protected]>:

> Hello!
>
> On Mon, Mar 12, 2018 at 09:55:15AM +0100, max wrote:
>
> > > When you want nginx to replace matching part of the URI with "/",
> > > it will do so on the decoded/normalized URI, and will re-encode
> > > special characters in what's left.
> > >
> > > If you want nginx to preserve original URI as sent by the client,
> > > consider using proxy_pass without the URI part. That is,
> > > instead of
> > >
> > > proxy_pass http://127.0.0.1:82/;
> > >
> > > use
> > >
> > > proxy_pass http://127.0.0.1:82;
> > >
> > > Note no trailing "/". This way the original URI as sent by the
> > > client will be preserved without any modifications.
> > >
> >
> >
> > Thank you for your answer but it is not correct for location different
> than
> > '/'. With your proposal, targeting http://domain1.com/api/foo/bar,
> socket
> > on port 82 receives: /api/foo/bar. I guess the only way to remove the
> /api
> > part is "rewrite" and involves re-encoding...
>
> Whether this is correct or no depends on the particular setup - in
> particular, it depends on what your backend expects as an URI. If
> your backend is picky about specific forms of encoding, preserving
> full original URI might be much better option than trying to
> invent hacky workarounds like the one you've linked. Obviously
> enough, this might either envolve re-configuring the backend to
> accept full original URIs, or hosting things on a dedicated
> domain.
>
> --
> Maxim Dounin
> http://mdounin.ru/
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
>
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login