nginx 源码学习笔记(一)——初识nginx helloworld模块

来源:互联网 发布:linux jdk1.6下载 编辑:程序博客网 时间:2024/05/17 03:46

参考:http://blog.csdn.net/lengzijian/article/details/7349673

最近看完一本书,而且还是跟我一个学校的人写的关于nginx的内容,个人觉得内容可以更充实点,包括整本书的后半本没有太大的价值,但是总体来说还是不错的哈哈!(是不有点自恋),不费话了,今天记录下我刚刚编写的nginx hello_world模块内容,网上也没有源代码,就完全手敲了,累呀!

 

1.nginx模块

首先nginx和apache最大的不同就是nginx的模块不能够动态添加,需要在编译时,指定要添加的模块路径,与nginx源码一起编译。

nginx模块的处理流程:

a.客户端发送http请求道nginx服务器

b.nginx基于配置文件中的位置选择一个合适的处理模块

c.负载均衡模块选择一台后端服务器(反向代理情况下)

d.处理模块进行处理并把输出缓冲放到第一个过滤模块上

e.第一个过滤模块处理后输出给第二个过滤模块

f.然后第二个过滤模块又到第三个过滤模块

g.第N个过滤模块。。。

h.发处理结果发给客户端

 

2.nginx模块编写

a、创建模块文件夹

[java] view plaincopyprint?
  1. <span style="font-size:16px;">mkdir -p /opt/nginx_hello_world  
  2. cd /op/nginx_hello_word</span>  


b、创建模块配置文件

[java] view plaincopyprint?
  1. <span style="font-size:16px;">vi /opt/nginx_hello_word/config</span>  


写入如下内容:

[java] view plaincopyprint?
  1. <span style="font-size:16px;">ngx_addon_name=ngx_http_hello_world_module  
  2. HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"  
  3. NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"  
  4. CORE_LIBS="$CORE_LIBS -lpcre"</span>  


 

c、创建模块主文件

[java] view plaincopyprint?
  1. <span style="font-size:16px;">vi /opt/nginx_hello_world/ngx_http_hello_world_module.c</span>  


写入如下内容:

[cpp] view plaincopyprint?
  1. <span style="font-size:16px;">#include <ngx_config.h>  
  2. #include <ngx_core.h>  
  3. #include <ngx_http.h>  
  4.   
  5.   
  6. static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);  
  7.   
  8.   
  9. /* Commands */  
  10. static ngx_command_t  ngx_http_hello_world_commands[] = {  
  11.     { ngx_string("hello_world"),  
  12.       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,  
  13.       ngx_http_hello_world,  
  14.       0,  
  15.       0,  
  16.       NULL },  
  17.     ngx_null_command  
  18. };  
  19.   
  20. static u_char ngx_hello_world[] = "hello world";  
  21.   
  22. static ngx_http_module_t  ngx_http_hello_world_module_ctx = {  
  23.     NULL,                                  /* preconfiguration */  
  24.     NULL,                                     /* postconfiguration */  
  25.     NULL,                                  /* create main configuration */  
  26.     NULL,                                  /* init main configuration */  
  27.     NULL,                                  /* create server configuration */  
  28.     NULL,                                  /* merge server configuration */  
  29.     NULL,                                  /* create location configuration */  
  30.     NULL                                   /* merge location configuration */  
  31. };  
  32. /* hook */  
  33. ngx_module_t  ngx_http_hello_world_module = {  
  34.     NGX_MODULE_V1,  
  35.     &ngx_http_hello_world_module_ctx,              /* module context */  
  36.     ngx_http_hello_world_commands,                 /* module directives */  
  37.     NGX_HTTP_MODULE,                       /* module type */  
  38.     NULL,                                  /* init master */  
  39.     NULL,                                  /* init module */  
  40.     NULL,             /* init process */  
  41.     NULL,                                  /* init thread */  
  42.     NULL,                                  /* exit thread */  
  43.     NULL,             /* exit process */  
  44.     NULL,                                  /* exit master */  
  45.     NGX_MODULE_V1_PADDING  
  46. };  
  47. static ngx_int_t  
  48. ngx_http_hello_world_handler(ngx_http_request_t *r)  
  49. {  
  50.     ngx_int_t     rc;  
  51.     ngx_buf_t    *b;  
  52.     ngx_chain_t   out;  
  53.     /* Http Output Buffer */  
  54.     r->headers_out.content_type.len = sizeof("text/plain") - 1;  
  55.     r->headers_out.content_type.data = (u_char *) "text/plain";  
  56.       
  57.     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));  
  58.       
  59.     out.buf = b;  
  60.     out.next = NULL;  
  61.       
  62.     b->pos = ngx_hello_world;  
  63.     b->last = ngx_hello_world + sizeof(ngx_hello_world);  
  64.     b->memory = 1;  
  65.     b->last_buf = 1;  
  66.       
  67.     r->headers_out.status = NGX_HTTP_OK;  
  68.     r->headers_out.content_length_n = sizeof(ngx_hello_world);  
  69.     ngx_http_send_header(r);  
  70.       
  71.     return ngx_http_output_filter(r, &out);  
  72. }  
  73. static char *  
  74. ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  75. {  
  76.     ngx_http_core_loc_conf_t *clcf ;  
  77.     /* register hanlder */  
  78.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);  
  79.     clcf->handler = ngx_http_hello_world_handler;  
  80.     return NGX_CONF_OK;  
  81. }  
  82. </span>  


 

