How to write a rule in nginx so that when requesting pictures

http://site.ru/ping/images/324565789/i.png

redirected to address

http://site.ru/ping/images/324565789/

?

Host config text:

server { listen 000.000.000.000:80; server_name site.ru; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { root /srv/nginx/site/www/; index index.htm index.html index.php; autoindex off; # don't check $uri/, send to php for nice error message try_files $uri /index.php?$query_string; } location ~ /\. { deny all; } location ~ \.php$ { root /srv/nginx/site/www/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/nginx/site/www/$fastcgi_script_name; include fastcgi_params; } location ~* \.css|\.js|\.jpg|\.jpeg|\.png|\.gif|\.swf|\.svg|\.tiff|\.pdf$ { root /srv/nginx/site/www/; }} 

I finish at the end:

  location /ping/images/ { rewrite ^/ping/images/(\d+)/i\.png$ /ping/images/$1/ last; } 

gives 404 (even on / ping / images /).

I finish at the end:

 location /ping/ { rewrite ^/ping/images/(.+)/.+\.png$ http://site.ru/ping/images/$1/ break;} 

gives 404.

    2 answers 2

    This is how it should work:

     rewrite ^/ping/images/(\d+)/i\.png$ /ping/images/$1/ redirect; 

    This is the simplest option to make it easier to understand. I would also tighten the regular expression conditions. For example, if only numbers follow / images /, then I would use the \d+ condition, for file names something like [a-z0-9_-] . In general, according to circumstances.

    More details can be found in the nginx documentation .

    UPD: removed the location, here it is superfluous. Thank you, @pyatak , suggested. The task is not difficult, you just need to carefully read the documentation.

    • The server gives 404. - temoffey
    • Updated the answer. The location context was redundant. - Acrux

    Well, Duc is just the same .... The docks are completely lazy to read

     rewrite .*(/[0-9]*)/(.*).png $1/ redirect;