Writing first test Nginx module

nima0102 nginx-forum at nginx.us
Sat Jun 6 07:25:21 UTC 2015


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: http://forum.nginx.org/read.php?2,259411,259411#msg-259411



More information about the nginx mailing list