user2980769
user2980769

Reputation: 141

Nginx rewrite location path

Is there a way I can add a rewrite which will dynamically set the location/path of the file?

The following is from the nginx config:

server {
   root /media;
   server_name media.domain.com;
   location / {
       autoindex off;
 }

I have images with names like "e9m7L4_1.jpg" that are stored in a directory according to the first 6 letters/numbers of the filename, for example:

e9m7L4_1.jpg (stored in)-> e/9/m/7/L/4/e9m7L4_1.jpg

km40lj_1.jpg (stored in)-> k/m/4/0/l/j/km40lj_1.jpg

Currently I can access it like this:

http://media.domain.com/e/9/m/7/L/4/e9m7L4_1.jpg

Is there a way to rewrite the location using filename passed to nginx so it could be accessed like this, without the long directory path/prefix: http://media.domain.com/e9m7L4_1.jpg

Thanks

Upvotes: 1

Views: 1638

Answers (1)

Alexey Ten
Alexey Ten

Reputation: 14354

You could try this:

server {
    server_name media.domain.com;
    root /media;
    location / {
       rewrite ^/((.)(.)(.)(.)(.)(.).+)$ /$2/$3/$4/$5/$6/$7/$1 break;
    }
}

Upvotes: 1

Related Questions