Skip to content

Commit

Permalink
fix geoip
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Sep 27, 2022
1 parent fbc97bd commit 5b3be40
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
10 changes: 6 additions & 4 deletions files/etc/nginx/cdn-ips.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Local cache/proxy
set_real_ip_from 0.0.0.0/0;
set_real_ip_from ::/0;
set_real_ip_from 127.0.0.1/32;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 0.0.0.0/0;
set_real_ip_from ::/0;
set_real_ip_from 127.0.0.1/32;
set_real_ip_from 192.168.0.0/16;
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.0.0.0/24;

#real_ip_header X-Real-IP;
#real_ip_header X-Forwarded-For;
Expand Down
35 changes: 9 additions & 26 deletions files/etc/nginx/geoipme.conf
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
# usage: include /etc/nginx/geoipme.conf;
# this proxy pass to sites-enabled/geoip.conf
location ~ /(geoip|geoipme)+/(?<ip>.*) {
charset utf-8;
add_header Cache-Control no-cache;
set $realip $ip;
set $realip $ip;

if ($ip = '') {
set $realip $realip_remote_addr;
if ($realip = '') {
set $realip $realip_remote_addr;
}

default_type application/json;
set $mybody '{"ip":"$realip","country_code":"$geoip2_country_code","region_code":"$geoip2_region_code","region_name":"$geoip2_region","city":"$geoip2_city","zip_code":"$geoip2_postal_code","latitude":"$geoip2_latitude","longitude":"$geoip2_longitude","metro_code":"$geoip2_dma_code"}';

if ($arg_format = "xml") {
add_header Content-Type application/xml;

set $mybody '<Response><IP>$realip</IP><CountryCode>$geoip2_country_code</CountryCode><CountryName>$geoip2_country</CountryName><RegionCode>$geoip2_region_code</RegionCode><RegionName>$geoip2_region</RegionName><City>$geoip2_city</City><ZipCode>$geoip2_postal_code</ZipCode><Latitude>$geoip2_latitude</Latitude><Longitude>$geoip2_longitude</Longitude><MetroCode>$geoip2_dma_code</MetroCode></Response>';
}

if ($arg_format = "csv") {
add_header Content-Type text/csv;
set $mybody '$realip,$geoip2_country_code,$geoip2_region_code,$geoip2_region,$geoip2_city,$geoip2_postal_code,$geoip2_latitude,$geoip2_longitude,$geoip2_dma_code';
}

if ($arg_callback) {
add_header Content-Type application/javascript;
return 200 '$arg_callback($mybody);';
}

return 200 $mybody;
}
proxy_set_header X-Forwarded-For $realip;
proxy_set_header Host "127.0.0.1";
proxy_pass "http://127.0.0.1:9081";
}
35 changes: 20 additions & 15 deletions files/etc/nginx/geolite2.conf
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
map $http_x_forwarded_for $realip {
default $http_x_forwarded_for;
'' $realip;
}

geoip2 /etc/nginx/GeoLite2-Country.mmdb {
auto_reload 60m;

$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country source=$realip_remote_addr country names en;
$geoip2_data_country_code source=$realip_remote_addr country iso_code;
$geoip2_data_country source=$realip country names en;
$geoip2_data_country_code source=$realip country iso_code;
}

geoip2 /etc/nginx/GeoLite2-City.mmdb {
auto_reload 60m;

$geoip2_continent_code source=$realip_remote_addr continent code;
$geoip2_country source=$realip_remote_addr country names en;
$geoip2_country_code source=$realip_remote_addr country iso_code;
$geoip2_region source=$realip_remote_addr subdivisions 0 names en;
$geoip2_region_code source=$realip_remote_addr subdivisions 0 iso_code;
$geoip2_city source=$realip_remote_addr city names en;
$geoip2_postal_code source=$realip_remote_addr postal code;
$geoip2_latitude source=$realip_remote_addr location latitude;
$geoip2_longitude source=$realip_remote_addr location longitude;
$geoip2_time_zone source=$realip_remote_addr location time_zone;
$geoip2_dma_code source=$realip_remote_addr location metro_code;
$geoip2_continent_code source=$realip continent code;
$geoip2_country source=$realip country names en;
$geoip2_country_code source=$realip country iso_code;
$geoip2_region source=$realip subdivisions 0 names en;
$geoip2_region_code source=$realip subdivisions 0 iso_code;
$geoip2_city source=$realip city names en;
$geoip2_postal_code source=$realip postal code;
$geoip2_latitude source=$realip location latitude;
$geoip2_longitude source=$realip location longitude;
$geoip2_time_zone source=$realip location time_zone;
$geoip2_dma_code source=$realip location metro_code;
}

geoip2 /etc/nginx/GeoLite2-ASN.mmdb {
auto_reload 60m;

$geoip2_asn source=$realip_remote_addr autonomous_system_number;
$geoip2_organization source=$realip_remote_addr autonomous_system_organization;
$geoip2_asn source=$realip autonomous_system_number;
$geoip2_organization source=$realip autonomous_system_organization;
}
30 changes: 30 additions & 0 deletions files/etc/nginx/sites-enabled/geoip.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
server {
listen 127.0.0.1:9081;
resolver 8.8.8.8 8.8.4.4;

location / {
charset utf-8;
add_header Cache-Control no-cache;

default_type application/json;
set $mybody '{"ip":"$realip","country_code":"$geoip2_country_code","region_code":"$geoip2_region_code","region_name":"$geoip2_region","city":"$geoip2_city","zip_code":"$geoip2_postal_code","latitude":"$geoip2_latitude","longitude":"$geoip2_longitude","metro_code":"$geoip2_dma_code"}';

if ($arg_format = "xml") {
add_header Content-Type application/xml;

set $mybody '<Response><IP>$realip</IP><CountryCode>$geoip2_country_code</CountryCode><CountryName>$geoip2_country</CountryName><RegionCode>$geoip2_region_code</RegionCode><RegionName>$geoip2_region</RegionName><City>$geoip2_city</City><ZipCode>$geoip2_postal_code</ZipCode><Latitude>$geoip2_latitude</Latitude><Longitude>$geoip2_longitude</Longitude><MetroCode>$geoip2_dma_code</MetroCode></Response>';
}

if ($arg_format = "csv") {
add_header Content-Type text/csv;
set $mybody '$realip,$geoip2_country_code,$geoip2_region_code,$geoip2_region,$geoip2_city,$geoip2_postal_code,$geoip2_latitude,$geoip2_longitude,$geoip2_dma_code';
}

if ($arg_callback) {
add_header Content-Type application/javascript;
return 200 '$arg_callback($mybody);';
}

return 200 $mybody;
}
}

0 comments on commit 5b3be40

Please sign in to comment.