nginx的helloworld模块的helloworld

来源:互联网 发布:python初学者书籍推荐 编辑:程序博客网 时间:2024/05/22 08:20

http://haoningabc.iteye.com/blog/1283098

经典的nginx的helloworld尝试了一下
过程就是

Java代码 复制代码 收藏代码
  1. nginx--->config文件---->module--->command[]<----->函数----->handler   
  2.                           |           |   
  3.                          ctx等        位置等  


函数就是这个ngx_http_hello_world,和command[]互相指


自己加的注释
ngx_http_hello_world_module.c
-----------------------
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_buf.h>
static  char *ngx_http_hello_world(ngx_conf_t *cf ,ngx_command_t *cmd,void *conf);
//*cf 指向ngx_conf_t 结构体指针,从指令后面传过来的参数
//*cmd 指向当前结构体ngx_command_t 的指针(互相指)
//*conf指向自定义模块配置结构体的指针
static ngx_command_t ngx_http_hello_world_commands[]={
        {
                ngx_string("hello_world"),//指令名称,nginx.conf中使用
                NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,  //注释1
                ngx_http_hello_world,//回调函数 ,上面定义的带三个参数
                0,//保持的值放的位置:全局,server,location
                0,//指令的值保存的位置
                NULL //一般都为NULL
        },
        ngx_null_command  //读入ngx_null_command 指令后停止
};
static u_char ngx_hello_world[]="hello world";
//ngx_http_<module name>_module_ctx用于创建和合并三个配置
//参考http/ngx_http_config.h
static ngx_http_module_t ngx_http_hello_world_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 localtion configuration
};
//nginx进程,线程相关,ngx_http_<module name>_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, //init thread
        NULL, //exit thread
        NULL, //exit process
        NULL, //exit master
        NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
        ngx_buf_t *b;
        ngx_chain_t out;

        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);
}
//回调函数,1获得location中的“核心”结构体,2为他分配个处理函数
static char *ngx_http_hello_world(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_hello_world_handler;
        return NGX_CONF_OK;
}
---------------------------
config:

Java代码 复制代码 收藏代码
  1. 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"  


config和c文件放到同一个目录下,比如/opt/nginxtest,nginx-1.0.4解压到/opt/nginxtest/nginx
然后编译nginx
./configure --help|grep debug
./configure --prefix=/opt/nginxtest/nginx --add-module=/opt/nginxtest/nginx_hello_world/
或./configure --with-debug --prefix=/opt/nginxtest/nginx --add-module=/opt/nginxtest/nginx_hello_world/

在objs/下生成Makefile之后
修改

Java代码 复制代码 收藏代码
  1. CC =    gcc -g  


让他支持gdb看代码
make
make install

配置nginx.conf
location /hello {
    hello_world;
}
启动nginx
curl http://localhost/hello
然后hello world出来了,牛b了


Java代码 复制代码 收藏代码
  1. 注释1:   
  2. NGX_HTTP_MAIN_CONF  指令出现在全局位置部分是合法的   
  3. NGX_HTTP_SRV_CONF   指令出现在server主机配置部分是合法的   
  4. NGX_HTTP_LOC_CONF   指令出现在Location配置部分是合法的   
  5. NGX_HTTP_UPS_CONF   指令出现在upstream配置部分是合法的   
  6. NGX_CONF_NOARGS     指令没有参数   
  7. NGX_CONF_TAKE1      指令读一个参数   
  8. 。。。。。   
  9. NGX_CONF_TAKE7      指令读7个参数   
  10. NGX_CONF_FLAG       指令读一个布尔型数据   
  11. NGX_CONF_1MORE      指令至少读一个参数   
  12. NGX_CONF_2MORE      指令至少读2个参数  






ldd命令查看依赖的动态库
root@red54apple objs]# ldd nginx
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003b6ee00000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003b80400000)
        libpcre.so.0 => /usr/local/lib/libpcre.so.0 (0x00002aadbeab3000)
        libcrypto.so.6 => /lib64/libcrypto.so.6 (0x0000003291200000)
        libz.so.1 => /lib64/libz.so.1 (0x0000003290a00000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003b6e200000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003b6de00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003b6ea00000)
[root@red54apple objs]#

-------------调试--------
gdb -d /opt/nginxtest/nginx/sbin/ nginx 10540
l就能看代码了

----------ltrace 和strace-------------
strace -T -c -p 10700
curl localhost/hello
ctl+c 看epoll调用次数

Java代码 复制代码 收藏代码
  1. [root@red54apple sbin]# strace -T -c -p 10700  
  2. Process 10700 attached - interrupt to quit   
  3. Process 10700 detached   
  4. % time     seconds  usecs/call     calls    errors syscall   
  5. ------ ----------- ----------- --------- --------- ----------------   
  6. 100.00    0.000060          60         1           write   
  7.   0.00    0.000000           0         1           close   
  8.   0.00    0.000000           0         1           ioctl   
  9.   0.00    0.000000           0         1           writev   
  10.   0.00    0.000000           0         1           accept   
  11.   0.00    0.000000           0         3         1 recvfrom   
  12.   0.00    0.000000           0         1           setsockopt   
  13.   0.00    0.000000           0         3           epoll_wait   
  14.   0.00    0.000000           0         1           epoll_ctl   
  15. ------ ----------- ----------- --------- --------- ----------------   
  16. 100.00    0.000060                    13         1 total   
  17. [root@red54apple sbin]# ldconfig -p |grep mysql  


