Writing first test Nginx module

Hello everyone,

Recently for some of our needs on Nginx, I am working to develop a new
module on Nginx. So I have started to develop a test and basic module.
My
intention is to call a function before selecting one servers in upstream
section. For this, I set NGX_STREAM_MODULE type. when running the nginx
with
new option “stream_test” in upstream configuration, I get below error :
ERROR: nginx: [emerg] “stream_test” directive is not allowed here in
xxxxxx

I will appreciate if somebody assist me on this.Thanks

Following is the source code of test module
#######################################Test
Module###############################
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
#include <ngx_stream_upstream.h>

static char *ngx_http_test(ngx_conf_t *cf, ngx_command_t *cmd,void
*conf);

static ngx_command_t ngx_stream_upstream_test_commands[] =
{
{ ngx_string(“stream_test”),
NGX_STREAM_SRV_CONF|NGX_CONF_NOARGS,
ngx_http_test,
0,
0,
NULL
},

ngx_null_command

};

static ngx_stream_module_t ngx_http_test_ctx =
{
NULL,NULL,NULL,NULL
};

ngx_module_t ngx_http_test_module =
{
NGX_MODULE_V1,
&ngx_http_test_ctx, /* module context /
ngx_stream_upstream_test_commands, /
module directives /
NGX_STREAM_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_test(ngx_conf_t *cf, ngx_command_t *cmd,void
*conf)
{
//ngx_stream_upstream_srv_conf_t *uscf;
//uscf = ngx_stream_conf_get_module_srv_conf(cf,
ngx_stream_upstream_module);
ngx_conf_log_error(NGX_LOG_ERR,cf,0,“Test Function was called!”);
/if (uscf->peer.init_upstream)
{
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,“Nima Test Func: load
balancing
method redefined”);
}
/

return NGX_CONF_OK;

}

Posted at Nginx Forum:

Hi again,

After some searching on Nginx source code, eventually i did find out the
issue. The issue was on module type. I had defined it as
NGX_STREAM_MODULE,
but it must be NGX_HTTP_MODULE because i did intend to add some features
to
upstream module. Also I had to change configuration directive from
NGX_STREAM_SRV_CONF|NGX_CONF_NOARGS to
NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS.
Type of ngx_*_ctx was not correct and it was changed to
ngx_http_module_t .

Sincerely,
Nima

Posted at Nginx Forum: