<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">I believe it happens because you changed cf->cmd_type, so when nginx tries to parse the second location,<br>
it thinks it’s in a scope of ‘my custom module conf’ and not in a scope of ‘server’, and therefore doesn’t
<br>
recognize the location directive.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Nginx doesn’t automatically parse the contents of the block, you have to handle that yourself in your block<br>
handler. Usually this is done by updating the ngx_conf_t and calling ngx_conf_parse (make sure to save the<br>
original cf in a local variable so that you can restore it when ngx_conf_parse returns)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">There quite a few built-in nginx modules that add block directives, I suggest you look at one of them and
<br>
match your code to their implementation (for example, I used ‘map’ as reference)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Hope this help,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Eran<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> nginx-devel [mailto:nginx-devel-bounces@nginx.org]
<b>On Behalf Of </b>Joseph Spencer<br>
<b>Sent:</b> Thursday, June 29, 2017 1:01 AM<br>
<b>To:</b> nginx-devel@nginx.org<br>
<b>Subject:</b> Re: Help developing a block directive<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Thanks for the fast reply!<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">So I think I see how this works now; however, I'm getting a new error. Seems to be parsing related. Here is what I have now:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> #define NGX_MY_CUSTOM_MODULE_CONF 0x80000001<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> static ngx_command_t ngx_my_custom_module_commands[] = { <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> { <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> ngx_string("my_custom_module_block"), /* directive */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NGX_HTTP_SRV_CONF | <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NGX_HTTP_LOC_CONF | <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NGX_CONF_NOARGS | <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NGX_CONF_BLOCK, <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> my_custom_module_block, /* configuration setup function */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> 0, /* No offset. Only one context is supported. */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> 0, /* No offset when storing the module configuration on struct. */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NULL <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> }, <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> { <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> ngx_string("directive_for_my_custom_module_block"), /* directive */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NGX_CONF_TAKE1|</span>NGX_MY_CUSTOM_MODULE_CONF<span style="font-size:9.5pt">, <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> ngx_directive_for_my_custom_module_block_cmd, /* configuration setup function */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> 0, /* No offset. Only one context is supported. */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> 0, /* No offset when storing the module configuration on struct. */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> NULL <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> }, <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> ngx_null_command /* command termination */ <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> }; <o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"> static char *<span style="font-size:9.5pt">my_custom_module_block</span>(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> { <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> cf->cmd_type = NGX_MY_CUSTOM_MODULE_CONF; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> ngx_http_core_loc_conf_t *clcf; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> clcf->handler = ngx_my_custom_module_handler; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> return NGX_CONF_OK; <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">How I'm seeing the following error:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">2017/06/28 21:48:46 [emerg] 1#1: "location" directive is not allowed here in /etc/nginx/nginx.conf:26<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:26<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Here is my full configuration file:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">worker_processes 1;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">load_module "modules/ngx_my_custom_module.so";<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">events {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> worker_connections 1024;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">}<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">http {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> include mime.types;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> default_type application/octet-stream;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> sendfile on;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> keepalive_timeout 65;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> server {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> listen 80;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> server_name localhost;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> location / {<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> my_custom_module_block {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt"> directive_for_my_custom_module_block "Some value";<br>
}<o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> error_page 500 502 503 504 /50x.html;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> location = /50x.html {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> root html;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">}<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Wed, Jun 28, 2017 at 2:38 PM, Vladimir Homutov <<a href="mailto:vl@nginx.com" target="_blank">vl@nginx.com</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">On 29.06.2017 00:15, Joseph Spencer wrote:<br>
> Hello,<br>
><br>
> I'm trying to support this in my nginx.conf:<br>
><br>
> my_custom_module_block {<br>
> directive_for_my_custom_module_block "Some value";<br>
> }<br>
><br>
> Here is how I'm setting up the commands:<br>
><br>
> static ngx_command_t ngx_my_custom_module_commands[] = {<br>
><br>
> {<br>
><br>
> ngx_string("my_custom_module_block"), /* directive */<br>
><br>
> NGX_HTTP_SRV_CONF |<br>
><br>
> NGX_HTTP_LOC_CONF |<br>
><br>
> NGX_CONF_NOARGS |<br>
><br>
> NGX_CONF_BLOCK,<br>
><br>
> my_custom_module_block, /* configuration setup function */<br>
><br>
> 0, /* No offset. Only one context is supported. */<br>
><br>
> 0, /* No offset when storing the module configuration on struct.<br>
> */<br>
> NULL<br>
><br>
> },<br>
><br>
><br>
><br>
> {<br>
><br>
> ngx_string("directive_for_my_custom_module_block"), /* directive<br>
> */<br>
> NGX_CONF_TAKE1,<br>
><br>
> ngx_directive_for_my_custom_module_block_cmd, /* configuration<br>
> setup function */<br>
> 0, /* No offset. Only one context is supported. */<br>
><br>
> 0, /* No offset when storing the module configuration on struct.<br>
> */<br>
> NULL<br>
><br>
> },<br>
><br>
><br>
><br>
> ngx_null_command /* command termination */<br>
><br>
> };<br>
><br>
> Everything compiles fine; however, I receive the following error and<br>
> nginx won't start:<br>
><br>
> 2017/06/28 21:02:44 [emerg] 1#1: "directive_for_my_custom_module_block"<br>
> directive is not allowed here in /etc/nginx/nginx.conf:19<br>
> nginx: [emerg] "directive_for_my_custom_module_block" directive is not<br>
> allowed here in /etc/nginx/nginx.conf:19<br>
><br>
> If anyone could be of assistance I would greatly appreciate it.<br>
><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal">Take a look at ngx_conf_file.h to understand which structure flags have.<br>
You did not specify any possible context for the command that will<br>
appear in your block: NGX_CONF_TAKE1 solo only describes number of<br>
arguments, but command is not matching anything, thus the error.<br>
<br>
You can either declare your own context, like ngx_http_upstream_module<br>
does (NGX_HTTP_UPS_CONF for commands inside upstream{} block) or<br>
use API from ngx_conf_file.h to process commands inside your block<br>
like ngx_conf_handler() does.<br>
<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
nginx-devel mailing list<br>
<a href="mailto:nginx-devel@nginx.org">nginx-devel@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx-devel" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx-devel</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><br>
<br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">-- <o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<div>
<p class="MsoNormal">Joe Spencer (member)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Kogo Software LLC<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>