A
A
Alexey2020-04-22 10:55:04
Nginx
Alexey, 2020-04-22 10:55:04

How to handle files with %-encoded names in rewrite rule in nginx?

There is a site on 1C-Bitrix with images in JPEG and PNG formats. I found an article https://camouf.ru/blog-note/7780/ , which tells how to give WebP instead of JPEG and PNG on the fly. Implemented like this:

location ~* ^.+\.(jpg|jpeg|png)$ 
{
  if ( $http_accept ~* webp ) {
  set $webp "A";
  }
  if ( $request_filename ~ (.+)\.(png|jpe?g)$ ) {
  set $file_without_ext $1;
  }
  if ( -f $file_without_ext.webp ) {
  set $webp "${webp}E";
  }
  if ( $webp = AE ) {
  add_header Vary Accept;
  rewrite ^(.+)\.(png|jpe?g)$ $1.webp break;
  }
  expires 365d;
}


The problem is that rewrite only works if the file name contains latin characters. If the file name contains Cyrillic or a space, then rewrite does not work, most likely due to the fact that the file name is %-encoded. Is there an easy way to get nginx to decode the content of $request_filename?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-04-22
@Vidalia

map $http_accept $img_suffix {
  "~*webp"  ".webp";
}

server {

    location ~ ^(?<filename>.+)\.(png|jpe?g)$ {
        try_files $filename$img_suffix $uri =404;
    }

V
Viktor Taran, 2020-04-22
@shambler81

google page speed does not use the "mistakes" found on the site in assessing the speed of the site.
they are advisory.
It evaluates the average speed of a site in its subject matter no more, no less.
Translation in webp will not give you anything in this regard.
If its use gave a real increase, everyone would have done it long ago. (the format itself does not do miracles, as you understand)
If you really want to speed up
1. take vps with 9900k (the number of megahertz per core for the site is more important than their number)
2. in russia (if there is money for it), closer, faster works .
3. Rework the site caching system in 99% it is miserable. and you can speed it up 10-20 times
If it works well, rewrite the heaviest queries to pure D7.
Etc.
Believe me, the web will be in the millionth place there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question