目 录CONTENT

文章目录

【教程】Nginx如何安装GeoIP模块以及禁止国内外IP访问

XiaoY 小亦
2023-05-18 / 0 评论 / 0 点赞 / 48 阅读 / 5219 字
温馨提示:
本文最后更新于 2023-05-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言

最近配置了一下nginx的ip解析模块 ngx_http_geoip2_module ,接下来我会告诉你们如何安装GeoIP2模块以及怎么禁止国外以及国内ip,以下是需要用到的文件

【Github release】 Releases · leev/ngx_http_geoip2_module (github.com) Release 1.7.1 · maxmind/libmaxminddb (github.com)

配置

安装 libmaxminddb

  1. 下载 libmaxminddb 压缩包
# 我是在/usr里新建了一个专门下载的文件夹dl
cd /usr
mkdir dl&&cd dl
wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz
  1. 解压
tar zxvf libmaxminddb-1.7.1.tar.gz
  1. 安装
cd libmaxminddb-1.7.1
./configure
make install

安装 GeoIP2 模块

  1. 下载文件
#进入你的nginx所在目录,因为我是用宝塔安装的,所以目录不同
cd /www/server/nginx/src
git clone https://github.com/leev/ngx_http_geoip2_module.git

#如果提示 git 命令不存在可以自行安装
#centos
yum install -y git
  1. 编译nginx 这里我是查看网上教程之后自己搞的,不知道对不对,反正最后安装成功了(‾◡◝)
#查看nginx安装的模块
nginx -V

#将上条命令返回出来的文本复制追加在下面的命令后面,注意如果有“ngx_devel_kit”这个的话删掉不要复制在后面,因为我就不行,也不知道啥意思😑
./configure --with-http_stub_status_module \
        --prefix=/www/server/nginx \ #--prefix要看你自己的目录,nginx -V 里面有
        --user=www --group=www \ #这里也是要看你自己的目录,nginx -V
        #这一行加上后面一大堆模块例如--add-module=xxxx
        #然后在最后加上ngx_http_geoip2_module --with-stream

建议将上面的命令写到notepad整理一下,然后再复制到终端里回车,最终返回和以下类似就说明好了

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library
  nginx path prefix: "/user/local/nginx"
  nginx binary file: "/user/local/nginx/sbin/nginx"
  nginx modules path: "/user/local/nginx/modules"
  nginx configuration prefix: "/user/local/nginx/conf"
  nginx configuration file: "/user/local/nginx/conf/nginx.conf"
  nginx pid file: "/user/local/nginx/logs/nginx.pid"
  nginx error log file: "/user/local/nginx/logs/error.log"
  nginx http access log file: "/user/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

然后

make
#搞完之后备份nginx目录
cp -r <你的nginx目录> nginx-backup

#覆盖nginx
rm nginx/sbin/nginx
cp nginx/src/objs/nginx nginx/sbin/nginx

#重启nginx
systemctl restart nginx

下载GeoIP文件

浏览器打开 https://www.maxmind.com/ 注册好账号后点击右上角的 My account 然后在左边点击 Download files

分别找到 GeoLite2 CityGeoLite2 Country,点击右边的 Download GZIP,然后解压,将里面的mmdb文件覆盖到/usr/share/GeoIP

配置nginx.conf

编辑nginx.con文件,加入以下代码

http {
    # 上一层nginx的配置是 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # 在这里想要取到这个值,需要在前面加http_ 且原字段全部变小写,横线变下划线
    map $http_x_forwarded_for $real_ip {
        ~^(\d+\.\d+\.\d+\.\d+) $1;
        default $remote_addr;
    }

    #geoip2的相关配置, 必须放在http节点下
    geoip2 /home/geoip2/GeoLite2-Country.mmdb {
        #自动重新加载数据库文件
        auto_reload 5m;
        # source的意思是根据哪个字段来计算country_code,默认是$remote_ip
        # 因为前面还有一层nginx,所以remote_ip不是想要的真正的ip
        $geoip2_metadata_country_build metadata build_epoch;
        $geoip2_data_country_name source=$real_ip country names en;
        $geoip2_data_country_code source=$real_ip country iso_code;
        $geoip2_data_country_continent source=$arg_ip continent names en;
    }
    server {
        # 对US|HK|KR|JP|CN|RU这几个国家禁止访问
        location / {
	        set $is_allow 0;
	        if ($geoip2_data_country_code ~* "(US|HK|KR|JP|CN|RU)") {
	            set $is_allow 1;
	        }
	        if ($is_allow = 1) {
	            return 403 'deny';
	            break;
	        }
        proxy_pass https://s3;
}
}
  • 禁止国外ip访问
if ($allowed_country = yes) {
	# 这里可以返回百度网页,或者直接返回404
	# return https://www.baidu.com;
	# return /home/japan;
	return 404;
}
完结,撒花 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

参考: nginx install geoip2 module - 掘金 (juejin.cn) 通过Nginx来实现禁止国外IP访问网站 - 知乎 (zhihu.com) nginx使用geoip2,对个别国家限制访问,多层nginx代理解决方法_小洋-的博客-CSDN博客

0

评论区