如何在生产环境中运行的Nginx上安装Geoip2模块?

问题描述 投票:0回答:1

有人可以向我解释如何在生产环境中运行的Nginx 1.14上安装Geoip2模块而不破坏当前配置吗?

我仅找到指示如何在首次安装期间使用geoip2模块编译Nginx的资源。

我正在使用Linux发行版Debian 10。

谢谢

nginx debian geoip
1个回答
1
投票

首先安装libmaxminddb库:

sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

下载和解压缩geoip2模块:

https://github.com/leev/ngx_http_geoip2_module/archive/3.3.tar.gz
tar zxvf 3.3.tar.gz

下载nginx来源:

wget https://nginx.org/download/nginx-1.14.2.tar.gz
tar zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2

然后将geoip2构建为动态模块:

./configure --with-compat --add-dynamic-module=/path/to/ngx_http_geoip2_module
make
make modules

这将产生objs/ngx_http_geoip2_module.so。如果需要,可以将其手动复制到您的nginx模块路径。

例如:

cp objs/ngx_http_geoip2_module.so /etc/nginx/modules

从(需要免费注册)获得最新数据库:

https://dev.maxmind.com/geoip/geoip2/geolite2/#Download_Access

Unpack dtabase files to /usr/share/GeoIP2 directory

将以下行添加到您的nginx.conf:

load_module modules/ngx_http_geoip2_module.so;

添加到nginx.conf http部分:

geoip2 /usr/share/GeoIP2/GeoLite2-Country.mmdb {
    auto_reload 60m;
    $geoip2_metadata_country_build metadata build_epoch;
    $geoip2_data_country_code default=US source=$variable_with_ip country iso_code;
    $geoip2_data_country_name country names en;
}
geoip2 /usr/share/GeoIP2/GeoLite2-City.mmdb {
    auto_reload 60m;
    $geoip2_metadata_city_build metadata build_epoch;
    $geoip2_data_city_name default=London city names en;
}

然后重新启动Nginx服务。希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.