0

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

2
  • What research have you done to figure this out for yourself?
    – EEAA
    Commented Mar 9, 2013 at 21:45
  • I've googled and also looked at similar ServerFault questions but could not find any solution for this particular problem.
    – alfish
    Commented Mar 9, 2013 at 21:53

2 Answers 2

1

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;
         }
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .