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.

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.:

/images/1/5/3/153.png

(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:

/images/153.png

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

share|improve this question

1 Answer 1

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

 
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.