【问题标题】:Fallback NGINX location后备 NGINX 位置
【发布时间】:2018-09-26 20:18:40
【问题描述】:

我是 NGINX 的新手,我正在迁移服务器。我还没有完成新服务器上的所有内容,所以我希望它与新服务器匹配,除非该资源或路径不存在。如果是这样,我想将其发送到旧服务器。有没有办法做到这一点?

【问题讨论】:

    标签: nginx nginx-location nginx-reverse-proxy


    【解决方案1】:

    我通过proxy_next_upstream hack 做到了这一点

    定义一个upstream,通过控制权重将大部分reqeust转发到new_server,proxy_next_upstream会重试将失败的请求转发到下一个server(old_server)

    upstream backend {
        server new_server weight=10000;
        server old_server weight=1;
    }
    
    server {
        location / {
            proxy_pass http://backend;
            proxy_next_upstream error timeout http_404 http_500 http_502 http_503 http_504 non_idempotent;
        }
    }
    

    ============

    解决方案二

    server {
        location / {
            proxy_pass http://new_server;
            error_page 404 500 502 503 504 = @fallback;
        }
    
        location @fallback {
            proxy_pass http://old_server;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 2017-06-06
      • 2021-10-05
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2020-07-25
      相关资源
      最近更新 更多