【问题标题】:How to query the GeoIP in NGINX programmably如何以编程方式在 NGINX 中查询 GeoIP
【发布时间】:2013-07-03 13:56:59
【问题描述】:

我使用参数“--with-http-geoip-module”成功编译了NGINX和geoip模块。

数据库在我的 nginx.conf 文件中显示。

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

一切似乎都很好,这个模块加载没有错误。

我已将自己的模块添加到 NGINX 中。 我尝试从 geo 模块查询 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,无法获取位置信息。

有谁知道问题出在哪里?

【问题讨论】:

    标签: nginx geoip maxmind


    【解决方案1】:

    我错了,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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多