Note that minniux's solution is not a rewrite, but will cause all traffic to pass through the "redirecting" host. This is not what I would want.
Use the rewrite directive instead:
location /files/.*\.(jpg|gif|png) {
rewrite ^/(.*)$ "$scheme://static.mydomain.com/$1" permanent;
}
You don't need to make the rewrite
's regex more complex; the location
block already affects only matching requests.
The $scheme
variable allows you to redirect to https or http depending on the original scheme.
PS: A redirect will also void the pain of caring for X-Forwarded-*
header fields on the static server.