Trying to configure an origin pull CDN-like reverse proxy

Francis Daly francis at daoine.org
Sat Nov 3 09:44:36 UTC 2012


On Sat, Nov 03, 2012 at 12:16:46AM -0400, cachito wrote:

Hi there,

All untested by me, but...

> I thought it would be easy to use the proxy_* features, but I'm hitting a
> wall and I can't find an applicable tutorial/article anywhere. Would
> somebody have any advice on how to do this? This is the basic behavior I'm
> after:
> - Client requests static file cdn.blog.com/dir/photo.jpg
> - cdn.blog.com looks for the file in its cache
> - If the cache has it, check original or revalidate according with original
> headers (this is internal, I know).
> - If the cache doesn't have it, request it from www.blog.com/dir/photo.jpg,
> cache it and serve it.
> - Preferably, allow for this to be done for many sites/domains, acting as a
> CDN server for many sites.

So far, it looks like a straightforward caching reverse proxy setup. I'm
not quite sure what the last point means -- but one server{} block per
site should work.

> upstream backend_cdn.blog.com {
> ip_hash;
> server 333.333.333.333;
> }
> 
> server {
> listen 80;
> server_name cdn.blog.com;
> access_log off;
> # Set proxy headers for the passthrough
> proxy_set_header Host $host;

$host here is probably "cdn.blog.com".

What happens if you change this to "proxy_set_header Host www.blog.com;" ?

> location ~* \.(css|js|html|htm)$ {
> expires 7d;
> log_not_found off;
> proxy_pass http://backend_$host;
> proxy_cache scripts;
> proxy_cache_key $cache_key;
> }

> With this configuration, whenever I call a static file such as
> http://cdn.blog.com/wp-includes/js/prototype.js I end up being redirected to
> http://www.blog.com/wp-includes/js/prototype.js. I've tried many things,
> like setting the Host header to various values or adding $uri to the end of
> the proxy_pass directives, to no avail. One thing to notice is that the
> 333.333.333.333 server only responds to www.blog.com, not cdn.blog.com.

What is the output of

  curl -i -0 -H 'Host: cdn.blog.com' http://333.333.333.333/wp-includes/js/prototype.js

? That is approximately what nginx will do. (You can add the extra
proxy_set_header headers there, if you think it will make a difference.)

My guess is that the 333.333.333.333 server returns the http redirect,
and nginx is correct in passing that on to the client.

The nginx log files should show more details.

> Do I need a root directive in server1.conf?

If you read from the filesystem, or otherwise access $document_root,
then the root directive is used.

I don't see that needed for this request.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list