# Modules programming. Several questions. $variables, URI rewriting

**URL:** <https://www.ruby-forum.com/t/modules-programming-several-questions-variables-uri-rewriting/185261>\
**Category:** NGINX\
**Created:** [March 17, 2010, 12:21am UTC](https://www.ruby-forum.com/t/modules-programming-several-questions-variables-uri-rewriting/185261 "2010-03-17T00:21:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mriadus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mriadus](https://www.ruby-forum.com/u/mriadus)\
**Post date:** [March 17, 2010, 12:21am UTC](https://www.ruby-forum.com/t/modules-programming-several-questions-variables-uri-rewriting/185261/1 "2010-03-17T00:21:38Z")

</div>

1. I can set a handler to one of several “phases”, like:  
“NGX\_HTTP\_REWRITE\_PHASE”, “NGX\_HTTP\_CONTENT\_PHASE” and other. How to  
define an order the handlers will be called inside the same “phase”?  
Lets suppose i bond several handlers to some phase and what will be  
called first? If that order is depends on order of modules “loading”,  
how to define that order of modules loading?

2. I developed a module that creates new variable “$z” during  
initialization. That variable can be used in configuration file. When  
nginx meets $z in configuration file, my handler is invoking (handler  
that returning the content of $z). If configuration file has three  
occurrences in the configuration file, my handler called 3 times. But i  
have a situation: the handler is calling 2 times per one variable. So it  
called 6 times for 3 occurrences of $z. Why? (that handler is called  
“get\_handler”).

3. OK. Lets suppose (2) is resolved. I want to create the content of $z  
once per request, but the “get\_handler” can be invoked N times for the  
same request. Where i can set the content of $z between “get\_handler”  
invocations for the same request?

4. I want to rewrite the ARGS from the request: i need to add “&z=1” to  
the end of args. For example: “a=1&b=1” —\> “a=1&b=2&z=1”. But inside  
the handler i have access to ngx\_http\_request\_t via pointer. And i have  
access to that ARGS via member “args” of the structure  
“ngx\_http\_request\_t”. That “args” member is a “ngx\_str\_t”. How can i  
change its length? I tried to create a new string and rewrite old  
pointer (r → args = new\_str\_ptr), but it is not working. Seems to be i  
have a copy of request during handler invocation and nginx killing that  
copy after request is processed?

Posted at Nginx Forum:

> **[Modules programming. Several questions. $variables, URI rewriting.](https://forum.nginx.org/read.php?2,64543,64543#msg-64543)**
>
> 1. I can set a handler to one of several "phases", like: "NGX\_HTTP\_REWRITE\_PHASE", "NGX\_HTTP\_CONTENT\_PHASE" and other. How to define an order the handlers will be called inside the same "phase"? Lets suppose i...

---

<div class="post-metadata">

**Author:** ![mriadus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mriadus](https://www.ruby-forum.com/u/mriadus)\
**Post date:** [March 17, 2010, 11:18am UTC](https://www.ruby-forum.com/t/modules-programming-several-questions-variables-uri-rewriting/185261/2 "2010-03-17T11:18:54Z")

</div>

Hello!

On Tue, Mar 16, 2010 at 07:20:47PM -0400, mriadus wrote:

> 1. I can set a handler to one of several “phases”, like:  
> “NGX\_HTTP\_REWRITE\_PHASE”, “NGX\_HTTP\_CONTENT\_PHASE” and other.  
> How to define an order the handlers will be called inside the  
> same “phase”? Lets suppose i bond several handlers to some phase  
> and what will be called first? If that order is depends on order  
> of modules “loading”, how to define that order of modules  
> loading?

Order of modules within phase is currently defined only by order  
in which modules were added during configure.

> 1. I developed a module that creates new variable “$z” during  
> initialization. That variable can be used in configuration file.  
> When nginx meets $z in configuration file, my handler is  
> invoking (handler that returning the content of $z). If  
> configuration file has three occurrences in the configuration  
> file, my handler called 3 times. But i have a situation: the  
> handler is calling 2 times per one variable. So it called 6  
> times for 3 occurrences of $z. Why? (that handler is called  
> “get\_handler”).

During evaluation of complex values variable obtained 2 times: to  
find out it’s length and to find out it’s content. It doesn’t  
normally lead to two calls to handler though. The fact that  
your handler is called 2 times indicate that you don’t set valid  
or not\_found flags on returned variable correctly.

> 1. OK. Lets suppose (2) is resolved. I want to create the  
> content of $z once per request, but the “get\_handler” can be  
> invoked N times for the same request. Where i can set the  
> content of $z between “get\_handler” invocations for the same  
> request?

Any content returned correctly will be cached for the rest of  
request (unless variable caching is explicitly disabled via  
appropriate flags guring variable creation or in it’s handler).

> 1. I want to rewrite the ARGS from the request: i need to add  
> “&z=1” to the end of args. For example: “a=1&b=1” —\>  
> “a=1&b=2&z=1”. But inside the handler i have access to  
> ngx\_http\_request\_t via pointer. And i have access to that ARGS  
> via member “args” of the structure “ngx\_http\_request\_t”. That  
> “args” member is a “ngx\_str\_t”. How can i change its length? I  
> tried to create a new string and rewrite old pointer (r → args  
> = new\_str\_ptr), but it is not working.

Changing r-\>args should work as long as done correctly (assigning  
a pointer to r-\>args isn’t correct one as it’s not pointer, you  
have to set r-\>args.data and r-\>args.len).

Note well: you may also want to set r-\>valid\_unparsed\_uri = 0 if  
you are changing args.

> Seems to be i have a copy  
> of request during handler invocation and nginx killing that copy  
> after request is processed?

No.

Maxim D.

p.s. It’s probably a good idea to ask such questions in  
nginx-devel@ mailing list instead, CC’d.
