1

nginx 1.4.1 のカスタム ロード バランシング モジュールを使い始めましたが、モジュールのスケルトンを起動して実行するのに問題があります。私はEvan Miller's Guideと、この 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;
    }
    ...
}
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 に答える