You're currently matching only the exact location /folder/
. You should edit that to match all locations beginning with /folder/
:
location ^~ /folder/ {
Also, your rewrite has one and a half errors. If I request http://www.mydomain.com/folder/123.png
, then you will redirect me to http://cdn.mydomain.com/folder//folder/123.png
, which is most probably not correct. Below is the complete location and rewrite directive. The question mark after $request_uri
makes sure that a potential query string only gets added once.
location ^~ /folder/ {
rewrite ^ http://cdn.mydomain.com$request_uri? permanent;
}