Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 460
Tuan Anh Tran Avatar asked Jun 23 '26 22:06

Tuan Anh Tran


2 Answers

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

map $geoip_country_code $preferred_host {
    default first;
    CN      second;
}
like image 56
G.Pei Avatar answered Jun 26 '26 19:06

G.Pei


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/;
    ...
}
like image 27
Tuan Anh Tran Avatar answered Jun 26 '26 17:06

Tuan Anh Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!