1、安装依赖
sudo apt-get install libmaxminddb0 libmaxminddb-dev
2、下载模块
git clone https://github.com/leev/ngx_http_geoip2_module.git
3、查看nginx已安装模块
nginx -V
示例
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
4、手动编译 cd /www/server/nginx/src/ 执行以下命令修改配置
./configure --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/www/server/ngx_http_geoip2_module
5、解决编译中luajit模块异常,再执行4的命令
export LUAJIT_INC=/usr/local/include/luajit-2.1
export LUAJIT_LIB=/usr/local/lib
6、第4成功后,开始编译
make && make install
7、重启nginx
8、下载Country.mmdb库到服务器/www/server/Country.mmdb,在nginx中http模块配置
geoip2 /www/server/Country.mmdb {
auto_reload 5m ;
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
default yes;
CN no;
}
9、网站站点service中添加配置
# 匹配国家代码定义为no的,禁止访问,返回403(上面定义中国的 iso_code 为no)
if ( $allowed_country = no ) { return 403; }
宝塔nginx安装geoip2
十二月未完2024-08-08 4:06
相关推荐
木易 士心13 小时前
ESLint 全指南:从原理到实践,构建高质量的 JavaScript/TypeScript 代码成震197117 小时前
UBUNTU 安装虚拟机阿莫西林夹馍19 小时前
ubuntu22.04国内源longxibo21 小时前
【Ubuntu datasophon1.2.1 二开之六:解决CLICKHOUSE安装问题】淡唱暮念1 天前
Linux系统使用夸克网盘CLI上传服务器数据至网盘教程,解决大数据备份苦恼阿莫西林夹馍1 天前
ubuntu安装gitlab悲伤小伞1 天前
Linux_应用层自定义协议与序列化——网络计算器一匹电信狗1 天前
【Linux我做主】从 fopen 到 open:Linux 文件 I/O 的本质与内核视角charlie1145141911 天前
SSH X11 转发排查与解决指南(Windows + Xming + Ubuntu)CS_Zero1 天前
Ubuntu安装Claude Code