Answer the question
In order to leave comments, you need to log in
How to rename a file on upload in Nginx?
Lord, help me. There is a site, there are pictures in it, there is a resizing of these pictures. The resize link looks like site.ru/images/2016/12/22/7c3b090855d3ec4c92722e6d276a8c02.jpg:h250 , i.e. via : sets the size parameter. And everything would be fine, but only when saving the image, it is assigned the extension .jpg-h250 .
I tried this build:
location ~* ^/.+?\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ {
rewrite ^/(.*)/([^/]+?)\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ /$1/resized-$4/$2.$3 break;
root /var/www/site/www/static;
if ($request_filename ~ "^.*/(.+):(.+)$"){
set $fname $1;
add_header 'Content-Disposition' 'inline;filename="$fname"';
}
}
Answer the question
In order to leave comments, you need to log in
It's decided.
Now, when requesting the site.ru/images/2016/12/22/7c3b090855d3ec4c92722e6d276a8c02.jpg:h250 type, a file with the required size is first generated, placed in a folder with a name corresponding to the parameter, and then redirected along the site.ru/images/2016/ type path 12/22/resized-h250/7c3b090855d3ec4c92722e6d276a8c02.jpg The config
looks like this:
location ~* ^/.+?\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ {
rewrite ^/(.*)/([^/]+?)\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ /$1/resized-$4/$2.$3 permanent;
root /var/www/site/www/static;
try_files $uri @resize_image;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question