`
haoningabc
  • 浏览: 1449341 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

nginx的helloworld模块的helloworld

阅读更多
经典的nginx的helloworld尝试了一下
过程就是
nginx--->config文件---->module--->command[]<----->函数----->handler
                          |           |
                         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:
ngx_addon_name=ngx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
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之后
修改
CC =    gcc -g

让他支持gdb看代码
make
make install

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


注释1:
NGX_HTTP_MAIN_CONF	指令出现在全局位置部分是合法的
NGX_HTTP_SRV_CONF	指令出现在server主机配置部分是合法的
NGX_HTTP_LOC_CONF	指令出现在Location配置部分是合法的
NGX_HTTP_UPS_CONF	指令出现在upstream配置部分是合法的
NGX_CONF_NOARGS		指令没有参数
NGX_CONF_TAKE1		指令读一个参数
。。。。。
NGX_CONF_TAKE7		指令读7个参数
NGX_CONF_FLAG		指令读一个布尔型数据
NGX_CONF_1MORE		指令至少读一个参数
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调用次数
[root@red54apple sbin]# strace -T -c -p 10700
Process 10700 attached - interrupt to quit
Process 10700 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.000060          60         1           write
  0.00    0.000000           0         1           close
  0.00    0.000000           0         1           ioctl
  0.00    0.000000           0         1           writev
  0.00    0.000000           0         1           accept
  0.00    0.000000           0         3         1 recvfrom
  0.00    0.000000           0         1           setsockopt
  0.00    0.000000           0         3           epoll_wait
  0.00    0.000000           0         1           epoll_ctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000060                    13         1 total
[root@red54apple sbin]# ldconfig -p |grep mysql

----------------------
ltrace -p 10700   注意这里用worker进程,master木有
curl localhost/hello
得到
[root@red54apple sbin]# ltrace -p 10700
__errno_location()                                                                                           = 0x2b8699230640
gettimeofday(0x7fff5219a250, NULL)                                                                           = 0
localtime_r(0x7fff5219a1b8, 0x7fff5219a2a0, 84, 0, 0x6c7d27)                                                 = 0x7fff5219a2a0
epoll_wait(8, 0xa607610, 512, 0xffffffff, 0x6c8b48
)                                                          = 1
gettimeofday(0x7fff5219a250, NULL)                                                                           = 0
localtime_r(0x7fff5219a1b8, 0x7fff5219a2a0, 84, 0, 0x6c7d45)                                                 = 0x7fff5219a2a0
accept(6, 0x7fff5219a250, 0x7fff5219a2c0, 0xa621250, 0x6c8b62)                                               = 3
memset(0x2b869943c180, '\000', 184)                                                                          = 0x2b869943c180
memset(0xa621320, '\000', 104)                                                                               = 0xa621320
memset(0xa63b330, '\000', 104)                                                                               = 0xa63b330
posix_memalign(0x7fff5219a1b0, 16, 256, 0xa63b330, 104)                                                      = 0
ioctl(3, 21537, 0x7fff5219a1fc)                                                                              = 0
epoll_ctl(8, 1, 3, 0x7fff5219a160, 0xa613718)                                                                = 0
epoll_wait(8, 0xa607610, 512, 60000, 0xa613718)                                                              = 1
gettimeofday(0x7fff5219a250, NULL)                                                                           = 0
malloc(1256)                                                                                                 = 0xa608e20
posix_memalign(0x7fff5219a170, 16, 256, 256, 3)                                                              = 0
malloc(1024)                                                                                                 = 0xa609310
posix_memalign(0x7fff5219a240, 16, 4096, 144, 3)                                                             = 0
recv(3, 0xa609310, 1024, 0, 3)                                                                               = 158
strncmp("penSSL/0.9.8b zlib/1.2.3 libidn/"..., "pera", 4)                                                    = -4
strncmp("SL/0.9.8b zlib/1.2.3 libidn/0.6."..., "afari/", 6)                                                  = -14
strncmp("L/0.9.8b zlib/1.2.3 libidn/0.6.5", "afari/", 6)                                                     = -21
writev(3, 0x7fff52199650, 2, 0x7fff52199650, 0xa60a1cd)                                                      = 159
write(4, "127.0.0.1 - - [29/Nov/2011:15:21"..., 170)                                                         = 170
free(0xa609720)                                                                                              = <void>
free(0xa608e20)                                                                                              = <void>
free(0xa609310)                                                                                              = <void>
setsockopt(3, 6, 1, 0x7fff52199fe4, 4)                                                                       = 0
malloc(1024)                                                                                                 = 0xa608e20
__errno_location()                                                                                           = 0x2b8699230640
recv(3, 0xa608e20, 1024, 0, 3)                                                                               = 0
close(3)                                                                                                     = 0
free(0xa608e20)                                                                                              = <void>
free(0xa613680)                                                                                              = <void>
free(0xa5fe390)                                                                                              = <void>
epoll_wait(8, 0xa607610, 512, 0xffffffff, 0

 <unfinished ...>
[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


http://t.cn/S4WEFG

upstream main_db {
       drizzle_server 127.0.0.1:3306
           user=monty password=some_pass dbname=test
           protocol=mysql;
 
       drizzle_keepalive max=10 overflow=reject
           mode=single;
   }


location /mysql {
       set_unescape_uri $sql $arg_sql;
       set_unescape_uri $backend $arg_backend;
 
       drizzle_query $sql;
       drizzle_pass $backend;
   }

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

分享到:
评论
3 楼 zhaoyanzimm 2016-02-23  
感谢您的分享,给我提供了很大的帮助,在使用过程中发现了一个问题如下:
r->headers_out.content_type.len =sizeof("text/plain")-1;
r->headers_out.content_type.data=(u_char *) "text/plain";
中的text/plain 是否需要改为text/html,我直接用的时候网页上不能显示内容,但是可以通过curl获取结果。修改之后就可以直接通过网页察看结果了,请问是否是我浏览器的原意或者其他什么原因呢?
2 楼 haoningabc 2012-12-12  
    
1 楼 martialarts 2012-12-12  
goood

相关推荐

Global site tag (gtag.js) - Google Analytics