Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

From file.jpg




  • "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
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


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.