Установка geoip2 Nginx

D
На сайте с 28.06.2008
Offline
1101
4333

Решил попробовать установить geoip2 в Nginx.

Начал по шаблону, по которому пересобирал Nginx буквально неделю назад. Все папки с Nginx, бротли, openssl лежат в /opt еще с прошлого раза.

sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y libpcre3-dev zlib1g-dev libssl-dev libxslt1-dev
sudo apt-get install -y libmaxminddb-dev

cd /opt/ngx_brotli && git submodule update --init && cd /opt/nginx-1.15.3
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-pcre-jit --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --with-http_xslt_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed' --with-openssl=/opt/openssl-1.1.1a --with-openssl-opt='enable-tls1_3' --add-dynamic-module=/opt/ngx_http_geoip2_module-master/ --add-module=/opt/ngx_brotli

make
make install

./configure проходит нормально и завершается стандартно без ошибок. Далее делаю make и получаю ошибку. Что не так?

root@test /opt/nginx-1.15.3 # make
make -f objs/Makefile
make[1]: Entering directory '/opt/nginx-1.15.3'
cd /opt/openssl-1.1.1a \
&& if [ -f Makefile ]; then make clean; fi \
&& ./config --prefix=/opt/openssl-1.1.1a/.openssl no-shared no-threads enable-tls1_3 \
&& make \
&& make install_sw LIBDIR=lib
/bin/sh: 1: cd: can't cd to /opt/openssl-1.1.1a
objs/Makefile:2087: recipe for target '/opt/openssl-1.1.1a/.openssl/include/openssl/ssl.h' failed
make[1]: *** [/opt/openssl-1.1.1a/.openssl/include/openssl/ssl.h] Error 2
make[1]: Leaving directory '/opt/nginx-1.15.3'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2

ВСЕ ПОНЯЛ

папки /opt/openssl-1.1.1a нет

LEOnidUKG
На сайте с 25.11.2006
Online
1722
#1

https://nginx.org/ru/docs/http/ngx_http_geoip_module.html

Для сборки и работы этого модуля нужна библиотека MaxMind GeoIP.



---------- Добавлено 08.01.2019 в 14:44 ----------

С другой стороны нафига городить и пересобирать, когда nginx поддерживает динамические модуля:

https://docs.nginx.com/nginx/admin-guide/dynamic-modules/geoip2/

Ставим стандартно из репозитария и подключаем нужные модуля.

✅ Мой Телеграм канал по SEO, оптимизации сайтов и серверов: https://t.me/leonidukgLIVE ✅ Качественное и рабочее размещение SEO статей СНГ и Бурж: https://getmanylinks.ru/
D
На сайте с 28.06.2008
Offline
1101
#2

Я нашел эту команду, но что-то ошибка какая-то вылетела.

В общем пересобрал, все работает. Но так как инфы на русском в сети мало, выложу сюда конфиг, чтобы русскоязычным удобно было.

В конфиг Nginx в самом начале нужно прописать:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
load_module "modules/ngx_http_geoip2_module.so";
......

Далее в секции http {

http {
.........

geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_data_city_name city names en;
$geoip2_data_postal_code postal code;
$geoip2_data_latitude location latitude;
$geoip2_data_longitude location longitude;
$geoip2_data_state_name subdivisions 0 names en;
$geoip2_data_state_code subdivisions 0 iso_code;
}

geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
$geoip2_data_country_code default=US country iso_code;
$geoip2_data_city_name city names en;
$geoip2_data_country_name country names en;
$geoip2_data_state_code subdivisions 0 iso_code;
}

..............

В конфиг нужного сайта в секцию

location ~ \.php$ {
...........
fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
fastcgi_param CITY_NAME $geoip2_data_city_name;
fastcgi_param STATE_CODE $geoip2_data_state_code;

Для тестирования создайте пхп файл

<?php
$geoip2_data_city_name = getenv(CITY_NAME);
echo $geoip2_data_city_name;
?>
<br>
<?php
$geoip2_data_country_code = getenv(COUNTRY_CODE);
echo $geoip2_data_country_code;
?>
<br>
<?php
$geoip2_data_country_name = getenv(COUNTRY_NAME);
echo $geoip2_data_country_name;
?>
<br>
<?php
$geoip2_data_state_code = getenv(STATE_CODE);
echo $geoip2_data_state_code;
?>

Для Москвы я получил:

Moscow
RU
Russia
MOW

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий