Welcome! Log In Create A New Profile

Advanced

Nginx url decoding URI problem with proxy_pass

Posted by Michael Kovacs 
Michael Kovacs
Nginx url decoding URI problem with proxy_pass
July 26, 2018 07:20PM
Greetings Nginx mailing list!

I'm using nginx as an image proxy and am using proxy_pass to fetch the
image. Unfortunately if that image URL has a space in it the proxy_pass
fails. It works fine with any other image.

example successful URL:

/image_preview/https://somedomain.com/image.jpg

example failedl URL:

/image_preview/https://somedomain.com/My%20Images/image.jpg

^^
Nginx is URL decoding the url in the path and putting the space back in.

Here's my nginx.conf

# redirect thumbnail url to real world
location ~ ^/image_preview/(.*?)$ {
resolver ${HOST};

set $fallback_image_url ${FALLBACK_IMAGE_URL};
set $image_url $1;

if ($args) {
set $image_url $1?$args;
}

proxy_intercept_errors on;
error_page 301 302 303 305 306 307 308 $fallback_image_url;
error_page 400 401 402 403 404 405 406 409 408 409 410 411 412
413 414 415 416 417 $fallback_image_url;
error_page 500 501 502 503 504 505 506 507 508 509 510 511 520
522 598 599 $fallback_image_url;

proxy_connect_timeout 2s;
proxy_read_timeout 4s;
proxy_pass_request_headers off;
proxy_buffering off;
proxy_redirect off;

proxy_pass $image_url;
proxy_set_header If-None-Match $http_if_none_match;
proxy_set_header If-Modified-Since $http_if_modified_since;
}

I've scoured the docs, stackoverflow, and the internet in general but don't
see how to address this problem. As I see it I have two options:

1) Find a way to make nginx not URL decode the param URL (doesn't seem
possible)
2) The original $request_uri contains the URL encoded URL. Find a way to
create a rewrite rule to strip off the prefix and proxy_pass to the
resulting URL. I haven't found a way to do something liek that as it
appears rewrite rules will only operate on the URI in context and that URI
appears to be decoded.

I've found an entire chapter in "Nginx Troubleshooting" on creating a proxy
for external links. But that example also appears to fall vicitm to this
same problem.

Any help/pointers would be appreciated as I am pretty well stuck at this
point on an approach that might work.

Thanks,
-Michael
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Igor A. Ippolitov
Re: Nginx url decoding URI problem with proxy_pass
July 26, 2018 09:10PM
Michael,

You can use rewrite. Just catch the host part:
>rewrite /image_preview/https://(?<my_host>;[^ :/]/(.*) /$1;
>proxy_pass https://$my_host
rewrite will encode the URL back again.

On 26.07.2018 20:18, Michael Kovacs wrote:
> Greetings Nginx mailing list!
>
> I'm using nginx as an image proxy and am using proxy_pass to fetch the
> image. Unfortunately if that image URL has a space in it the
> proxy_pass fails. It works fine with any other image.
>
> example successful URL:
>
> /image_preview/https://somedomain.com/image.jpg
> https://somedomain.com/image.jpg
>
> example failedl URL:
>
> /image_preview/https://somedomain.com/My%20Images/image.jpg
> https://somedomain.com/My%20Images/image.jpg
>
> ^^
> Nginx is URL decoding the url in the path and putting the space back in.
>
> Here's my nginx.conf
>
>         # redirect thumbnail url to real world
>         location ~ ^/image_preview/(.*?)$  {
>             resolver ${HOST};
>
>             set $fallback_image_url ${FALLBACK_IMAGE_URL};
>             set $image_url $1;
>
>             if ($args) {
>                 set $image_url $1?$args;
>             }
>
>             proxy_intercept_errors on;
>             error_page 301 302 303 305 306 307 308 $fallback_image_url;
>             error_page 400 401 402 403 404 405 406 409 408 409 410 411
> 412 413 414 415 416 417 $fallback_image_url;
>             error_page 500 501 502 503 504 505 506 507 508 509 510 511
> 520 522 598 599 $fallback_image_url;
>
>             proxy_connect_timeout 2s;
>             proxy_read_timeout 4s;
>             proxy_pass_request_headers off;
>             proxy_buffering off;
>             proxy_redirect off;
>
>             proxy_pass $image_url;
>             proxy_set_header If-None-Match $http_if_none_match;
>             proxy_set_header If-Modified-Since $http_if_modified_since;
>         }
>
> I've scoured the docs, stackoverflow, and the internet in general but
> don't see how to address this problem. As I see it I have two options:
>
> 1) Find a way to make nginx not URL decode the param URL (doesn't seem
> possible)
> 2) The original $request_uri contains the URL encoded URL. Find a way
> to create a rewrite rule to strip off the prefix and proxy_pass to the
> resulting URL. I haven't found a way to do something liek that as it
> appears rewrite rules will only operate on the URI in context and that
> URI appears to be decoded.
>
> I've found an entire chapter in "Nginx Troubleshooting" on creating a
> proxy for external links. But that example also appears to fall vicitm
> to this same problem.
>
> Any help/pointers would be appreciated as I am pretty well stuck at
> this point on an  approach that might work.
>
> Thanks,
> -Michael
>
>
> _______________________________________________
> 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