Apache rewrite in Nginx format

Philip Murray pmurray at open2view.com
Thu Sep 27 06:04:31 MSD 2007


On 26/09/2007, at 11:14 PM, Igor Sysoev wrote:

> On Wed, Sep 26, 2007 at 04:18:43PM +1200, Philip Murray wrote:
>
>> Is it possible to express the following Apache rewrite rule in Nginx?
>>
>>     RewriteCond %{QUERY_STRING} !^nobranding$
>>     RewriteCond %{REQUEST_FILENAME} ^/assets/XL/([^.]+)\.([jpegif]+)$
>>     RewriteCond /usr/local/www/assets-nz/XL/%1.branded.%2 -f
>>     RewriteRule ^/([^.]+)\.([jpeg]+)$ /$1.branded.$2
>>
>> In Nginx, this would be in a location block with an alias, so
>>
>> location /assets {
>>     alias /usr/local/www/assets-nz/;
>>
>>     ... rewrites go here ...
>> }
>>
>> But, I understand you can't rewrite with an alias present? So how can
>> I implement the above Apache rewrite?
>
> Try
>
>   location /assets {
>
>       if ($args = nobranding) {
>           rewrite  ^(.+)$ /nobranding$1  last;
>       }
>
>       rewrite  ^/assets/XL/(.+)\.(jpeg|jpg|gif)  /XL/$1.branded.$2;
>       root /usr/local/www/assets-nz;
>       error_page  404  =  /nobranding/assets$uri;
>   }
>
>
>   location /nobranding/assets {
>       internal;
>       alias /usr/local/www/assets-nz/;
>   }
>

Thanks Igor, that works... but to throw a spanner into the works, I  
have at the end of this configuration:

location / {
	root  /path/to/rails/public/
	# ... lots of rewrites for fixing up old URLs
	
	if (!-f $request_filename) {
		proxy_pass http://mongrel;
		break;
	}
}

And the !-f/proxy_pass seems to pick up the request in the case where  
blah.branded.jpeg doesn't exist (and should just serve blah.jpeg  
instead), and thus passing it through to mongrel.

Is there a way to force the /nobranding/assets location to be used.  
Do rewrites alter $uri ?

Cheers

Phil






More information about the nginx mailing list