----------------------
ltrace -p 10700   注意这里用worker进程,master木有
curl localhost/hello
得到

Java代码 复制代码 收藏代码
  1. [root@red54apple sbin]# ltrace -p 10700  
  2. __errno_location()                                                                                           = 0x2b8699230640  
  3. gettimeofday(0x7fff5219a250, NULL)                                                                           = 0  
  4. localtime_r(0x7fff5219a1b80x7fff5219a2a08400x6c7d27)                                                 = 0x7fff5219a2a0  
  5. epoll_wait(80xa6076105120xffffffff0x6c8b48  
  6. )                                                          = 1  
  7. gettimeofday(0x7fff5219a250, NULL)                                                                           = 0  
  8. localtime_r(0x7fff5219a1b80x7fff5219a2a08400x6c7d45)                                                 = 0x7fff5219a2a0  
  9. accept(60x7fff5219a2500x7fff5219a2c00xa6212500x6c8b62)                                               = 3  
  10. memset(0x2b869943c180'\000'184)                                                                          = 0x2b869943c180  
  11. memset(0xa621320'\000'104)                                                                               = 0xa621320  
  12. memset(0xa63b330'\000'104)                                                                               = 0xa63b330  
  13. posix_memalign(0x7fff5219a1b0162560xa63b330104)                                                      = 0  
  14. ioctl(3215370x7fff5219a1fc)                                                                              = 0  
  15. epoll_ctl(8130x7fff5219a1600xa613718)                                                                = 0  
  16. epoll_wait(80xa607610512600000xa613718)                                                              = 1  
  17. gettimeofday(0x7fff5219a250, NULL)                                                                           = 0  
  18. malloc(1256)                                                                                                 = 0xa608e20  
  19. posix_memalign(0x7fff5219a170162562563)                                                              = 0  
  20. malloc(1024)                                                                                                 = 0xa609310  
  21. posix_memalign(0x7fff5219a2401640961443)                                                             = 0  
  22. recv(30xa609310102403)                                                                               = 158  
  23. strncmp("penSSL/0.9.8b zlib/1.2.3 libidn/"..., "pera"4)                                                    = -4  
  24. strncmp("SL/0.9.8b zlib/1.2.3 libidn/0.6."..., "afari/"6)                                                  = -14  
  25. strncmp("L/0.9.8b zlib/1.2.3 libidn/0.6.5""afari/"6)                                                     = -21  
  26. writev(30x7fff5219965020x7fff521996500xa60a1cd)                                                      = 159  
  27. write(4"127.0.0.1 - - [29/Nov/2011:15:21"..., 170)                                                         = 170  
  28. free(0xa609720)                                                                                              = <void>   
  29. free(0xa608e20)                                                                                              = <void>   
  30. free(0xa609310)                                                                                              = <void>   
  31. setsockopt(3610x7fff52199fe44)                                                                       = 0  
  32. malloc(1024)                                                                                                 = 0xa608e20  
  33. __errno_location()                                                                                           = 0x2b8699230640  
  34. recv(30xa608e20102403)                                                                               = 0  
  35. close(3)                                                                                                     = 0  
  36. free(0xa608e20)                                                                                              = <void>   
  37. free(0xa613680)                                                                                              = <void>   
  38. free(0xa5fe390)                                                                                              = <void>   
  39. epoll_wait(80xa6076105120xffffffff0  
  40.   
  41.  <unfinished ...>   
  42. [root@red54apple sbin]#   


这下牛b了吧

然后就该看这个了
http://www.evanmiller.org/nginx-modules-guide.html

顺便统计下代码行数
find . -name "*.c" |xargs cat|wc -l
find . -name "*.c" |xargs cat|grep -v ^$|wc -l
8万有效代码,精读吧,恩,好的


更多需要参考
http://search.cpan.org/~agent/
http://agentzh.org/#Presentations
agentzh.blogspot.com
zhangyichun那个大牛

微薄叫agentzh
http://www.codinglabs.org/html/intro-of-nginx-module-development.html
echo模块
http://openresty.org/
多模块的开发

视频和ppt
http://agentzh.org/misc/slides/perl-lz-apps/#26

Java代码 复制代码 收藏代码
  1.   

http://t.cn/S4WEFG

Java代码 复制代码 收藏代码
  1. upstream main_db {   
  2.        drizzle_server 127.0.0.1:3306  
  3.            user=monty password=some_pass dbname=test   
  4.            protocol=mysql;   
  5.     
  6.        drizzle_keepalive max=10 overflow=reject   
  7.            mode=single;   
  8.    }  



 

Java代码 复制代码 收藏代码
  1. location /mysql {   
  2.        set_unescape_uri $sql $arg_sql;   
  3.        set_unescape_uri $backend $arg_backend;   
  4.     
  5.        drizzle_query $sql;   
  6.        drizzle_pass $backend;   
  7.    }  


----------
如果在mac lion下编译则用
./configure --prefix=/Users/apple/Desktop/myfile/nginxtest/nginx --with-cc-opt="-Wno-deprecated-declarations"
否则过不去