Migrate from apache mod_rewrite

Igor Sysoev is at rambler-co.ru
Mon Feb 9 18:16:15 MSK 2009


On Mon, Feb 09, 2009 at 03:52:21PM +0100, Andreas Lehr wrote:

> I`m trying to migrate one of our external programmed tools to nginx from 
>  traditional apache.
> 
> But unfortunately I screw things up on migrating apache mod_rewrite to 
> nginx mod_rewrite!
> 
> Would be great, if one of you could help me on this.
> The mod_rewrite sections in apache looks like this:
> 
>     RewriteEngine On
> 
>     # if thumb exists, output directly
>     RewriteCond %{QUERY_STRING} ^(thumb|normal)$
>     RewriteCond %{DOCUMENT_ROOT}/cache/$1/%{QUERY_STRING}.jpg -f
>     RewriteRule (.*)\.jpg$ /cache/$1/%{QUERY_STRING}.jpg [L]
> 
>     # otherwise redirect all jpg-image-requests to processing
>     # script if they are not in cache dir
>     RewriteCond %{REQUEST_URI} !^/cache
>     RewriteRule \.jpg$ /image_processing.php [QSA,L]
> 
> 
> How to do this with nginx?

If you use 0.6.35, then get http://sysoev.ru/nginx/patch.try_files.0.6.35
and try the following:

server {

     ...

     set $image "";

     if ($uri ~ (.*)\.jpg$) {
         set  $image  $1;
     }

     location ~ (.+)\.jpg$ {
         try_files  /cache/$image/$args.jpg  @image;
     }

     location @images {
         proxy_pass  http://backend/image_processing.php;
     }

     ...


I do want to add captures in regex location's:

     location ~ (.+)\.jpg$ {
         try_files  /cache/$1/$args.jpg  @image;
     }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list