Nginx可以通过GeoIP模块来实现基于地理位置的访问控制,允许您根据访问者的IP地址所在国家或地区来允许或拒绝访问。以下是完整的实现步骤:
# 安装GeoIP数据库和模块
sudo apt-get install libgeoip1 geoip-database libnginx-mod-http-geoip # Debian/Ubuntu
sudo yum install geoip geoip-devel nginx-module-geoip # CentOS/RHEL
需要添加--with-http_geoip_module
参数重新编译Nginx。
# 下载国家数据库
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
# 下载城市数据库(可选)
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
在nginx.conf的http块中添加以下配置:
http {
# 加载GeoIP数据库
geoip_country /path/to/GeoIP.dat;
geoip_city /path/to/GeoLiteCity.dat; # 可选
# 创建变量映射国家代码
map $geoip_country_code $allowed_country {
default no;
US yes; # 允许美国
CA yes; # 允许加拿大
GB yes; # 允许英国
# 添加更多允许的国家代码...
}
}
server {
listen 80;
server_name example.com;
# 基于国家代码的访问控制
if ($allowed_country = no) {
return 403; # 拒绝访问
# 或者重定向到其他页面
# return 301 https://example.com/access-denied;
}
# 其他配置...
}
geoip_city /path/to/GeoLiteCity.dat;
location / {
if ($geoip_city = "New York") {
return 403;
}
# 其他配置...
}
location /us-only-content {
if ($geoip_country_code != US) {
return 404;
}
# 其他配置...
}
log_format geo_log '$remote_addr - $geoip_country_name - $geoip_city - "$request"';
access_log /var/log/nginx/geo_access.log geo_log;
GeoIP数据库需要定期更新以保持准确性:
# 创建更新脚本 /usr/local/bin/update_geoip.sh
#!/bin/bash
wget -O /tmp/GeoIP.dat.gz http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip -c /tmp/GeoIP.dat.gz > /path/to/GeoIP.dat
rm /tmp/GeoIP.dat.gz
service nginx reload
# 设置定时任务
crontab -e
# 添加每月1号更新
0 0 1 * * /usr/local/bin/update_geoip.sh
如果需要更复杂的地理位置逻辑,可以考虑使用OpenResty(Nginx+Lua):
http {
lua_shared_dict geoip 10m;
init_by_lua_block {
local geo = require "resty.maxminddb"
geo.init("/path/to/GeoLite2-City.mmdb")
}
server {
location / {
access_by_lua_block {
local geo = require "resty.maxminddb"
local res, err = geo.lookup(ngx.var.remote_addr)
if res and res.country and res.country.iso_code == "CN" then
ngx.exit(403)
end
}
}
}
}
通过以上配置,您可以有效地实现基于地理位置的访问控制,保护您的网站内容或服务。