Nginx HTTP Module Development

Source: Internet
Author: User

About the Nginx HTTP module development of very few articles, only emiler of the article on the HTTP module, but that article, did not say that the event-based module how to develop. And the content of the article is really a bit of a little meaning. Therefore, the development of the HTTP event module is summarized and shared with you. However, in any case, to carry out Nginx module development, the best way is to find more similar modules of the code for reference, try to see more.

In general, an HTTP module is composed of the following parts: 1. Module Configuration structure: (Configure structure) is responsible for storing the contents of configuration items, each configuration item will generate a configuration structure, can be easily configured by the module preprocessing, storage of the corresponding structure of the rest of the content, The approximate structure is as follows:
typedef struct {
/**
* Your configuration struct members */} Ngx_http_<your module>_conf_t; 2. Module instructions (modules directives)
Static ngx_command_t Ngx_<your module>_commands[] = {
     {ngx_string ("test"),
Ngx_http_loc_conf| Ngx_conf_noargs,
<your Read Conf function>
Ngx_http_loc_conf_offset,
0,
NULL},
...
Ngx_null_command
};
The structure is as follows:
struct ngx_command_t {
    ngx_str_t             name;           /* Text of instructions in configuration */
ngx_uint_t type; /* Indicates where the instruction can be used */
Char * (*set) (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);/* Read the configuration callback function, usually processing the parameters, and writing to the configuration structure body */
    ngx_uint_t            conf;
ngx_uint_t offset;
void *post;
};
3. Module context
This structure mainly defines a series of callback functions, which are callbacks at different times.
typedef struct {
     ngx_int_t   (*preconfiguration) (ngx_conf_t *CF);
ngx_int_t (*postconfiguration) (ngx_conf_t *CF);

void * (*create_main_conf) (ngx_conf_t *CF);
Char * (*init_main_conf) (ngx_conf_t *cf, void *conf);

void * (*create_srv_conf) (ngx_conf_t *CF);
Char * (*merge_srv_conf) (ngx_conf_t *cf, void *prev, void *conf);

void * (*create_loc_conf) (ngx_conf_t *CF);
Char * (*merge_loc_conf) (ngx_conf_t *cf, void *prev, void *conf);
} ngx_http_module_t;
for the development of phase handler, it is necessary to insert handler into the appropriate stage during the Postconfiguration period.  4. Define the module structure body ngx_module_t Ngx_http_<module name>_module = {ngx_module_v1, &ngx_http_<module name>_module_ctx,/* Module context */Ngx_http_<module name>_commands,/*module directives */ngx_http_module,/* Module type */NULL  ,/* INIT master */NULL,/* INIT module */NULL,/* INIT process */NULL,/* INIT thread */NULL,/* Exit thread */  NULL,/* exit process/NULL,/* Exit Master */ngx_module_v1_padding}; After completing this step, basically a module has been formed.     5. Insert phase callback function in order to add the corresponding handler function in the phase of HTTP processing, the corresponding handler insertion in the Postconfigure callback function is required. The following is the code in the Http_access module:
Static ngx_int_t
Ngx_http_access_init (ngx_conf_t *CF)
{
Ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;

CMCF = ngx_http_conf_get_module_main_conf (cf, Ngx_http_core_module);

h = Ngx_array_push (&cmcf->phases[ngx_http_access_phase].handlers);
if (h = = NULL) {
return ngx_error;
}

*h = Ngx_http_access_handler;

return NGX_OK;
And Ngx_http_access_handler is the handler inserted in the Ngx_http_access_phase. There are a total of several stages in the process of HTTP processing: typedef enum {
Ngx_http_post_read_phase = 0,

Ngx_http_server_rewrite_phase,

Ngx_http_find_config_phase,
Ngx_http_rewrite_phase,
Ngx_http_post_rewrite_phase,

Ngx_http_preaccess_phase,

Ngx_http_access_phase,
Ngx_http_post_access_phase,

Ngx_http_try_files_phase,
Ngx_http_content_phase,

Ngx_http_log_phase
} ngx_http_phases; If you feel that phases is not enough, you can join your own stage in the Nginx code. This processing method, can be in Nginx as a proxy forwarding, add their own handler to do some processing, quite practical. Different stages of processing methods, the specific method can refer to the corresponding checker function in ngx_http_core_module.c.

Nginx HTTP Module Development

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.