Nginx rewrite rule to server images from subdomain

nginxrewrite

I'd like all my images (jpg|gif|png) which currently being serversd from mydomain.com/files/ to be servered from a second machine which I've set up to server files from static.mydomain.com.

I am wondering how should I define nginx rewrite rule to do so?

Thanks

Best Answer

You can use proxy pass: http://wiki.nginx.org/HttpProxyModule#proxy_pass

In your case this could be something like:

location /mydomain.com/files/.*\.(jpg|gif|png) {
       proxy_pass http://static.mydomain.com;
       proxy_set_header X-Real-IP  $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
Related Topic