首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx上游代理的回退

nginx上游代理的回退
EN

Stack Overflow用户
提问于 2012-11-14 22:11:28
回答 3查看 8.2K关注 0票数 8

我在镜像服务器集群前面有一个nginx实例:

代码语言:javascript
复制
  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;
    }
  }

这个集群正在被一个即将上线的新集群所取代,在一段时间内,我想要为来自旧集群的映像提供服务,但如果映像是新的或者已经从旧集群迁移到新集群,则退回到新集群。迁移完成后,我可以返回到原始设置。

所以我想我可以

代码语言:javascript
复制
  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写入任何内容。

我很确定我不能使用try_files,因为,嗯,没有任何本地文件,所有东西都是代理的。

以前有没有人做过这样的事情?我遗漏了什么?

EN

回答 3

Stack Overflow用户

发布于 2012-11-14 22:31:30

我真傻。所需要的只是第一个位置的proxy_intercept_errors on;

票数 13
EN

Stack Overflow用户

发布于 2020-09-25 16:25:14

我遇到过类似的情况,但如果我的第一台服务器停机(状态) 502,我想退回到另一台服务器上。我在不需要proxy_intercept_errors`的情况下让它工作(nginx/1.17.8)。

此外,对于任何使用带有URI的proxy_pass (本例中为/)的用户,您需要使用稍微不同的配置,否则将得到错误消息(见下文)。

代码语言:javascript
复制
location /texts/ {
  proxy_pass http://127.0.0.1:8084/;
  proxy_set_header X-Forwarded-For $remote_addr;
  error_page 502 = @fallback;
}

location @fallback {
  # This will pass failed requests to /texts/foo above to
  #  http://someotherserver:8080/texts/foo
  proxy_pass http://someotherserver:8080$request_uri;
}

无论出于什么原因,Nginx都不允许在这里使用斜杠:

代码语言:javascript
复制
location @fallback {
  proxy_pass http://someotherserver:8080/;
}

并将抛出:

nginx: emerg "proxy_pass“不能在正则表达式给定的位置、命名位置内、"if”语句内或中的"limit_except“块内具有URI部分

不幸的是,如果没有/,你就不能(AFAIK) proxy_pass一个子目录。带有$1的No RegEx也不能工作,因为它不支持空格。

票数 3
EN

Stack Overflow用户

发布于 2021-12-09 11:56:37

这应该是可行的

代码语言:javascript
复制
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;
    proxy_intercept_errors on;
    error_page 404 = @fallback-2;
}

location @fallback-2 {
    access_log /var/log/nginx/static-2.access.log;
    proxy_pass http://img-farm-2;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13380439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档