简体   繁体   English

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

[英]How install the Geoip2 module on a Nginx running in a production environment?

Can someone explain to me how to install the Geoip2 module on a Nginx 1.14 running in a production environment without breaking the current configuration?有人可以向我解释如何在生产环境中运行的 Nginx 1.14 上安装 Geoip2 模块而不破坏当前配置吗?

I only find sources indicating how to compile Nginx with the geoip2 module during a first installation.我只找到指示如何在第一次安装期间使用 geoip2 模块编译 Nginx 的资源。

I am using the Linux distribution Debian 10.我正在使用 Linux 分布 Debian 10。

Thank you谢谢

First install libmaxminddb libs:首先安装 libmaxminddb 库:

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

Download and unpack geoip2 module:下载并解压 geoip2 模块:

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

Download nginx source:下载 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

Then build geoip2 as a dynamic module:然后将 geoip2 构建为动态模块:

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

This will produce objs/ngx_http_geoip2_module.so .这将产生objs/ngx_http_geoip2_module.so It can be copied to your nginx module path manually if you wish.如果您愿意,可以手动将其复制到您的 nginx 模块路径。

For example:例如:

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

Obtain latest databases from (need a free registration):获取最新数据库(需要免费注册):

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

Unpack dtabase files to /usr/share/GeoIP2 directory

Add the following line to your nginx.conf:将以下行添加到您的 nginx.conf 中:

load_module modules/ngx_http_geoip2_module.so;

Add to the nginx.conf http section:添加到 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;
}

Then restart Nginx service.然后重启 Nginx 服务。 Hope that will help you.希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM