James M
James M

Reputation: 558

How to handle special symbols in nginx

please help me fix the issue. I have nginx that resize thumbs. My problem is when i request image with symbols in name, i get error. for example if i request:

http://example.com/thumbs/_default_upload_bucket/75/75/test+.jpg

then i get error 415 Unsupported Media Type. And in error log i see

[error] 509#509: *62531 open() "/var/www/html/assets/_default_upload_bucket/test%2B.jpg" failed (2: No such file or directory)

Why is that happens, and how can i solve this. Here is my nginx configuration. Please help me with that. Thank you.

location ~* /thumbs/(.*)/(\d+)/(\d+)/(.*)$ {
            set $bucket $1;
            set $width $2;
            set $height $3;
            set $filename $4;
    try_files /$1/$2/$3/$4 @images;
    root /var/www/html/tmp/image-thumbnails;
    expires 2w;
    access_log off;
}


location @images {
            root /var/www/html/assets/;
            rewrite ^.*$ /$bucket/$filename break;
            image_filter_buffer 50M;
            image_filter resize $width $height;
}

Upvotes: 0

Views: 1584

Answers (1)

Richard Smith
Richard Smith

Reputation: 49692

The problem appears to originate from the set $filename $4; statement. The $filename variable becomes URL-encoded which prevents Nginx from finding the correct file.

You could avoid using the set directives by using named captures instead.

For example:

location ~* /thumbs/(?<bucket>.*)/(?<width>\d+)/(?<height>\d+)/(?<filename>.*)$ {
    try_files /$1/$2/$3/$4 @images;
    ...
}
location @images {
    rewrite ^.*$ /$bucket/$filename break;
    ...
}

As an alternative to the rewrite...break you could also use try_files /$bucket/$filename =404; which may be more efficient and matches the implementation in the previous block.

Upvotes: 2

Related Questions