我的自定义负载平衡模块中的上游服务器配置为NULL

时间:2013-09-30 00:33:42

标签: c nginx

我开始使用nginx 1.4.1的自定义负载平衡模块,而且我无法启动并运行模块框架。我正在关注Evan Miller's Guide以及this consistent-hashing module

我的问题是在我的模块初始化函数中,对ngx_http_conf_get_module_srv_conf的调用返回NULL,但我需要访问此结构以分配另一个回调(请参阅下面的完整代码以放置以下内容代码进入上下文):

static char * ngx_http_upstream_test(
        ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
    ngx_http_upstream_srv_conf_t  *uscf;
    uscf = ngx_http_conf_get_module_srv_conf(
            cf, ngx_http_upstream_test_module);
    if(NULL == uscf) {
        // This branch gets hit :(
        printf("ERROR!  Couldn't get server config.\n");
        return NGX_CONF_ERROR;
    }
    uscf->peer.init_upstream = ngx_http_upstream_init_test;
    uscf->flags = NGX_HTTP_UPSTREAM_CREATE;
    return NGX_CONF_OK;
}

这是我的裸骨模块的完整代码:

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static char * ngx_http_upstream_test(
        ngx_conf_t*,ngx_command_t*,void*);
static ngx_int_t ngx_http_upstream_init_test(
        ngx_conf_t *,ngx_http_upstream_srv_conf_t*);
static ngx_int_t ngx_http_upstream_init_test_peer(
        ngx_http_request_t*,ngx_http_upstream_srv_conf_t*);
static ngx_int_t ngx_http_upstream_get_test_peer(
        ngx_peer_connection_t*,void*);
static void ngx_http_upstream_free_test_peer(
        ngx_peer_connection_t *pc,void *data,ngx_uint_t state);

static ngx_command_t  ngx_http_upstream_test_commands[] = {

    {   ngx_string("test"),
        NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
        ngx_http_upstream_test,
        0,
        0,
        NULL },

    ngx_null_command
};

static ngx_http_module_t  ngx_http_upstream_test_module_ctx = {
    NULL,                                  /* preconfiguration */
    NULL,                                  /* postconfiguration */
    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */
    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */
    NULL,                                  /* create location configuration */
    NULL                                   /* merge location configuration */
};

ngx_module_t  ngx_http_upstream_test_module = {
    NGX_MODULE_V1,
    &ngx_http_upstream_test_module_ctx,            /* module context */
    ngx_http_upstream_test_commands,               /* module directives */
    NGX_HTTP_MODULE,                               /* module type */
    NULL,                                          /* init master */
    NULL,                                          /* init module */
    NULL,                                          /* init process */
    NULL,                                          /* init thread */
    NULL,                                          /* exit thread */
    NULL,                                          /* exit process */
    NULL,                                          /* exit master */
    NGX_MODULE_V1_PADDING
};

static char * ngx_http_upstream_test(
        ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
    ngx_http_upstream_srv_conf_t  *uscf;
    uscf = ngx_http_conf_get_module_srv_conf(
            cf, ngx_http_upstream_test_module);
    if(NULL == uscf) {
        printf("ERROR!  Couldn't get server config.\n");
        return NGX_CONF_ERROR;
    }
    uscf->peer.init_upstream = ngx_http_upstream_init_test;
    uscf->flags = NGX_HTTP_UPSTREAM_CREATE;
    return NGX_CONF_OK;
}

static ngx_int_t ngx_http_upstream_init_test(
        ngx_conf_t *cf,ngx_http_upstream_srv_conf_t *us) {
    us->peer.init = ngx_http_upstream_init_test_peer;
    return NGX_OK;
}

static ngx_int_t ngx_http_upstream_init_test_peer(
        ngx_http_request_t* r,
        ngx_http_upstream_srv_conf_t* us) {
        r->upstream->peer.free = ngx_http_upstream_free_test_peer;
        r->upstream->peer.get = ngx_http_upstream_get_test_peer;
        return NGX_OK;
}

static ngx_int_t ngx_http_upstream_get_test_peer(
        ngx_peer_connection_t* pc,
        void* data) { return NGX_OK; }

static void ngx_http_upstream_free_test_peer(
        ngx_peer_connection_t *pc,
        void *data,
        ngx_uint_t state) { return; }

我的配置文件如下所示:

ngx_addon_name=ngx_http_upstream_test_module
HTTP_MODULES="$HTTP_MODULES ngx_http_upstream_test_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upstream_test_module.c"

我通过运行我的模块编译nginx 1.4.1:

./configure --add-module=/path/to/my/test/module
make
make install

我的nginx.conf文件如下所示:

http {

    upstream main {
        test;
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
    }
    ...
}

1 个答案:

答案 0 :(得分:0)

嗯,我觉得很傻。问题是这一行:

ngx_http_upstream_srv_conf_t  *uscf;
uscf = ngx_http_conf_get_module_srv_conf(
            cf, ngx_http_upstream_test_module);

当我应该通过ngx_http_conf_get_module_srv_conf时,我正在将我的模块传递给ngx_http_upstream_module来电。正确的代码是:

ngx_http_upstream_srv_conf_t  *uscf;
uscf = ngx_http_conf_get_module_srv_conf(
            cf, ngx_http_upstream_module);