Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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'm trying to create a rewrite rule with nginx which rewrites a non-existent image uri to dynamic page with params but failing.




Param details in request are:

395 . 4b2a        - c6b         . large - red   /blabla-image.jpg
id  . 4 char hash - 3 char hash . size  - color

Colours are limited with : red | blue | green

Sizes are limited too : small | large

My non-working rewrite rule is:

location ~* \.(jpg)$ {
   rewrite ^/images/([0-9-]+)\.[a-zA-Z0-9]{4}+\-[a-zA-Z0-9]{3}+\.(.*)\-(.*)/(.*)$ /thumb.php?id=$1&param1=$2&param3=$3&size=$4&color=$5 last;

What is wrong with this rule? Any ideas?

share|improve this question

Using Nginx 1.4.2, your location produced an error with the message...

nginx: [emerg] directive "rewrite" is not terminated by ";" in /path/to/conf

It's because, as per Nginx docs...

If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.

Fixed it by enclosing the rewrite condition in quotes.

What is wrong with this rule? Any ideas?

You missed the capturing braces too.

Here's the working solution...

rewrite "^/images/([0-9-]+)\.([a-zA-Z0-9]{4}+)\-([a-zA-Z0-9]{3}+)\.(.*)\-(.*)/(.*)$" /thumb.php?id=$1&param1=$2&param3=$3&size=$4&color=$5 last;

Your solution can be improved further, like...

rewrite "^/images/([0-9-]+)\.([a-zA-Z0-9]{4}+)\-([a-zA-Z0-9]{3}+)\.([a-zA-Z]+)\-([a-zA-Z]+)/(.*)$" /thumb.php?id=$1&param1=$2&param3=$3&size=$4&color=$5 last;
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.