The sample code in the "Nginx Source Learning and use" series blog is on csdn code hosting server, address Https://code.csdn.net/u012819339/nginx_study, you can download it locally, Or use Git to get updates in real time
This blog will simply show the development of a simple module on Nginx to handle the entire process of the request, and give an example. As for the details, you need more children's shoes to reference other materials, a lot of understanding to Create your own code directory
Create a new directory MyModule under Nginx's SRC directory, creating two file Config files and ngx_http_mytest_module.c file config in the directory
Config contents and explanations are as follows:
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"
Ngx_addon_name: is the name of the module, which is only used when configure executes
http_modules: Save all HTTP module names in Nginx, separated by spaces between names
Ngx_addon_srcs: Specifies the source code for the compiled module
Ngx_addon_dir: equivalent to executing configure when –add-module=path's Path parameter writes its own module source code
NGX_HTTP_MYTEST_MODULE.C source code is as follows:
#include <ngx_config.h> #include <ngx_core.h> #include <ngx_event.h> #include <ngx_http.h>// Methods for handling user requests static ngx_int_t Ngx_http_mytest_handler (ngx_http_request_t *r) {//Set MyTest module handles only get or HEAD requests if (!) (! R->method & (ngx_http_get|
Ngx_http_head)) return ngx_http_not_allowed;
The package body in the discard request ngx_int_t rc = Ngx_http_discard_request_body (r);
if (RC!= NGX_OK) return RC;
ngx_str_t type = ngx_string ("Text/plain");
ngx_str_t respone = ngx_string ("Hello World, Arvik test!");
R->headers_out.status = NGX_HTTP_OK;
R->headers_out.content_length_n = Respone.len;
R->headers_out.content_type = type;
Send HTTP Header rc = Ngx_http_send_header (r);
if (rc = = Ngx_error | | rc > NGX_OK | | | r->header_only) {return rc;
}//constructs the ngx_buf_t structure body to prepare to send the package body ngx_buf_t *b;
b = Ngx_create_temp_buf (R->pool, Respone.len);
if (b = = NULL) return ngx_http_internal_server_error; Ngx_memcpy (B->pos, Respone.data, Respone.len);
B->last = B->pos + Respone.len;
B->last_buf = 1;
Construct ngx_chain_t pass to filter to use ngx_chain_t out;
Out.buf = b;
Out.next = NULL;
Return Ngx_http_output_filter (R, &out);
Static char *ngx_http_mytest (ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {ngx_http_core_loc_conf_t *clcf;
CLCF = ngx_http_conf_get_module_loc_conf (cf, Ngx_http_core_module);
Clcf->handler = Ngx_http_mytest_handler;
return NGX_CONF_OK; } static ngx_command_t ngx_http_mytest_commands[] = {{ngx_string ("mytest"), ngx_http_main_conf | N gx_http_srv_conf | ngx_http_loc_conf | ngx_http_lmt_conf | Ngx_conf_noargs, Ngx_http_mytest, Ngx_http_loc_conf_offset, 0, NULL}, Ngx_null_co
Mmand};
The 8 stages described in the module interface, function pointers can be null static ngx_http_module_t Ngx_http_mytest_module_ctx = {null, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,}; Ngx_module_t Ngx_http_mytest_module = {ngx_module_v1, &ngx_http_mytest_module_ctx, Ngx_http_mytest_commands,
Ngx_http_module, NULL, NULL, NULL, NULL, NULL, NULL, NULL, ngx_module_v1_padding};
Generate Makefile
Execute the following command:
./configure--add-module=./src/mymodule/
Compile and install Nginx source code
#make
#make Install
After the compilation is installed, its own module is embedded in the Nginx module. Modify Configuration
Open/usr/local/nginx/conf/nginx.conf File
Add a few lines of code to the corresponding position as follows:
server {
listen ;
server_name localhost;
#charset Koi8-r;
#access_log logs/host.access.log main;
Location/{
root html;
Index index.html index.htm;
}
The following 3 lines add content
location/test {
mytest
}
#error_page 404 /404.html;
# REDIRECT Server error pages to the static page/50x.html
#
error_page 502 503 504 />location =/50x.html {
root html;
}
start Nginx
/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf
Run Results
Local browser address input http://localhost/test, the result is as follows:
The code has been stored and uploaded to this location https://code.csdn.net/u012819339/nginx_study, in the project's Test9 directory, children's shoes can download the demo