Nginx Server module writing and related kernel source code _nginx

Source: Internet
Author: User
Tags http request nginx server

1.nginx Module
first Nginx and Apache the biggest difference is that Nginx module can not dynamically add, need to compile, specify the module path to add, and Nginx source code compiled together.
The processing process of the Nginx module:
A. Client sends HTTP request to Nginx server
B.nginx Select an appropriate processing module based on the location in the configuration file
C. Load Balancing module Select a back-end server (in reverse proxy case)
D. The processing module is processed and the output buffer is placed on the first filter module
E. The first filter module is processed and output to the second filter module
F. Then the second filter module and then to the third filter module
G. Nth Filter Module ...
H. Processing results sent to the client

2.nginx Module Writing
A, creating a module folder

Mkdir-p/opt/nginx_hello_world 
Cd/op/nginx_hello_word 

b, create a module configuration file

Vi/opt/nginx_hello_word/config 

C, create module master file

Vi/opt/nginx_hello_world/ngx_http_hello_world_module.c 

Write the following:

#include <ngx_config.h> 
#include <ngx_core.h> 
#include <ngx_http.h> 
 
 
static char *ngx_ Http_hello_world (ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 

Written HelloWorld module


/* Commands/static ngx_command_t ngx_http_hello_world_commands[] = {ngx_string ("Hello_world"), Ngx_http_loc_c Onf| 
 
Ngx_conf_noargs, Ngx_http_hello_world, 0, 0, NULL}, Ngx_null_command}; 
 
Static U_char ngx_hello_world[] = "Hello World"; Static ngx_http_module_t Ngx_http_hello_world_module_ctx = {NULL,/* Preconfiguration/NULL,/* PO         Stconfiguration */NULL,/* Create main configuration/NULL,/* init main configuration/NULL, /* Create server configuration */NULL,/* Merge server Configuration/NULL,/* Create Locati 
On configuration */NULL/* Merge location configuration */}; * * Hook/ngx_module_t ngx_http_hello_world_module = {NGX_MODULE_V1, &ngx_http_hello_world_module_ctx, * mo Dule context */ngx_http_hello_world_commands,/* Module directives/ngx_http_module,/* Module type */N        ull,/* init master */NULL, /* init module/NULL,/* INIT process */NULL,/* init thread/NULL,/* exit thread */NUL 
L,/* exit process/NULL,/* Exit master/ngx_module_v1_padding}; 
 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; 
 /* Http Output Buffer */r->headers_out.content_type.len = sizeof ("Text/plain")-1; 
  
 R->headers_out.content_type.data = (U_char *) "Text/plain"; 
  
 b = Ngx_pcalloc (R->pool, sizeof (ngx_buf_t)); 
 Out.buf = b; 
  
 Out.next = NULL; 
 B->pos = Ngx_hello_world; 
 B->last = Ngx_hello_world + sizeof (Ngx_hello_world); 
 B->memory = 1; 
  
 B->last_buf = 1; 
 R->headers_out.status = NGX_HTTP_OK; 
 R->headers_out.content_length_n = sizeof (Ngx_hello_world); 
  
 Ngx_http_send_header (R); 
Return Ngx_http_output_filter (R, &out); } Static char * Ngx_http_hello_world (ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {ngx_http_core_loc_conf_t *CLCF; 
 /* Register Hanlder */CLCF = ngx_http_conf_get_module_loc_conf (cf, Ngx_http_core_module); 
 Clcf->handler = Ngx_http_hello_world_handler; 
return NGX_CONF_OK; 
 }

D, download the Nginx source package, I downloaded is nginx-1.0.13.tar.gz
Note here to first verify that the HelloWorld module is ready, nginx whether it can be successfully compiled independently, and whether all the required modules are installed.
Compile the Nginx with the HelloWorld module:

./configure--prefix=/usr/local/nginx--add-module=/opt/nginx_hello_world/make make 
install 

E, configure nginx.conf

location=/hello { 
 hello_world; 
} 

F, start Nginx, Access Http://localhost/hello, you can see the written HelloWorld module output text.

3.hello World Module Analysis
the a.ngx_command_t function is used to define a static array containing module directives Ngx_http_hello_world_commands

Static ngx_command_t ngx_http_hello_world_commands[] = { 
 ngx_string ("Hello_world"),//Set instruction name string, note cannot contain spaces, Data type ngx_str_t will be explained in detail later. 
  ngx_http_loc_conf| Ngx_conf_noargs,//Configure the legal location of the directive, which means that the location part is legal and the instruction has no parameters. 
  ngx_http_hello_world,//callback function, three parameters (ngx_conf_t *cf,ngx_command_t *cmd, void *conf) 
  0,//The parameters behind it to be excavated, I haven't used 
  it yet. 0, 
  NULL}, 
 Ngx_null_command 
}; 

