2013-07-03 4 views
1

geoip 모듈과 함께 성공적으로 매개 변수 "--with-http-geoip-module"을 사용하여 NGINX를 컴파일합니다.NGINX에서 GeoIP를 프로그래밍 방식으로 쿼리하는 방법

이며 데이터베이스는 내 nginx.conf 파일에 표시됩니다.

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

이 모듈은 오류없이로드됩니다.

나는 자체 모듈을 NGINX에 추가했습니다. 와 내가 쿼리하려고 시도했습니다. geoip_country_code 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); 

} 

는 NOT_FOUND는 항상 1이고 위치 정보를 얻을 수 없습니다.

누구에게 무슨 문제가 있는지 알고 있나요?

답변

3

내가 틀 렸습니다. 두 번째 매개 변수에 변수 이름이 표시 되더라도 ngx_http_get_variable에 대한 세 번째 매개 변수 (해시)는 선택 사항이 아닙니다.

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