d、下载nginx源码包,我下载的是nginx-1.0.13.tar.gz

这里注意在编译helloworld模块前首先确认,nginx是否可以独立编译成功,是否安装了所需的所有模块

与helloworld模块一起编译nginx:

[java] view plaincopyprint?
  1. <span style="font-size:16px;">./configure --prefix=/usr/local/nginx --add-module=/opt/nginx_hello_world/  
  2. make  
  3. make install</span>  


 

e、配置nginx.conf

[java] view plaincopyprint?
  1. <span style="font-size:16px;">location= /hello {  
  2.     hello_world;  
  3. }</span>  


 

f、启动nginx,访问http://localhost/hello ,可以看到编写的helloworld模块输出的文字。

 

3.hello world模块分析

a.ngx_command_t函数用于定义包含模块指令的静态数组ngx_http_hello_world_commands

[cpp] view plaincopyprint?
  1. <span style="font-size:16px;">static ngx_command_t  ngx_http_hello_world_commands[] = {  
  2.     { ngx_string("hello_world"),  //设置指令名称字符串,注意不能包含空格,数据类型ngx_str_t之后会详细讲解。  
  3.       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,这里表示:location部分合法,并且指令没有参数。  
  4.       ngx_http_hello_world,//回调函数,三个参数(ngx_conf_t *cf,ngx_command_t *cmd, void *conf)  
  5.       0,//后面的参数有待发掘,我还没有用到  
  6.       0,  
  7.       NULL },  
  8.     ngx_null_command  
  9. };</span>  

b.static u_char ngx_hello_world[] ="hello world" 则是输出到屏幕的字符串。

c.ngx_http_module_t用来定义结构体ngx_http_hello_world_module_ctx:

[cpp] view plaincopyprint?
  1. <span style="font-size:16px;">static ngx_http_module_t  ngx_http_hello_world_module_ctx = {  
  2.     NULL,                                  /* 读入配置前调用*/  
  3.     NULL,                                     /* 读入配置后调用*/  
  4.     NULL,                                  /* 创建全局部分配置时调用 */  
  5.     NULL,                                  /* 初始化全局部分的配置时调用*/  
  6.     NULL,                                  /* 创建虚拟主机部分的配置时调用*/  
  7.     NULL,                                  /* 与全局部分配置合并时调用 */  
  8.     NULL,                                  /* 创建位置部分的配置时调用 */  
  9.     NULL                                   /* 与主机部分配置合并时调用*/  
  10. };</span>  

d.ngx_module_t定义结构体ngx_http_hello_world_module

[cpp] view plaincopyprint?
  1. <span style="font-size:16px;">ngx_module_t  ngx_http_hello_world_module = {  
  2.     NGX_MODULE_V1,  
  3.     &ngx_http_hello_world_module_ctx,              /* module context */  
  4.     ngx_http_hello_world_commands,                 /* module directives */  
  5.     NGX_HTTP_MODULE,                       /* module type */  
  6.     NULL,                                  /* init master */  
  7.     NULL,                                  /* init module */  
  8.     NULL,             /* init process */  
  9.     NULL,                                  /* init thread */  
  10.     NULL,                                  /* exit thread */  
  11.     NULL,             /* exit process */  
  12.     NULL,                                  /* exit master */  
  13.     NGX_MODULE_V1_PADDING  
  14. };</span>  


 

他包含有模块的主要内容和指令的执行部分,下一节会详细讲解。

e.处理函数,ngx_http_hello_world_handler,也是hello world 模块的核心部分。

[cpp] view plaincopyprint?
  1. <span style="font-size:16px;">static ngx_int_t  
  2. ngx_http_hello_world_handler(ngx_http_request_t *r)//ngx_http_request_t *r 可以访问到客户端的头部和不久要发送的回复头部  
  3. {  
  4.     ngx_int_t     rc;  
  5.     ngx_buf_t    *b;  
  6.     ngx_chain_t   out;  
  7.     /* Http Output Buffer */  
  8.     r->headers_out.content_type.len = sizeof("text/plain") - 1;  
  9.     r->headers_out.content_type.data = (u_char *) "text/plain";  
  10.       
  11.     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));  
  12.       
  13.     out.buf = b;  
  14.     out.next = NULL;  
  15.       
  16.     b->pos = ngx_hello_world;  
  17.     b->last = ngx_hello_world + sizeof(ngx_hello_world);  
  18.     b->memory = 1;  
  19.     b->last_buf = 1;  
  20.       
  21.     r->headers_out.status = NGX_HTTP_OK;  
  22.     r->headers_out.content_length_n = sizeof(ngx_hello_world);  
  23.     ngx_http_send_header(r);  
  24.       
  25.     return ngx_http_output_filter(r, &out);  
  26. }</span>  


0 0
原创粉丝点击