设为首页 收藏本站
查看: 546|回复: 0

[经验分享] nginx的helloworld模块的helloworld

[复制链接]
发表于 2016-12-26 06:52:09 | 显示全部楼层 |阅读模式
经典的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)
[iyunv@red54apple objs]#
-------------调试--------
gdb -d /opt/nginxtest/nginx/sbin/ nginx 10540
l就能看代码了
----------ltrace 和strace-------------
strace -T -c -p 10700
curl localhost/hello
ctl+c 看epoll调用次数
[iyunv@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
[iyunv@red54apple sbin]# ldconfig -p |grep mysql
----------------------
ltrace -p 10700   注意这里用worker进程,master木有
curl localhost/hello
得到

[iyunv@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 ...>
[iyunv@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"
否则过不去

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319298-1-1.html 上篇帖子: 第二章 OpenResty(Nginx+Lua)开发入门 下篇帖子: 实例讲解Nginx下的rewrite规则
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表