【问题标题】:Setup dynamic upstream with nginx使用 nginx 设置动态上游
【发布时间】:2016-08-11 03:52:04
【问题描述】:

假设我必须上游源

upstream first {
}

upstream second {
}

然后在server块中

map $geoip_country_code $is_china {
    default no;
    CN yes;
}

我想要实现的是如果$is_china,使用不同的上游

proxy_pass http://$preferred_host/;

我不知道如何用 nginx 做到这一点。

【问题讨论】:

    标签: nginx load-balancing geo


    【解决方案1】:

    map 可能就足够了。您是否尝试过以下方法?

    map $geoip_country_code $preferred_host {
        default first;
        CN      second;
    }
    

    【讨论】:

    • 谢谢。我也刚刚了解了map。我会试试这个,因为它看起来比上面的更干净。
    • np。 map 的一个好处是您可以轻松添加更多键值对。
    【解决方案2】:

    原来我可以在 nginx 中使用if

    set $preferred_host http://first;
    if ($is_china) {
        set $preferred_host http://second;
    }
    
    location / {
        proxy_pass $preferred_host/;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 2017-12-27
      • 2017-07-12
      • 2016-12-10
      • 1970-01-01
      相关资源
      最近更新 更多