Modules programming. Several questions. $variables, URI rewriting

  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:

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.