nginx 上游代理的后备 [英] fallback for an nginx upstream proxy

查看:45
本文介绍了nginx 上游代理的后备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一组图像服务器前面有一个 nginx 实例:

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.

所以我觉得我可以做到

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

但这行不通.我在 static.access.log 中看到 404,但是 error_page 404 指令没有被执行,因为没有任何东西被写入 static-2.access.log.

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.

我很确定我不能使用 try_files 因为,呃,没有任何本地文件,一切都是代理.

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?

推荐答案

我傻了.所需要的只是 proxy_intercept_errors on; 在第一个位置

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

这篇关于nginx 上游代理的后备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