views:

567

answers:

2

I need to do a rewrite with nginx from /blah/.../3275 to /id/3275 if the second file exists, otherwise I want to hand it off to apache. Here is my (feeble) attempt

(...) represents irrelevant stuff

if ($request_filename ~^/.../([0-9]+)/$) {
               if (-d /id/$1) {
                        rewrite ^/.../[0-9]+/([0-9]+)/$ /id/$1;
                }
        }

Does anyone have any ideas

+1  A: 

Best to do this with internal rewrites:

set $original_uri $uri;

location /blah/irrelevant_stuff {
   error_page 404 = @apache;
   rewrite ^/blah/irrelevant_stuff/([0-9]+)$ /id/$1;
}

location @apache {
   proxy_pass http://upstream$original_uri;
}
wulong
Very nice. I didn't realize you could save the original uri like that. I'll try it out
A: 

The above answer from wulong I couldn't get to work for some reason but I did get it to work by using

  if (!-e $request_filename) {
    proxy_pass http://apache$original_uri;
    break;
  }

rather than the error_page directive. Same idea basically