Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 自定义负载平衡模块中的上游服务器配置为空_C_Nginx - Fatal编程技术网

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

C 自定义负载平衡模块中的上游服务器配置为空,c,nginx,C,Nginx,我正在开始使用nginx1.4.1的定制负载平衡模块,但在启动和运行模块框架时遇到了问题。我跟在后面 我的问题是,在我的模块初始化函数中,对的调用返回NULL,但我需要访问此结构以分配另一个回调。请参阅下面的完整代码,将以下代码放入上下文: static char * ngx_http_upstream_test( ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_upstream_srv_conf_t

我正在开始使用nginx1.4.1的定制负载平衡模块,但在启动和运行模块框架时遇到了问题。我跟在后面

我的问题是,在我的模块初始化函数中,对的调用返回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; }
我通过运行以下命令,使用我的模块编译nginx 1.4.1:

./configure --add-module=/path/to/my/test/module
make
make install
我的nginx.conf文件如下所示:

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"
http {

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

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

ngx_http_upstream_srv_conf_t  *uscf;
uscf = ngx_http_conf_get_module_srv_conf(
            cf, ngx_http_upstream_test_module);
我正在将模块传递给呼叫,而我本应该传递模块的。正确的代码是:

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