登陆
方案 | 优点 | 缺点 |
DNS 智能解析 | 实现简单,配置完成后无额外服务器负载 | 依赖 DNS 服务商支持,部分功能可能收费 |
Nginx 反向代理 | 完全自主控制,灵活定制规则 | 增加服务器负载,配置相对复杂 |
nslookup yourdomain.com
# Ubuntu/Debian系统apt-get install libmaxminddb0 libmaxminddb-dev mmdb-bin# CentOS系统yum install libmaxminddb-devel -y
# 进入root目录cd /root# 克隆模块git clone https://github.com/leev/ngx_http_geoip2_module.git
# 进入数据库目录cd /usr/local/share/GeoIP# 下载国家数据库wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz# 解压数据库gunzip GeoLite2-Country.mmdb.gz
geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb { $geoip2_country_code country iso_code;}
server { listen 80; server_name yourdomain.com; # 根据用户IP地址判断所在国家 geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb { $geoip2_country_code country iso_code; } # 配置国内服务器地址 set $backend_url "http://国内服务器IP:端口"; # 如果是国外用户,将请求转发到国外服务器 if ($geoip2_country_code != "CN") { set $backend_url "http://国外服务器IP:端口"; } location / { proxy_pass $backend_url; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
server { listen 80; server_name yourdomain.com; geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb { $geoip2_country_code country iso_code; } # 配置国外服务器地址 set $backend_url "http://国外服务器IP:端口"; # 如果是国内用户,将请求转发到国内服务器 if ($geoip2_country_code == "CN") { set $backend_url "http://国内服务器IP:端口"; } location / { proxy_pass $backend_url; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
http { # 定义国内服务器组 upstream domestic_servers { server 国内服务器IP1:端口; server 国内服务器IP2:端口; } # 定义国外服务器组 upstream overseas_servers { server 国外服务器IP1:端口; server 国外服务器IP2:端口; } server { listen 80; server_name yourdomain.com; geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb { $geoip2_country_code country iso_code; } location / { if ($geoip2_country_code == "CN") { proxy_pass http://domestic_servers; } else { proxy_pass http://overseas_servers; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }}
// 获取用户绑定的服务器信息$user_server = getUserServerBinding($user_id);// 根据服务器类型重定向if ($user_server == 'overseas') { header("Location: https://overseas.yourdomain.com");} else { header("Location: https://domestic.yourdomain.com");}exit;
service nginx restart