B.static U_char ngx_hello_world[] = "Hello World" is a string output to the screen.
c.ngx_http_module_t is used to define the structural body Ngx_http_hello_world_module_ctx:

Static ngx_http_module_t Ngx_http_hello_world_module_ctx = { 
 NULL,/         * call/NULL before the read- 
 in configuration,/          * called after the read-in configuration */null,/         * Call/NULL when creating a global partial configuration 
 , * call/NULL when         initializing the configuration of the global part 
 ,/* Call         when 
 creating the configuration of the virtual host part/* NULL,         /* call/NULL when merging with global partial configuration,/* Call/NULL when         creating configuration of Location Part * * * * *         with host part configuration merge/ 
}; 

d.ngx_module_t defines the structural body Ngx_http_hello_world_module

ngx_module_t ngx_http_hello_world_module = { 
 ngx_module_v1, 
 &ngx_http_hello_world_module_ctx,    * Module context */ 
 ngx_http_hello_world_commands,/     * Module directives/ 
 Ngx_http_module,/      * module Type */null,/         * init master/ 
 NULL,/         * init module/ 
 null, 
 /* init process */null,< c15/>/* init thread */null,/         * Exit thread/NULL,/*    exit process/ 
 NULL,/         * Exit Maste R * 
/ngx_module_v1_padding}; 

E. Processing functions, Ngx_http_hello_world_handler, is also a core part of the Hello World module.

Static ngx_int_t 
Ngx_http_hello_world_handler (ngx_http_request_t *r)//ngx_http_request_t *r 
// can access to the client's head and soon to be sent back to the head 
{ 
 ngx_int_t  RC; 
 ngx_buf_t *b; 
 Ngx_chain_t out; 
 /* Http Output Buffer * 
 /R->headers_out.content_type.len = sizeof ("Text/plain")-1; 
 R->headers_out.content_type.data = (U_char *) "Text/plain"; 
  
 b = Ngx_pcalloc (R->pool, sizeof (ngx_buf_t)); 
  
 Out.buf = b; 
 Out.next = NULL; 
  
 B->pos = Ngx_hello_world; 
 B->last = Ngx_hello_world + sizeof (ngx_hello_world); 
 B->memory = 1; 
 B->last_buf = 1; 
  
 R->headers_out.status = NGX_HTTP_OK; 
 R->headers_out.content_length_n = sizeof (Ngx_hello_world); 
 Ngx_http_send_header (r); 
  
 Return Ngx_http_output_filter (R, &out); 
 

The most important data in the HelloWorld module is the ngx_module_t pointer array, which contains all the modules supported by the current compiled version, which defines the objs/ngx_modules.c of the actual automatic script generation, as follows:

 extern ngx_module_t Ngx_core_module; 
 extern ngx_module_t Ngx_errlog_module; 
 extern ngx_module_t Ngx_conf_module; 
 extern ngx_module_t Ngx_events_module; 
 extern ngx_module_t Ngx_event_core_module; 
 extern ngx_module_t Ngx_epoll_module; 
 extern ngx_module_t Ngx_http_module; 
 extern ngx_module_t Ngx_http_core_module; 
 extern ngx_module_t Ngx_http_log_module; 
 extern ngx_module_t Ngx_http_upstream_module; 
 extern ngx_module_t Ngx_http_static_module; 
 extern ngx_module_t Ngx_http_autoindex_module; 
 extern ngx_module_t Ngx_http_index_module; 
 extern ngx_module_t Ngx_http_auth_basic_module; 
 extern ngx_module_t Ngx_http_access_module; 
 extern ngx_module_t Ngx_http_limit_zone_module; 
 extern ngx_module_t Ngx_http_limit_req_module; 
 extern ngx_module_t Ngx_http_geo_module; 
 extern ngx_module_t Ngx_http_map_module; 
 extern ngx_module_t Ngx_http_split_clients_module; 
 extern ngx_module_t Ngx_http_referer_module; 
 extern ngx_module_t Ngx_http_rewrite_module; extern ngx_module_t Ngx_http_proxy_module; 
 extern ngx_module_t Ngx_http_fastcgi_module; 
 extern ngx_module_t Ngx_http_uwsgi_module; 
 extern ngx_module_t Ngx_http_scgi_module; 
 extern ngx_module_t Ngx_http_memcached_module; 
 extern ngx_module_t Ngx_http_empty_gif_module; 
 extern ngx_module_t Ngx_http_browser_module; 
 extern ngx_module_t Ngx_http_upstream_ip_hash_module; 
 extern ngx_module_t Ngx_http_cache_purge_module; 
 extern ngx_module_t Ngx_http_write_filter_module; 
 extern ngx_module_t Ngx_http_header_filter_module; 
 extern ngx_module_t Ngx_http_chunked_filter_module; 
 extern ngx_module_t Ngx_http_range_header_filter_module; 
 extern ngx_module_t Ngx_http_gzip_filter_module; 
 extern ngx_module_t Ngx_http_postpone_filter_module; 
 extern ngx_module_t Ngx_http_ssi_filter_module; 
 extern ngx_module_t Ngx_http_charset_filter_module; 
 extern ngx_module_t Ngx_http_userid_filter_module; 
 extern ngx_module_t Ngx_http_headers_filter_module; extern ngx_module_t Ngx_http_copy_filter_moDule; 
 extern ngx_module_t Ngx_http_range_body_filter_module; 
  
 extern ngx_module_t Ngx_http_not_modified_filter_module; ngx_module_t *ngx_modules[] = {&ngx_core_module, &ngx_errlog_module, &ngx_conf_module, &ngx_e Vents_module, &ngx_event_core_module, &ngx_epoll_module, &ngx_http_module, &ngx_http_core_modu Le, &ngx_http_log_module, &ngx_http_upstream_module, &ngx_http_static_module, &ngx_http_autoin Dex_module, &ngx_http_index_module, &ngx_http_auth_basic_module, &ngx_http_access_module, &AMP;NGX _http_limit_zone_module, &ngx_http_limit_req_module, &ngx_http_geo_module, &ngx_http_map_module, & Amp;ngx_http_split_clients_module, &ngx_http_referer_module, &ngx_http_rewrite_module, &ngx_http_prox Y_module, &ngx_http_fastcgi_module, &ngx_http_uwsgi_module, &ngx_http_scgi_module, &ngx_http_m 
 Emcached_module, &ngx_http_empty_gif_module, &ngx_http_browser_module, &ngx_http_upstream_ip_hash_module, &ngx_ht Tp_cache_purge_module, &ngx_http_write_filter_module, &ngx_http_header_filter_module, &ngx_http_chunk Ed_filter_module, &ngx_http_range_header_filter_module, &ngx_http_gzip_filter_module, &AMP;NGX_HTTP_POSTP One_filter_module, &ngx_http_ssi_filter_module, &ngx_http_charset_filter_module, &ngx_http_userid_fil Ter_module, &ngx_http_headers_filter_module, &ngx_http_copy_filter_module, &ngx_http_range_body_filte  R_module, &ngx_http_not_modified_filter_module, NULL};

There are only declarations for each module variable, and each module's definition is contained within its own module file, such as Ngx_core_module definition in SRC/CORE/NGINX.C:

ngx_module_t ngx_core_module = { 
 ngx_module_v1, 
 &ngx_core_module_ctx,/     * Module Context */ 
 Ngx_ Core_commands,/      * Module directives/ 
 Ngx_core_module,/      * Module Type */ 
 NULL,/         * init master */< C10/>null,/         * INIT module */NULL,/* 
 init process/null,/         * init thread/ 
 null,         /* Exit thread */NULL,/* Exit         process/ 
 NULL,/         * Exit Master */ 
 ngx_module_v1_padding 
}; 

Is it very similar to HelloWorld inside, yes, they are all modules, the only difference is that HelloWorld is added to you.
To the present position is only to explore the Nginx module, finally to mention a drawing of other people's Nginx module diagram, to help the next study.

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.