HttpGeoipModule $geoip_country_code 为空

Kit*_*nde 3 nginx

在 nginx.conf 中:

http { 
    geoip_country /etc/nginx/GeoIP.dat;
    ...
}
Run Code Online (Sandbox Code Playgroud)

如果我做:

server{
    ...
    location / {
        add_header X-Geo $geoip_country_code;                   
        add_header X-Geo3 $geoip_country_code3;
        add_header X-IP $remote_addr;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

X-IP出现在我的标题中。

$ curl -I www.example.org
HTTP/1.1 302 FOUND
Content-Type: text/html; charset=utf-8
Date: Thu, 17 Jan 2013 19:29:23 GMT
Location: http://www.example.org/login/?next=/
Server: nginx/1.2.2
Vary: Cookie
X-IP: 10.139.34.12
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

如果我将位置块更改为:

location / {
    add_header X-Geo "foo";                   
    add_header X-Geo3 "bar";
    add_header X-IP $remote_addr;
    ...
}
Run Code Online (Sandbox Code Playgroud)

标题出现了,我怎样才能得到$geoip_country_code

小智 5

我刚刚发现 geo_ip 有一个内部选项来使用X-Forwarded-For

syntax: geoip_proxy address | CIDR;
default:     —
context:    http
This directive appeared in versions 1.3.0 and 1.2.1.
Defines trusted addresses. When a request comes from a trusted address, an address from the “X-Forwarded-For” request header field will be used instead.
Run Code Online (Sandbox Code Playgroud)