diff --git a/Dockerfile b/Dockerfile index daa61d1..efda7e6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ ENV php_vars /usr/local/etc/php/conf.d/docker-vars.ini ENV NGINX_VERSION 1.16.1 ENV LUA_MODULE_VERSION 0.10.14 ENV DEVEL_KIT_MODULE_VERSION 0.3.0 +ENV GEOIP2_MODULE_VERSION 3.2 ENV LUAJIT_LIB=/usr/lib ENV LUAJIT_INC=/usr/include/luajit-2.1 @@ -64,6 +65,7 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \ --with-http_v2_module \ --add-module=/usr/src/ngx_devel_kit-$DEVEL_KIT_MODULE_VERSION \ --add-module=/usr/src/lua-nginx-module-$LUA_MODULE_VERSION \ + --add-module=/usr/src/ngx_http_geoip2_module-$GEOIP2_MODULE_VERSION \ " \ && addgroup -S nginx \ && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \ @@ -81,12 +83,14 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \ libxslt-dev \ gd-dev \ geoip-dev \ + libmaxminddb-dev \ perl-dev \ luajit-dev \ && curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \ && curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc -o nginx.tar.gz.asc \ && curl -fSL https://github.com/simpl/ngx_devel_kit/archive/v$DEVEL_KIT_MODULE_VERSION.tar.gz -o ndk.tar.gz \ && curl -fSL https://github.com/openresty/lua-nginx-module/archive/v$LUA_MODULE_VERSION.tar.gz -o lua.tar.gz \ + && curl -fSL https://github.com/leev/ngx_http_geoip2_module/archive/$GEOIP2_MODULE_VERSION.tar.gz -o ngx_http_geoip2_module.tar.gz \ && export GNUPGHOME="$(mktemp -d)" \ && found=''; \ for server in \ @@ -105,7 +109,8 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \ && tar -zxC /usr/src -f nginx.tar.gz \ && tar -zxC /usr/src -f ndk.tar.gz \ && tar -zxC /usr/src -f lua.tar.gz \ - && rm nginx.tar.gz ndk.tar.gz lua.tar.gz \ + && tar -zxC /usr/src -f ngx_http_geoip2_module.tar.gz \ + && rm nginx.tar.gz ndk.tar.gz lua.tar.gz ngx_http_geoip2_module.tar.gz \ && cd /usr/src/nginx-$NGINX_VERSION \ && ./configure $CONFIG --with-debug \ && make -j$(getconf _NPROCESSORS_ONLN) \ @@ -133,6 +138,7 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \ && strip /usr/sbin/nginx* \ && strip /usr/lib/nginx/modules/*.so \ && rm -rf /usr/src/nginx-$NGINX_VERSION \ + && rm -rf /usr/src/ngx_http_geoip2_module-$GEOIP2_MODULE_VERSION \ \ # Bring in gettext so we can get `envsubst`, then throw # the rest away. To do this, we need to install `gettext` @@ -235,6 +241,12 @@ ADD conf/nginx-site.conf /etc/nginx/sites-available/default.conf ADD conf/nginx-site-ssl.conf /etc/nginx/sites-available/default-ssl.conf RUN ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf +# Add GeoLite2 databases (https://dev.maxmind.com/geoip/geoip2/geolite2/) +RUN curl -fSL http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz -o /etc/nginx/GeoLite2-City.mmdb.gz \ + && curl -fSL http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz -o /etc/nginx/GeoLite2-Country.mmdb.gz \ + && gunzip /etc/nginx/GeoLite2-City.mmdb.gz \ + && gunzip /etc/nginx/GeoLite2-Country.mmdb.gz + # tweak php-fpm config RUN echo "cgi.fix_pathinfo=0" > ${php_vars} &&\ echo "upload_max_filesize = 100M" >> ${php_vars} &&\ diff --git a/conf/nginx-site-ssl.conf b/conf/nginx-site-ssl.conf index b022445..13c286f 100644 --- a/conf/nginx-site-ssl.conf +++ b/conf/nginx-site-ssl.conf @@ -63,6 +63,16 @@ server { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; + fastcgi_param GEOIP2_LONGITUDE $geoip2_data_longitude; + fastcgi_param GEOIP2_LATITUDE $geoip2_data_latitude; + fastcgi_param GEOIP2_CONTINENT_CODE $geoip2_data_continent_code; + fastcgi_param GEOIP2_CONTINENT_NAME $geoip2_data_continent_name; + fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_data_country_code; + fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_data_country_name; + fastcgi_param GEOIP2_STATE_CODE $geoip2_data_state_code; + fastcgi_param GEOIP2_STATE_NAME $geoip2_data_state_name; + fastcgi_param GEOIP2_CITY_NAME $geoip2_data_city_name; + fastcgi_param GEOIP2_POSTAL_CODE $geoip2_data_postal_code; include fastcgi_params; } diff --git a/conf/nginx-site.conf b/conf/nginx-site.conf index 3c38946..728ba5b 100644 --- a/conf/nginx-site.conf +++ b/conf/nginx-site.conf @@ -59,6 +59,16 @@ server { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; + fastcgi_param GEOIP2_LONGITUDE $geoip2_data_longitude; + fastcgi_param GEOIP2_LATITUDE $geoip2_data_latitude; + fastcgi_param GEOIP2_CONTINENT_CODE $geoip2_data_continent_code; + fastcgi_param GEOIP2_CONTINENT_NAME $geoip2_data_continent_name; + fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_data_country_code; + fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_data_country_name; + fastcgi_param GEOIP2_STATE_CODE $geoip2_data_state_code; + fastcgi_param GEOIP2_STATE_NAME $geoip2_data_state_name; + fastcgi_param GEOIP2_CITY_NAME $geoip2_data_city_name; + fastcgi_param GEOIP2_POSTAL_CODE $geoip2_data_postal_code; include fastcgi_params; } diff --git a/conf/nginx.conf b/conf/nginx.conf index bcda82a..2df8821 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -33,6 +33,38 @@ http { server_tokens off; #gzip on; + geoip2 /etc/nginx/GeoLite2-Country.mmdb { + auto_reload 1h; + + $geoip2_metadata_country_build metadata build_epoch; + + # populate the country + $geoip2_data_country_code source=$remote_addr country iso_code; + $geoip2_data_country_name source=$remote_addr country names en; + + # populate the continent + $geoip2_data_continent_code source=$remote_addr continent code; + $geoip2_data_continent_name source=$remote_addr continent names en; + } + + geoip2 /etc/nginx/GeoLite2-City.mmdb { + auto_reload 1h; + + # City name itself + $geoip2_data_city_name source=$remote_addr city names en; + + # Postal code will be an approximation, probably the first one in the list that covers an area + $geoip2_data_postal_code source=$remote_addr postal code; + + # State in code and long form + $geoip2_data_state_code source=$remote_addr subdivisions 0 iso_code; + $geoip2_data_state_name source=$remote_addr subdivisions 0 names en; + + # Lat and Lng + $geoip2_data_latitude source=$remote_addr location latitude; + $geoip2_data_longitude source=$remote_addr location longitude; + } + include /etc/nginx/sites-enabled/*; } #daemon off;