https://www.52os.net/articles/configure-nginx-using-geoip-allow-whitelist.html 搞了几天没有搞定,这篇文章一下子解决了问题,点赞
记得nginx编译时一定要加载geoip这个模块
https://blog.csdn.net/beyond__devil/article/details/52838422 地区代码表
主配置文件geoip模块的配置如下:
geoip_country /usr/local/nginx/conf/GeoIP/GeoIP.dat;
geoip_city /usr/local/nginx/conf/GeoIP/GeoLiteCity.dat;
geo $remote_addr $ip_whitelist {
default 0;
include ip.conf;
}
map $geoip_city $allow_city {
default no;
Jinhua yes;
#Beijing yes;
Shanghai yes;
Guangzhou yes;
Chongqing yes;
Shandong yes;
}
我的vhost目录下的a.conf配置如下
server {
listen 80;
server_name jiaji.com;
access_log /home/nginx/beijing.log;
location / {
root /var/www/web/ABBEIJING/xiqing;
index index.html index.htm index.php;
if ( $geoip_region = "22" ) { ##这里的22是北京地区
root /var/www/web/ABBEIJING/laohushenhe;
}
if ($allow_city = yes) {
root /var/www/web/ABBEIJING/xiqing;
}
}
}
另一个运用geoip的站点,需求:默认访问/var/www/web/hongb目录,北上广深重庆访问/var/www/web/zhi目录,在允许城市列表(nginx.conf文件里指定的允许城市列表)的城市也是访问/var/www/web/hongb目录
server {
listen 80;
server_name hongb.com;
location / {
root /var/www/web/hongb;
index index.html index.htm index.php;
if ( $geoip_region = "22" ) { ##这里的22是北京地区
root /var/www/web/zhi;
}
if ( $geoip_region = "23" ) { ##这里的23是上海地区
root /var/www/web/jzhi;
}
if ( $geoip_region = "30" ) { ##这里的30是广东地区
root /var/www/web/jzhi;
}
if ( $geoip_region = "32" ) { ##这里的32是四川地区
root /var/www/web/jzhi;
}
if ($allow_city = yes) {
root /var/www/web/hongb;
}
}
}