Custom Module Directive is Duplicate? Error in conf?
de_nginx_noob
nginx-forum at nginx.us
Fri Oct 23 14:01:34 UTC 2015
After compiling a test module that sets a variable equal to "test
successful", I get a "directive is duplicate in (path to conf file)"
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
//#include <ngx_log.h>
//#include <ngx_conf_file.h>
#define MAX_STRING_LEN 256
// static char *ngx_http_netacuity(ngx_conf_t *cf, void *post, void *data);
// static ngx_conf_post_handler_pt ngx_http_netacuity_p =
ngx_http_netacuity;
static void *ngx_http_netacuity_create_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_netacuity_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_netacuity_test_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
/**
Module configuration struct
*/
typedef struct {
ngx_int_t featureCode;
} ngx_http_netacuity_conf_t;
/**
Initializes memory for module configuration struct
*/
static void *ngx_http_netacuity_create_conf(ngx_conf_t *cf)
{
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "HIT FUNCTION --->
ngx_http_netacuity_create_conf");
ngx_http_netacuity_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_netacuity_conf_t));
if(conf == NULL) {
return NULL;
}
conf->featureCode = NGX_CONF_UNSET_UINT;
return conf;
}
/**
Module directive array, which holds a subarray for each module directive
*/
static ngx_command_t ngx_http_netacuity_commands[] = {
{
ngx_string("testAPI"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_netacuity_conf_t, featureCode),
NULL
},
ngx_null_command
};
/**
The module context, which has hooks for creating the configuration
*/
static ngx_http_module_t ngx_http_netacuity_module_ctx = {
ngx_http_netacuity_add_variables, //preconfiguration
NULL, //postconfiguration
ngx_http_netacuity_create_conf, //create main configuration
NULL, //init main configuration
NULL, //create server configuration
NULL, //merge server configuration
NULL, //create location configuration
NULL //merge location configuration
};
/**
The module which binds the context and commands
*/
ngx_module_t ngx_http_netacuity_module = {
NGX_MODULE_V1,
&ngx_http_netacuity_module_ctx, //module context
ngx_http_netacuity_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
};
/**
Holds all the API Variables that the user can call upon
*/
static ngx_http_variable_t ngx_http_netacuity_vars[] = {
{
ngx_string("testVar"), NULL, ngx_http_netacuity_test_variable,
MAX_STRING_LEN, 0, 0
},
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
/**
Called during preconfiguration - adds variables for use
*/
static ngx_int_t ngx_http_netacuity_add_variables(ngx_conf_t *cf)
{
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "HIT FUNCTION --->
ngx_http_netacuity_add_variables");
ngx_http_variable_t *var, *v;
for(v = ngx_http_netacuity_vars; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if(var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
/**
Get handlers for variables
*/
static ngx_int_t ngx_http_netacuity_test_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_log_error(NGX_LOG_DEBUG, (r->connection)->log, 0, "HIT FUNCTION --->
ngx_http_netacuity_test_variable");
size_t len;
char *val;
ngx_http_netacuity_conf_t *nac;
nac = ngx_http_get_module_main_conf(r, ngx_http_netacuity_module);
if(nac->featureCode == 0) {
goto not_found;
}
val = "test successful";
if(val == NULL) {
goto not_found;
}
len = ngx_strlen(val);
v->data = ngx_pnalloc(r->pool, len);
if(v->data == NULL) {
//ngx_free(val);
return NGX_ERROR;
}
ngx_memcpy(v->data, val, len);
v->len = len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
//ngx_free(val);
return NGX_OK;
not_found:
v->not_found = 1;
return NGX_OK;
}
Can someone explain what's going on? I tried googling the error, but found
nothing useful.
This is my conf file :
worker_process 1;
events {
worker_connections 1024;
}
http {
include mine.types;
default_type application/octet-stream;
log_format main ‘$remote_addr’;
testAPI 7;
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Posted at Nginx Forum: https://forum.nginx.org/read.php?2,262425,262425#msg-262425
More information about the nginx
mailing list