Question

I complie the NGINX using the parameter "--with-http-geoip-module" with the geoip module successfully.

and the database is indicated in my nginx.conf file.

http{
    ....
    geoip_country   /usr/lib/local/geoip.dat;
    ....
}

everything seems fine and this module is loaded without error.

I have added my own module into NGINX. and I try to query geoip_country_code variable from geo module.

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);

}

the not_found is always 1 and can't obtain the location info.

does anyone know what is the problem?

Was it helpful?

Solution

I was wrong, the 3rd parameter (hash) to ngx_http_get_variable is not optional even if the variable name is indicated in 2nd parameter.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top