How to rewrite a path ending with a .jpg extension
from https://example.com/something/something/image.jpg
or https://example.com/something/something/something/something/image.jpg
to
https://example.com/image.jpg
but do not rewrite https://example.com/image.jpg
You can use the character class [^/] to match any character that is not a /.
Any URI that contains at least one character before a / will not match /image.jpg.
For example:
rewrite .(/[^/]+\.jpg)$ $1 permanent;