代码之家  ›  专栏  ›  技术社区  ›  Tuan Anh Tran

使用nginx设置动态上游

  •  3
  • Tuan Anh Tran  · 技术社区  · 8 年前

    假设我必须向上游源

    upstream first {
    }
    
    upstream second {
    }
    

    然后在 server

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

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

    proxy_pass http://$preferred_host/;
    

    我不知道如何使用nginx来实现这一点。

    2 回复  |  直到 8 年前
        1
  •  4
  •   G.Pei    8 年前

    map 可能就足够了。你试过以下吗?

    map $geoip_country_code $preferred_host {
        default first;
        CN      second;
    }
    
        2
  •  1
  •   Tuan Anh Tran    8 年前

    结果我可以用 if 在nginx中

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