Skip to content

Commit addfa1a

Browse files
committed
config: expose GeoLite2 data via env vars
Adds GeoLite2 databases and configures nginx to expose the data via GEOIP2_ namespaced environment variables.
1 parent 2e11991 commit addfa1a

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ ADD conf/nginx-site.conf /etc/nginx/sites-available/default.conf
242242
ADD conf/nginx-site-ssl.conf /etc/nginx/sites-available/default-ssl.conf
243243
RUN ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf
244244

245+
# Add GeoLite2 databases (https://dev.maxmind.com/geoip/geoip2/geolite2/)
246+
RUN curl -fSL http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz -o /etc/nginx/GeoLite2-City.mmdb.gz \
247+
&& curl -fSL http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz -o /etc/nginx/GeoLite2-Country.mmdb.gz \
248+
&& gunzip /etc/nginx/GeoLite2-City.mmdb.gz \
249+
&& gunzip /etc/nginx/GeoLite2-Country.mmdb.gz
250+
245251
# tweak php-fpm config
246252
RUN echo "cgi.fix_pathinfo=0" > ${php_vars} &&\
247253
echo "upload_max_filesize = 100M" >> ${php_vars} &&\

conf/nginx-site-ssl.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ server {
6363
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
6464
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
6565
fastcgi_index index.php;
66+
fastcgi_param GEOIP2_LONGITUDE $geoip2_data_longitude;
67+
fastcgi_param GEOIP2_LATITUDE $geoip2_data_latitude;
68+
fastcgi_param GEOIP2_CONTINENT_CODE $geoip2_data_continent_code;
69+
fastcgi_param GEOIP2_CONTINENT_NAME $geoip2_data_continent_name;
70+
fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_data_country_code;
71+
fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_data_country_name;
72+
fastcgi_param GEOIP2_STATE_CODE $geoip2_data_state_code;
73+
fastcgi_param GEOIP2_STATE_NAME $geoip2_data_state_name;
74+
fastcgi_param GEOIP2_CITY_NAME $geoip2_data_city_name;
75+
fastcgi_param GEOIP2_POSTAL_CODE $geoip2_data_postal_code;
6676
include fastcgi_params;
6777
}
6878

conf/nginx-site.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ server {
5959
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
6060
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
6161
fastcgi_index index.php;
62+
fastcgi_param GEOIP2_LONGITUDE $geoip2_data_longitude;
63+
fastcgi_param GEOIP2_LATITUDE $geoip2_data_latitude;
64+
fastcgi_param GEOIP2_CONTINENT_CODE $geoip2_data_continent_code;
65+
fastcgi_param GEOIP2_CONTINENT_NAME $geoip2_data_continent_name;
66+
fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_data_country_code;
67+
fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_data_country_name;
68+
fastcgi_param GEOIP2_STATE_CODE $geoip2_data_state_code;
69+
fastcgi_param GEOIP2_STATE_NAME $geoip2_data_state_name;
70+
fastcgi_param GEOIP2_CITY_NAME $geoip2_data_city_name;
71+
fastcgi_param GEOIP2_POSTAL_CODE $geoip2_data_postal_code;
6272
include fastcgi_params;
6373
}
6474

conf/nginx.conf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ http {
3333
server_tokens off;
3434
#gzip on;
3535

36+
geoip2 /etc/nginx/GeoLite2-Country.mmdb {
37+
auto_reload 1h;
38+
39+
$geoip2_metadata_country_build metadata build_epoch;
40+
41+
# populate the country
42+
$geoip2_data_country_code source=$remote_addr country iso_code;
43+
$geoip2_data_country_name source=$remote_addr country names en;
44+
45+
# populate the continent
46+
$geoip2_data_continent_code source=$remote_addr continent code;
47+
$geoip2_data_continent_name source=$remote_addr continent names en;
48+
}
49+
50+
geoip2 /etc/nginx/GeoLite2-City.mmdb {
51+
auto_reload 1h;
52+
53+
# City name itself
54+
$geoip2_data_city_name source=$remote_addr city names en;
55+
56+
# Postal code will be an approximation, probably the first one in the list that covers an area
57+
$geoip2_data_postal_code source=$remote_addr postal code;
58+
59+
# State in code and long form
60+
$geoip2_data_state_code source=$remote_addr subdivisions 0 iso_code;
61+
$geoip2_data_state_name source=$remote_addr subdivisions 0 names en;
62+
63+
# Lat and Lng
64+
$geoip2_data_latitude source=$remote_addr location latitude;
65+
$geoip2_data_longitude source=$remote_addr location longitude;
66+
}
67+
3668
include /etc/nginx/sites-enabled/*;
3769
}
3870
#daemon off;

0 commit comments

Comments
 (0)