Nginx to write the HTTP test module in C language

Source: Internet
Author: User

1. Create a Module catalog

Mkdir/work/nginx/modules/mytest

2. Create two files in this directory config and ngx_http_mytest_module.c

Touch Config ngx_http_mytest_module.c

3. The code is as follows

(1) Config file code

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"

(2) NGX_HTTP_MYTEST_MODULE.C code

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

Static ngx_int_t Ngx_http_mytest_handler (ngx_http_request_t *r);
Static char * ngx_http_mytest (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

Defining the processing of module configuration files
Static ngx_command_t ngx_http_mytest_commands[] = {
{//config item name
Ngx_string ("MyTest"),
A configuration item type that defines where he can appear
Ngx_http_main_conf| Ngx_http_srv_conf| Ngx_http_loc_conf| Ngx_http_lmt_conf| Ngx_conf_noargs,
Functions that handle configuration item parameters, which are defined below
Ngx_http_mytest,
Offsets in the configuration file
Ngx_http_loc_conf_offset,
Pre-defined parsing method configuration items
0,
How to handle a configuration item after reading
Null
},
Command array to end with Ngx_null_command
#define Ngx_null_command {Ngx_null_string,0,null,0,0,null}
Ngx_null_command
};


MyTest the module context, which is null to say that there is nothing to do when the HTTP framework is initialized
Static ngx_http_module_t Ngx_http_mytest_module_ctx = {
NULL,//preconfiguration
NULL,//postconfiguration
NULL,//create main configuration
NULL,//init main configuration
NULL,//create server configuration
NULL,//merge server configuration
NULL,//create location configuration
NULL//merge Location Configuration
};
The definition of your own mytest module is added to the global Ngx_modules array at compile time, so that all initialization methods of the module are called when Nginx initializes (the ngx_http_mytest_ of the ngx_http_module_t type above). MODULE_CTX)

ngx_module_t Ngx_http_mytest_module = {
NGX_MODULE_V1,//a macro defined by Nginx to initialize the first seven members
&ngx_http_mytest_module_ctx,//The context structure of the module, public methods that point to a specific module
Ngx_http_mytest_commands,//structure array for processing configuration items
Ngx_http_module,//module type
Nginx function pointers for seven execution points during start-stop
Null
Null
Null
Null
Null
Null
Null

ngx_module_v1_padding//definition of a macro defined by Nginx defines the remaining 8 reserved fields
};

The callback function that corresponds to the configuration item that will be called when the MyTest configuration item appears in the configuration item
Static char * ngx_http_mytest (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{//CKCF does not refer to a data structure within a particular location block, but can be a mian, SRV, loc-level configuration item
Each http{},sever{},location{} has a ngx_http_core_loc_conf_t type of data structure
ngx_http_core_loc_conf_t *CLCF;

Locate the configuration block where the MyTest configuration item resides
CLCF = ngx_http_conf_get_module_loc_conf (cf, Ngx_http_core_module);

The HTTP framework processes the user request to the Ngx_http_content_phase stage, if the requested host name, the URI matches the configuration block where the configuration item is located, calls the
The handle method in CLCF handles this request
Ngx_http_content_phase is used to process the content of HTTP requests, which is the phase in which most HTTP modules are usually involved
Clcf->handler = Ngx_http_mytest_handler;

return NGX_CONF_OK;
}

callback function to actually complete the processing
Static ngx_int_t Ngx_http_mytest_handler (ngx_http_request_t *r)
{
Request method
if (! ( R->method & (Ngx_http_get | Ngx_http_head)) {
return ngx_http_not_allowed;
}
Do not process the requested package body and discard it directly. But this step is not to be omitted, he is a way to accept the package body, but simply discarded,
If it is not accepted, the client may attempt to send the package body again, and the server will not accept it, causing the client to send a timeout
ngx_int_t rc = Ngx_http_discard_request_body (r);
if (rc = NGX_OK) {
return RC;
}
Construct the response header
ngx_str_t type = ngx_string ("Text/plain");
ngx_str_t response = ngx_string ("Hello World!") \n\rthis is my first Nginx module test! ");
R->headers_out.status = NGX_HTTP_OK;
R->headers_out.content_length_n = Response.len;
R->headers_out.content_type = type;
Sends the HTTP header, which also includes the response line
rc = Ngx_http_send_header (r);
if (rc = = Ngx_error | | rc > NGX_OK | | r->header_only) {
return RC;
}

ngx_buf_t *b;
Create memory BUF based on the memory pool object coming from the request
b = Ngx_create_temp_buf (R->pool, Response.len);
if (b = = NULL) {
return ngx_http_internal_server_error;
}
Valid content starts from the POS location and copies the contents of the Respon
ngx_memcpy (B->pos, Response.data, Response.len);
Valid content to last end
B->last = B->pos + Response.len;
Because the ngx_buf_t can be linked by a ngx_chain_t chain, LAST_BUF can mark this as the last buffer to be processed, simplifying processing
B->last_buf = 1;
The memory is BUF with a chain, as the Ngx_http_output_filter falls into a parameter
Ngx_chain_t out;
Out.buf = b;
Mark this is the last ngx_chain_t
Out.next = NULL;

Return Ngx_http_output_filter (R, &out);
}

4. Operation in Nginx directory

(1) First configure

./configure--add-module=/work/nginx/modules/mytest

(2) Make && make install

(3) Add the required configuration items in the/usr/local/nginx/conf/nginx.conf configuration file

Location=/nestle {

MyTest

}

(3) Next launch browser

172.15.26.12/nestle

Nginx to write the HTTP test module in C language

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.