Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

Setup dynamic upstream with nginx

Suppose that i have to upstream source

upstream first {
}

upstream second {
}

And then in server block

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

What I would like to achieve is if $is_china, use a different upstream

proxy_pass http://$preferred_host/;

I can't figure how to do this with nginx.

Upvotes: 3

Views: 909

Answers (2)

G.Pei
G.Pei

Reputation: 101

map might be just sufficient. Have you tried the following?

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

Upvotes: 4

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

Turn out I can use if in nginx

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

location / {
    proxy_pass $preferred_host/;
    ...
}

Upvotes: 1

Related Questions