basic module questions

agentzh agentzh at gmail.com
Sun Sep 25 16:17:28 UTC 2011


On Sun, Sep 25, 2011 at 4:40 AM, Shaun savage <savages at mozapps.com> wrote:
> I am starting to learn how to write nginx module.  I have a few basic
> questions, these are more reference questions.
>
> 1> how do you set/create request variable?

We create nginx variables at config time by calling
ngx_http_add_variable and assign value to it by calling the
ngx_http_variable_t variable's set_handler or write to  its value
buffer in r->variables directly. Check out the relavant source code
segments in the nginx core or 3rd-party modules like ngx_lua.

> 2> how do you read a request variable?

By calling ngx_http_get_indexed_variable or ngx_http_get_variable and
read the ngx_http_variable_value_t result returned by them.

> 3> can an authorization and a filter be in the same module

If you mean access phase handlers and an output filter in a same
module, then sure, you can. The ngx_lua module can register rewrite
phase handler, access phase handler, content handler, and output
header/body filters at the same time (though it only register any of
these things on demand, i.e., relavant config directives are truly
used in nginx.conf).

> 4> memcached checks at what phase?  I fails does it check again after
> the cache is loaded.

The Nginx core does not have this capability. If you mean ngx_srcahe
being used with ngx_memc, then ngx_srcache does memcached check at the
"post access" phase.

> 5> srcache first checks cache then does a subrequest?  can it do this?
>

The ngx_srcache module checks the cache *via* a subrequest. In case a
cache miss, there would be two subrequests, one for fetching values
(if any) from the cache (i.e., cache lookup), the other for
propagating values to the cache (i.e., cache update).

>        check cache     -> no -> load cache, retry
>            yes
>
>        check cache for more information -> no -> ERROR
>            yes
>

The actual workflow of ngx_srcache looks like this:

    cache lookup ->
          hit -> send the cached value as the response
          miss -> continue down to later phases and generate response normally
                  -> and then does a cache update with the response output

Please check out ngx_srcache's documentation for more details:

    http://wiki.nginx.org/HttpSRCacheModule

>        check $url or $request_filename with information from cache ->
> DECLINE

If you do have more sophisticated caching policies, you can code this
up with some Lua by means of the ngx_lua module:

    http://wiki.nginx.org/HttpLuaModule

Writing (non-trivial) Nginx modules in C is hard, and that's why we've
been working hard on ngx_lua ;) Coding in fast (compared with C)
scripting languages like Lua is much easier :)

Regards,
-agentzh



More information about the nginx-devel mailing list