Как запросить GeoIP в NGINX программно

Я успешно согласовываю NGINX с помощью параметра «--with-http-geoip-module» с модулем geoip.

и база данных указана в моем файле nginx.conf.

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

вроде все нормально и этот модуль загружается без ошибок.

Я добавил свой собственный модуль в NGINX. и я пытаюсь запросить переменную geoip_country_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);

}

not_found всегда равен 1 и не может получить информацию о местоположении.

кто-нибудь знает, в чем проблема?


person Mr.Wang from Next Door    schedule 03.07.2013    source источник


Ответы (1)


Я ошибся, 3-й параметр (хэш) для ngx_http_get_variable не является обязательным, даже если имя переменной указано во 2-м параметре.

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;
}
person Mr.Wang from Next Door    schedule 04.07.2013