Anatomy Nginx Module Development (3) Basic function Realization of Ngx_http_hello_world_module module

Source: Internet
Author: User

Remember when we defined a struct as follows?

typedef struct {    ngx_str_t output_words;} ngx_http_hello_world_loc_conf_t;

It is the location component configuration of HelloWorld, which has a string member Output_words.

1 Create Location

Used for location creation functions in Ngx_http_hello_world_module_ctx:

static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf) {    ngx_http_hello_world_loc_conf_t* conf;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    conf->output_words.len = 0;    conf->output_words.data = NULL;    return conf;}

As we can see, we are allocating the memory of the size of the ngx_http_hello_world_loc_conf_t used first. And initializes the ngx_http_hello_world_loc_conf_t unique member Output_words.

2 Merge Location

For the location merge function in Ngx_http_hello_world_module_ctx:

static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf,        void* parent,        void* child) {    ngx_http_hello_world_loc_conf_t* prev = parent;    ngx_http_hello_world_loc_conf_t* conf = child;    ngx_conf_merge_str_value(conf->output_words, prev->output_words, "boy");    return NGX_CONF_OK;}
3 ngx_http_hello_world3.1 ngx_http_conf_get_module_loc_conf

First you need to understand a "function" provided by Nginx:

ngx_http_conf_get_module_loc_conf(    cf, // configuration    module);

In fact, it is a macro definition, in Ngx_http_config.h:

#define ngx_http_conf_get_module_loc_conf(cf, module)     ((ngx_http_conf_ctx_t *) cf->ctx)->loc_conf[module.ctx_index]

Its role is to locate the location configuration in the specified module by using the context of the CF configuration.

3.2 Ngx_http_hello_world

The set field for the only command we define in Ngx_http_hello_world_commands.

 static char* Ngx_http_hello_world (ngx_conf_t* CF, ngx_command_t* cmd, void* conf) {Ngx_http_core_loc_co    nf_t* CLCF;    CLCF = ngx_http_conf_get_module_loc_conf (cf, Ngx_http_core_module);    Clcf->handler = Ngx_http_hello_world_handler;    Ngx_conf_set_str_slot (cf, CMD, conf); return NGX_CONF_OK;}  

The function is to generate a response to the request, that is, in this case hello_world, Poechant . Then get to the location configuration of Http_core_module, CLCF (core location ConF). Assign a value Ngx_http_hello_world_handler to the handler field of CLCF, which is described below. The Ngx_conf_set_str_slot is then routinely called.

4 Ngx_http_hello_world_handler

First you need to know a "function" provided by Nginx:

4.1 ngx_http_conf_get_module_loc_conf
ngx_http_conf_get_module_loc_conf(    r, // request    module);

In fact, it is a macro definition, in Ngx_http_config.h:

#define ngx_http_get_module_loc_conf(r, module)  (r)->loc_conf[module.ctx_index]

The function is to find the location configuration requested by the request according to the module's index field (Ctx_index).

4.2 Ngx_http_hello_world_handler

First of all, look at the more classic Nginx buffer ngx_buf_t bar. Only the sections related to this article are described here.

struct ngx_buf_s {    u_char          *pos;    u_char          *last;    u_char          *start;         /* start of buffer */    u_char          *end;           /* end of buffer */    …};

These four pointers divide the buffer into 3 parts. The following are the differences:

    • read-only buffers: for read-only buffers, this is part of the read-only section;
  • Part Two (Pos to last): Blockquote style= "BORDER-LEFT-WIDTH:4PX; Border-left-style:solid; Border-left-color: #dddddd; Color: #444444; padding-top:0px; padding-right:15px; padding-bottom:0px; padding-left:15px; margin:0px; " >
    • read-only buffers: for read-only buffers, this is part of the read-only section;
    • write buffers only: For write-only buffers, the area that has been written.
  • The third part (last to end):
    • Read-only buffers: there is no such part for read-only buffers;
    • Write-only buffers: for write-only buffers, the remaining writable regions.

Another reason why ngx_buf_t Classic is because nginx can flush the output in advance, so these buf can be reused after output, can avoid redistribution, improve system performance, known as FREE_BUF, and not be output buf is busy_buf.

So come and see Ngx_http_hello_world_handler:

Static ngx_int_t Ngx_http_hello_world_handler (ngx_http_request_t* r) {ngx_int_t rc;    ngx_buf_t* b;    Ngx_chain_t out[2];    ngx_http_hello_world_loc_conf_t* HLCF;    HLCF = ngx_http_get_module_loc_conf (R, Ngx_http_hello_world_module);    Set the header of the request R->headers_out.content_type.len = sizeof ("Text/plain")-1;    R->headers_out.content_type.data = (u_char*) "Text/plain";    Allocate buffer memory Space B = Ngx_pcalloc (R->pool, sizeof (ngx_buf_t));    1th block buffer out[0].buf = b;    Out[0].next = &out[1];    In this module, buffers only need to write data, so only set pos and last B->pos = (u_char*) "Hello_world,";    B->last = b->pos + sizeof ("Hello_world,")-1; B->memory = 1;    The indicator buffer is memory buffer//allocation buffer for memory space B = Ngx_pcalloc (R->pool, sizeof (ngx_buf_t));    2nd block Buffer out[1].buf = b;    Out[1].next = NULL;    In this module, buffers only need to write data, so only pos and last B->pos = hlcf->output_words.data; are set    B->last = Hlcf->output_words.data + (Hlcf->output_words.len); B->memory = 1; The indicator buffer is memory buffered B->LAST_BUF = 1;    Mark the entire response to the last buffer, Nginx will immediately send buffer all the data//Set the header of the request R->headers_out.status = NGX_HTTP_OK;    R->headers_out.content_length_n = Hlcf->output_words.len + sizeof ("Hello_world,")-1;    Send Request rc = Ngx_http_send_header (r);    if (rc = = Ngx_error | | rc > NGX_OK | | r->header_only) {return rc; } return Ngx_http_output_filter (R, &out[0]);}
5 Reference
    1. Www.evanmiller.org/nginx-modules-guide.html
    2. Http://blog.sina.com.cn/s/blog_7303a1dc0100x70t.html

-

Anatomy Nginx Module Development (3) Basic function Realization of Ngx_http_hello_world_module module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.