如何在NGINX中编程查询GeoIP

如何在NGINX中编程查询GeoIP,nginx,geoip,maxmind,Nginx,Geoip,Maxmind,我使用参数“--with http geoip module”成功地将NGINX编译为geoip模块 数据库显示在mynginx.conf文件中 http{ .... geoip_country /usr/lib/local/geoip.dat; .... } 一切似乎都很好,这个模块加载没有错误 我已经将自己的模块添加到NGINX中。 我尝试从geo模块中查询geoip\u country\u code变量 static ngx_int_t ngx_http_cm

我使用参数“--with http geoip module”成功地将NGINX编译为geoip模块

数据库显示在mynginx.conf文件中

http{
    ....
    geoip_country   /usr/lib/local/geoip.dat;
    ....
}
一切似乎都很好,这个模块加载没有错误

我已经将自己的模块添加到NGINX中。 我尝试从geo模块中查询geoip\u country\u code变量

static ngx_int_t
ngx_http_cms_url_handler(ngx_http_request_t *r)
{
    ngx_str_t variable_name = ngx_string("geoip_country_code");

    ngx_http_variable_value_t * geoip_country_code_var = ngx_http_get_variable( r, &variable_name, 0);
    ngx_log_debug( NGX_LOG_INFO, r->connection->log, 0, "ngx_http_get_variable[%d]", geoip_country_code_var->not_found);

}
未找到的始终为1,无法获取位置信息


有人知道问题出在哪里吗?

我错了,ngx_http_get_变量的第三个参数(散列)不是可选的,即使第二个参数中指明了变量名

static unsigned
get_ip_country(ngx_http_request_t *r, ngx_str_t *geoip_country_code){
    ngx_str_t variable_name = ngx_string("geoip_country_code");

    ngx_int_t hash = ngx_hash_key (variable_name.data, variable_name.len);

    ngx_http_variable_value_t * geoip_country_code_var = ngx_http_get_variable( r, &variable_name, hash);
    if( geoip_country_code_var && !geoip_country_code_var->not_found && geoip_country_code_var->valid){
        geoip_country_code->data = ngx_palloc( r->pool, geoip_country_code_var->len);
        geoip_country_code->len = geoip_country_code_var->len;


        ngx_strlow( geoip_country_code->data, geoip_country_code_var->data, geoip_country_code->len);
        return 1;
    }
    return 0;
}

我错了,ngx_http_get_变量的第三个参数(散列)不是可选的,即使在第二个参数中指明了变量名

static unsigned
get_ip_country(ngx_http_request_t *r, ngx_str_t *geoip_country_code){
    ngx_str_t variable_name = ngx_string("geoip_country_code");

    ngx_int_t hash = ngx_hash_key (variable_name.data, variable_name.len);

    ngx_http_variable_value_t * geoip_country_code_var = ngx_http_get_variable( r, &variable_name, hash);
    if( geoip_country_code_var && !geoip_country_code_var->not_found && geoip_country_code_var->valid){
        geoip_country_code->data = ngx_palloc( r->pool, geoip_country_code_var->len);
        geoip_country_code->len = geoip_country_code_var->len;


        ngx_strlow( geoip_country_code->data, geoip_country_code_var->data, geoip_country_code->len);
        return 1;
    }
    return 0;
}