getIp2.txt 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 1、宝塔编译安装nginx
  2. 2、终端运行
  3. 安装IP数据库支持
  4. yum install libmaxminddb-devel -y
  5. 下载Nginx扩展
  6. git clone https://github.com/leev/ngx_http_geoip2_module.git
  7. 或(此库包含国家数据IP库)
  8. git clone https://always77.xyz/always/geoIp2.git
  9. 修改nginx安装文件或用编辑工作打开
  10. vi /www/server/panel/install/nginx.sh
  11. 搜索:./configure 找到编译安装启动位置
  12. 插入:--add-module=/root/ngx_http_geoip2_module
  13. 3、重新执行编译安装(版本号需跟初始安装一致)
  14. sh /www/server/panel/install/nginx.sh install 1.22
  15. 4、登陆宝塔修改nginx配置
  16. 在http节点下增加配置项
  17. # 引入IP数据库
  18. geoip2 /root/ngx_http_geoip2_module/Country.mmdb {
  19. $geoip2_data_country_code country iso_code;
  20. }
  21. # 映射 默认值yes 中国禁止(其它根据情况配置)
  22. map $geoip2_data_country_code $allowed_country {
  23. default yes;
  24. CN no;
  25. }
  26. 5、修改站点配置
  27. 在server节点下
  28. if ($allowed_country = no){
  29. return 403;
  30. }
  31. 注:ngx_http_geoip2_module为扩展目录名。路径配置必需要准确