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

I have to store quite a lot of images (lets say 1M) for serving from Nginx, so to avoid having too many files in one folder I'm planning to break them out into folders with names related to the file name, e.g.:


(I realise that there are methods to achieve more even distribution, but I think this will be adequate for our needs)

However, I'd like these to be accessible from a URL without the directory structure, ie:


Is this possible to accomplish in Nginx with e.g. URL rewriting?

share|improve this question
up vote 0 down vote accepted

Ah, found it in the documentation (didn't see it earlier). Looks like it's pretty simple with the HttpRewriteModule.

Note: for curly braces( { and } ), as they are used both in regexes and for block control, to avoid conflicts, regexes with curly braces are to be enclosed with double quotes (or single quotes). For example, to rewrite URLs like:

/photos/123456 to:

/path/to/photos/12/1234/123456.png use the following (note the quotes enclosing the regex):

rewrite "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;

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.