Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What would be the best way to map this URL to this filesystem path?

From

http://site.com/200x300/ed3269d0-f9ef-4ffc-abea-5982969876c0/my file.jpg

To

/var/www/e/d/3/ed3269d0-f9ef-4ffc-abea-5982969876c0/200x300.jpg

Where

  • "200x300" is the size of the image
  • "ed3269d0-f9ef-4ffc-abea-5982969876c0" is the guid of the image
  • "my file" is the name of the file (used for SEO, may contain spaces)
  • "e", "d" and "3" are the first 3 letters of the guid
  • ".jpg" is the extension of the file
share|improve this question

2 Answers 2

up vote 5 down vote accepted

Try something like following:

location / {
   location ~* ^/(\d+x\d+)/((\w)(\w)(\w)[-\w]+)/[^/]+\.jpg$ {
      alias /var/www/$3/$4/$5/$2/$1.jpg;
   }
}
location /some_other_location/ { ... }
share|improve this answer
    
Why the use of a location inside of another location? –  ZogStriP Jan 18 '12 at 9:10
    
In case you have other non-regex locations (/some_other_location/ in my example) you'll be sure that they would not be mapped by regex location by mistake. Also, /some_other_location/ requests won't be checked by this regex and their processing would be slightly faster. –  rvs Jan 18 '12 at 9:43
    
Thanks for your answer! –  ZogStriP Jan 18 '12 at 9:48

Here's what I've come up (as I need to extract the extension too):

server {
   root /var/www;

   location ~* ^/(\d+x\d+)/((\w)(\w)(\w)[-\w]+)/[^\.]+\.(\w+)$ {
      try_files /$3/$4/$5/$2/$1.$6 /index.html;
   }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.