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

/images/395.4b2a-c6b.large-red/car-image.jpg

to

/thumb.php?param1=4b2a6aa&param2=c6bc&id=395&size=large&color=red

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
add comment

1 Answer

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
add comment

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.