nginx rewrite

Maxim Dounin mdounin at mdounin.ru
Mon May 2 23:19:06 MSD 2011


Hello!

On Mon, May 02, 2011 at 10:57:16PM +0400, Igor Sysoev wrote:

> On Mon, May 02, 2011 at 08:48:50PM +0200, Yanxin Z. wrote:
> > Hello Igor Sysoev,
> > Thank you for your help.
> > 
> > You suggestion is great, but my problem is a little bit complicated.
> > 
> > The image name is flexible, can be from 0.jpg to 23456.jpg, we organize 
> > the image files into folders, whose name is the number divide by 1000. 
> > For example, the folder 100 saves the file 100000.jpg to 100999.jpg.
> > 
> > Both the folder and file name are flexible in my case.
> > 
> > Do you have any suggestion on it?
> 
> location /img/ {
> 
>     location ~ ^/img/(\d)(\.jpg)$ {
>         alias   /path/to/img/0$1/$1$2;
>     }
> 
>     location ~ ^/img/(\d\d)(\d*\.jpg)$ {
>         alias   /path/to/img/$1/$1$2;
>     }
> 
>     return 404;
> }

As far as I understand original question, the following should 
work (same as above, but fixed to use division by 1000 instead of 
two first digits):

location /img/ {

    location ~ ^/img/(\d+)(\d\d\d\.jpg)$ {
        alias /path/to/img/$1/$1$2;
    }

    location ~ ^/img/(\d+\.jpg)$ {
        alias /path/to/img/0/$1;
    }

    return 404;
}

Maxim Dounin



More information about the nginx mailing list