Welcome! Log In Create A New Profile

Advanced

Serious rewrite rule:

Posted by waldemar 
Serious rewrite rule:
November 03, 2010 08:07PM
Hi folks.

I've been assigned to create some new rewrite rules for our static-content servers (due to a change in our file system). This structure is based on the id of the elements, if I want to get an element with id 484792 the url would be:
[b]http://content.domain.com/v/484792[/b]
now, I'm trying nginx to serve the file from the path:
[b]/nfs/video/000/004/847/92/484792[/b]

more examples:
http://content.domain.com/v/3 -> /nfs/video/000/000/000/03/3
http://content.domain.com/v/123456789 -> /nfs/video/001/234/567/89/123456789

The problem is I can't figure out how to dynamically assign zero's to the left according to the length of the id. I've trying to use a recursive rule, but I'm concerned about the toll it takes on performance, as it's the only way I've found to append the zero's...

location ~ "/v/.+" {
root /path/to/nfs;
rewrite "^/v/(\d{3})(\d{3})(\d{3})(\d{2})(\d*)" /$1/$2/$3/$4/$5 last;
rewrite "^/v/(\d*)" /v/0$1 last;
expires max;
}

Is there any way to manage rules like this I might be missing?

Your advice would be much appreciated, thanks in advance.
Waldemar
Re: Serious rewrite rule:
November 04, 2010 05:28PM
Ok, problem solved!

location ~ "/v/(.+)" {
root /path/to/nfs;
rewrite "^/v/(\d*)" /v/0000000000$1/$1;
rewrite "^/v/(\d*)(\d{3})(\d{3})(\d{3})(\d{2})/(\d*)" /$2/$3/$4/$5/$6 break;
default_type video/mp4;
expires max;
}

The first rewrite adds the needed zeros, then the second one parses the string taking the numbers from the far right and leaving the exceeding zeros out.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 271
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready