Too Many Redirects - CDN Rewrite Rule

Francis Daly francis at daoine.org
Fri Mar 22 09:27:24 UTC 2013


On Thu, Mar 21, 2013 at 06:48:43PM -0400, toddlahman wrote:

Hi there,

> The reply I received from NetDNA after supplying the same information as I
> did here is as follows:
> 
> "Too many redirects" is a legit message in this scenario - example: You are
> redirecting domain.com/file.jpg TO cdn.domain.com/file.jpg ---> request
> comes to CDN and CDN neds to cache this file from origin so it tries to
> fetch from origin from location "domain.com/file.jpg" ---> request comes to
> origin and redirect rule you made redirects this request back to CDN <---
> this is where infinite loop starts.

Ah, okay, that makes sense. You don't upload to the CDN; it instead
mirrors your content on demand. (That was the part I had missed.)

As was mentioned, the correct solution is for all of your links to
actually be to things like http://cdn.domain.com/file.jpg; but as was
also mentioned, that is not a quick change for you.

The NetDNA suggestion -- to serve when it comes from them and to redirect
when it comes from others -- is reasonable; but as they also indicate,
"when it comes from them" is non-trivial to get right, and will be a
problem if you ever get it wrong in one direction.

Using proxy_pass in your nginx would defeat the main purpose of using
a CDN, as your own bandwidth would be used always.

An alternative possibility could be for you to set up another
server{} block with a server_name of (say) cdn-src.domain.com which
has the same document root as your main one, but which just serves
the static files.  Then, at the point where your CDN is configured to
map cdn.domain.com/file.jpg -> domain.com/file.jpg, map it instead to
cdn-src.domain.com/file.jpg.

With no change to your application, your users go to domain.com/something
and follow a link to domain.com/file.jpg; your "main" server redirects
them to cdn.domain.com/file.jpg; they get that, which fetches (now
successfully) cdn-src.domain.com/file.jpg and returns it to the user.

Does that sound like it might do what you want, without taking too
much effort to keep synchronised?

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list