Skip to content

Commit 1f80088

Browse files
committed
fix geoip behind proxy
1 parent eb12290 commit 1f80088

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

files/etc/nginx/cdn-ips.conf

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# ref your cdn here, example bunnycdn below
2+
include cdn/cdn-bunny.conf;
3+
14
# Local cache/proxy
25
set_real_ip_from 0.0.0.0/0;
36
set_real_ip_from ::/0;
@@ -6,6 +9,17 @@ set_real_ip_from 192.168.0.0/16;
69
set_real_ip_from 10.0.0.0/8;
710
set_real_ip_from 172.0.0.0/24;
811

9-
#real_ip_header X-Real-IP;
10-
#real_ip_header X-Forwarded-For;
11-
#real_ip_recursive on;
12+
real_ip_header X-Forwarded-For;
13+
real_ip_recursive on;
14+
15+
# map realip to use with geolite2.conf
16+
map $http_x_forwarded_for $forwarded_ip {
17+
default $http_x_forwarded_for;
18+
'' $realip_remote_addr;
19+
}
20+
21+
# simply grab the first IP if there are multiples
22+
map $forwarded_ip $realip {"~(?<IP>[^,]*),*.*" $IP;}
23+
24+
25+
include geolite2.conf;

files/etc/nginx/geoipme.conf

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
# usage: include /etc/nginx/geoipme.conf;
2-
# this proxy pass to sites-enabled/geoip.conf
1+
# use this with sites-enabled/1-geoip.conf
32
location ~ /(geoip|geoipme)+/(?<ip>.*) {
4-
set $realip $ip;
5-
6-
if ($realip = '') {
7-
set $realip $realip_remote_addr;
3+
if ($ip) {
4+
set $realip $ip;
85
}
96

7+
proxy_set_header X-Real-IP $realip;
108
proxy_set_header X-Forwarded-For $realip;
119
proxy_set_header Host "127.0.0.1";
1210
proxy_pass "http://127.0.0.1:9081";
13-
}
11+
}

files/etc/nginx/geolite2.conf

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
map $http_x_forwarded_for $realip {
2-
default $http_x_forwarded_for;
3-
'' $realip;
4-
}
1+
# $realip is map inside of cdn-ips.conf
52

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

0 commit comments

Comments
 (0)