Rewrite rule for Nginx

Hello people.
There is a problem, inexperience in configuring Nginx can't handle it.

There is a site, it is one of the subfolders:
/customer_data/pictures/
it has subfolders, but this is not so important. For example: customer_data/pictures/2008/07/30/a_1/places/13496/4c718.jpg

The name of the target file can be one of three things: 4c718.jpg, big_4c718.jpg and sm_4c718.jpg

And the task is to add the name of the file of arbitrary text in different languages. Ie to write to. htaccess that looks like this:

customer_data/pictures/2008/07/30/a_1/places/13496/__a photo of my бабки__4с718.јрд
Moreover, the additional separator text, you can choose what is convenient.

According to the materials of this article have tried, but something somewhere doesn't add up.
October 8th 19 at 02:02
1 answer
October 8th 19 at 02:04
rewrite "^(.*/)([^/]*__)?((big_|sm_)?[a-z\d]+\.(jpg|gif|png))$" $1$3 last;
After proizvolnogo text should go double underline. - Tyshawn11 commented on October 8th 19 at 02:07
Thank you!
This time, I understand that in order not to check this condition for all queries, you need to add about a block to the config:

location ~* ^/customer_data/pictures/{
rewrite "^(.*/)([^/]*__)?((big_|sm_)?[a-z\d]+\.(jpg|gif|png))$" $1$3 last;
}

Is that right? Regular season ~* ^/customer_data/pictures/ correct?

Thanks again! - else.Nienow commented on October 8th 19 at 02:10
You don't need the regular season in the location. And I also need to rewrite, so she once again did not work.

The result:
location /customer_data/pictures/ {
 rewrite "^(.*/)[^/]*__((big_|sm_)?[a-z\d]+\.(jpg|gif|png))$" $1$3 last;
...
}
- Tyshawn11 commented on October 8th 19 at 02:13
Made a mistake :) At the end of "$1$2 last;" (2 instead of 3) - Tyshawn11 commented on October 8th 19 at 02:16
Thanks again. Now go be screwed. I'm sure all will turn out now - else.Nienow commented on October 8th 19 at 02:19
Eventually worked like this:

location ~ ^.*/customer_data/pictures/.*\.(jpg|gif|png)$ {
rewrite "^(.*/)[^/]*__((big_|sm_)?[a-z\d]+\.(jpg|gif|png))$" $1$2 last;
...
}

I.e. without regularnie in the location failed to do.
Attacks all cool - else.Nienow commented on October 8th 19 at 02:22
So you have a longer "simple" location or any location with a regular expression. It would be better to do without regexps.
nginx.org/ru/docs/http/ngx_http_core_module.html#location - Tyshawn11 commented on October 8th 19 at 02:25
There seems to be don't see anyone interrupting this rule. Here's all the locations:

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~* ^.*/customer_data/pictures/.*\.(jpg|gif|png)$ {
# location /customer_data/pictures/ {
rewrite "^(.*/)[^/]*__((big_|sm_)?[a-z\d]+\.(jpg|gif|png))$" $1$2 last;
root /opt/lampp/htdocs/;
expires max;
}

location ~ \.(jpg|jpeg|gif|bmp|png|js|css)$ {
root /opt/lampp/htdocs/;
expires max;
}

location ~ \.php$ {
proxy_pass 127.0.0.1:81;
}

location ~ /$ {
#All requests ends with / goes to the Apache
proxy_pass 127.0.0.1:81;
} - else.Nienow commented on October 8th 19 at 02:28
This interrupts:
location ~ \.(jpg|jpeg|gif|bmp|png|js|css)$
- Tyshawn11 commented on October 8th 19 at 02:31
Strange, I thought again, and he and regularnie, it fulfills the first match. - else.Nienow commented on October 8th 19 at 02:34
Right. So in fact it was about this:
I.e. without regularnie in the location failed to do.
- Tyshawn11 commented on October 8th 19 at 02:37
Should finally read the documentation? nginx.org/r/location/ru

location ^~ /customer_data/pictures/ {
 rewrite "^(.*/)[^/]*__((?:big_|sm_)?[a-z\d]+\.(?:jpg|gif|png))$" $1$2 last;
 root /opt/lampp/htdocs/;
 expires max;
}
- Ignatius.Fadel34 commented on October 8th 19 at 02:40
I understand that this location is kind of like to work, but in my config You proposed is not working. The condition of rewrite Your operate as well as
Documentation of course read first.
In principle, the issue is considered closed. Though not 100% optimal, but it works.
And thanks for the help!
- else.Nienow commented on October 8th 19 at 02:43

Find more questions by tags NginxProcessing URL