Nginx defines its own HTTP module

Source: Internet
Author: User
Tags data structures

Transfer from: http://blog.csdn.net/u012243115/article/details/46898573 config file writing

Nginx provides a simple way to compile third party modules into Nginx. First, put the source code files all in one directory, while writing a file in the directory to inform Nginx how to compile this module, the file name must be config. Then, add the parameter--add-module=path (the new module source code and config file storage directory) to the Configure script execution, and the new module can be added before compiling the Nginx and then compiled (make).

Config file format

Config file is actually an executable shell script, if you only want to develop an HTTP module, you need to define three variables:

(1) Ngx_adon_name

Used only when configure is executing, typically set to the module name.

(2) Http_modules

Saves all the HTTP module names. Each module is connected by a space. When you reset this variable, do not overwrite it directly, so set the following:

"$HTTP _modulesngx_http_mytest_module"

(3) Ngx_addon_srcs

Used to specify the source code for the new module, and several source codes to be compiled can be connected by spaces.

Note that you can use the $ngx_addon_dir variable when setting this variable, which is equivalent to the path parameter--add-module=path when configure executes.

The config file in this example reads as follows:

[CPP] View plain copy ngx_addon_name=ngx_http_mytest_module http_modules= "$HTTP _modules ngx_http_mytest_module" NGX_ addon_srcs= "$NGX _addon_srcs $ngx _addon_dir/ngx_http_mytest_module.c"

Ii. defining HTTP modules and handling user requests

Use C language to write module source code ngx_http_mytest_module.c. The book introduces a few data structures and functions, after combing, their relationship is this:


They complete the task of the specific process and coding to achieve the original book (some data transfer, such as HTTP request parsing, data delivery is done by the Nginx HTTP framework, not reflected in the module code), remember to add header files Ngx_config.h, ngx_core.h and Ngx_ Http.h:

The ngx_http_mytest_module.c file (located in the same directory as the config file above) is as follows:

Source: http://nginx.weebly.com/31034203632830430721.html

[cpp]  View plain  copy #include  <ngx_config.h>   #include  <ngx_core.h>    #include  <ngx_http.h>         static char *    Ngx_http_mytest (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);      Static ngx_int_t ngx_http_mytest_handler (ngx_http_request_t *r);         //Processing configuration Items      Static ngx_command_t  ngx_http_mytest_commands [] =   {          {            ngx_string ("MyTest"),           ngx_ http_main_conf | ngx_http_srv_conf | ngx_http_loc_conf | ngx_http_lmt_conf  | ngx_conf_noargs,   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;NGX_HTTP_MYTEST,&NBSP;&NBSp         NGX_HTTP_LOC_CONF_OFFSET,            0,           NULL        },          ngx_null_command  };   Module Context    static ngx_http_module_t  ngx_http_mytest_module_ctx =   {        NULL,                                /* preconfiguration */       NULL,                        /*  postconfiguration */          NULL,                                /* create main configuration */       NULL,                               /*  init main configuration */     

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.