Nginx url decoding URI problem with proxy_pass

Michael Kovacs kovacs at gmail.com
Thu Jul 26 17:18:07 UTC 2018


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20180726/95944021/attachment.html>


More information about the nginx mailing list