2013-07-03 18 views
1

我用geoip模塊成功地使用參數「--with-http-geoip-module」來編譯NGINX。如何在NGINX中可編程地查詢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,不能獲取位置信息。

有沒有人知道是什麼問題?

回答

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