How can Nginx rewrite the access path of a thousand-bit directory without a thousand-bit directory through rewrite after adding the thousand-bit directory of the self-added ID?

problem description

how to rewrite the access path of a thousand-bit directory without a thousand-bit directory through rewrite after Nginx follows the thousand-bit directory of the self-incremented ID?

directory structure

/image/
    Floor(id/1000)/
        id.jpg
        
/image/
    0/
        1.jpg
        234.jpg
    123/
        123000.jpg
        123456.jpg
        123999.jpg

goal

https://img.codeshelper.com/upload/img/2021/10/14/ejvx2sf0uli135.jpg visit / image/0/234.jpg
https://img.codeshelper.com/upload/img/2021/10/14/pe4d50atgu5136.jpg visit / image/123/123456.jpg

Oct.14,2021

solve

server {
    listen       80;
    location /image/ {
        alias        /image/;
        rewrite      "^/image/(\d{1,3})\.jpg$" /image/0/$1.jpg last;
        rewrite      "^/image/(\d{1,})(\d{3})\.jpg$" /image/$1/$1$2.jpg last;
    }
}
Menu