1

我开始使用 nginx 1.4.1 的自定义负载平衡模块,但在启动和运行我的模块框架时遇到了麻烦。我正在关注Evan Miller's Guide以及这个一致的散列模块

我的问题是,在我的模块初始化函数中,调用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;
    }
    ...
}
4

1 回答 1

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);
于 2013-10-05T14:32:00.770 回答