fallback for an nginx upstream proxy

dland picture dland · Nov 14, 2012 · Viewed 7.1k times · Source

I have an nginx instance in front of a cluster of image servers:

  upstream img-farm-1  {
    server 10.0.1.1;
    server 10.0.1.2;
    server 10.0.1.3;
    server 10.0.1.4;
    # etc
  }

  location ~ ^/static: {
    rewrite /static:(.*) /$1 break;
    proxy_pass http://img-farm-1;
    limit_except GET {
        allow all;
    }
  }

This cluster is being superceded by a new cluster that is coming on line, and for a while, I want to serve the image from the old cluster, but fallback on the new cluster if the image is new or has been migrated from the old to the new. Once the migration is complete I can go back to the original setup.

So I thought I could do

  upstream img-farm-2  {
    server 10.0.2.1;
    server 10.0.2.2;
    server 10.0.2.3;
    server 10.0.2.4;
    server 10.0.2.5;
    # etc
  }

  location ~ ^/static: {
    access_log /var/log/nginx/static.access.log;
    rewrite    /static:(.*) /$1 break;
    proxy_pass http://img-farm-1;
    error_page 404 = @fallback-2;
  }

  location @fallback-2 {
    access_log /var/log/nginx/static-2.access.log;
    proxy_pass http://img-farm-2;
  }

But this doesn't work. I'm seeing 404s in static.access.log but the error_page 404 directive is not being acted upon, insofar as there's nothing at all being written to static-2.access.log.

I'm pretty sure I can't use try_files because, well duh, there aren't any local files, everything is proxified.

Has anyone done something like this before? What am I missing?

Answer

dland picture dland · Nov 14, 2012

Silly me. All that was needed was proxy_intercept_errors on; in the first location