Nginx的第一个模块-Hello World
2014-08-25 21:34
453 查看
麻雀虽小,五脏俱全,小小的Hello World盛行于程序世界,就在于其代码虽短,但要真正运行起来,需要我们略通基本语法,稍懂编译运行环境,知晓操作过程,最后,还有一颗持之以恒,不怕折腾的心。前一阵子跑通了Nginx的Hello World程序,今天重温了一遍就顺便写篇博客,记录下来,好记性不如烂笔头,方便以后查阅。
首先在着手操作之前,需要安装好Nginx,因为开发过程中涉及到源代码编译等步骤,最好采用源代码安装的方式。关于安装的具体步骤,可以参考这篇文章或者自行百度,这里就不再多说。
nginx默认安装路径为/usr/local/nginx,进入该目录下的sbin文件中,该文件包含了nginx的可执行文件,在该目录下使用下面的命令启动nginx服务。
若在nginx已经启动的情况下添加的模块,那么可以使用下面的命令来平滑重启Nginx,使用新的配置。
经过上面的一系列操作,基本上就大功告成了,使用浏览器访问http://localhost/hello_world,就可以看到下面的效果。
首先在着手操作之前,需要安装好Nginx,因为开发过程中涉及到源代码编译等步骤,最好采用源代码安装的方式。关于安装的具体步骤,可以参考这篇文章或者自行百度,这里就不再多说。
编写config文件
安装完成后,我们在nginx源代码根目录下创建文件夹ngx_http_hello_world_module文件夹,进入该目录,创建文件配置文件config,在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"config是一个shell脚本,我们需要在其中定义三个变量,ngx_addon_name为要开发的模块名称:ngx_http_hello_world_module;HTTP_MODULES表示的是所有的HTTP模块的名称,通过shell 语句"$HTTP_MODULES ngx_http_hello_world_module"将我们的模块名称添加到HTTP_MODULES变量中;NGX_ADDON_SRCS表示的是新增模块的源代码路径,这个例子中NGX_ADDON_SRCS的值就是我们创建的文件夹ngx_http_hello_world_module路径。
添加配置项
向nginx中的配置文件/usr/local/nginx/conf/nginx.conf中添加下列配置项:location /hello_world { hello_world testing!!!; }这个配置项添加在localhost server下,当我们使用浏览器访问http://localhost/hello_world时,就会在浏览器中显示字符“hello_world, testing!!!”。
编写模块文件
创建ngx_http_hello_world_module.c文件,编写HelloWorld程序运行需要的给各个模块,具体代码如下:#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> typedef struct //配置项结构体 – 每个模块相关的配置项集中管理用的结构体 { ngx_str_t output_words; } ngx_http_hello_world_loc_conf_t; static char* ngx_http_hello_world(ngx_conf_t* cf, ngx_command_t* cmd, void* conf); static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf); static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child); static ngx_command_t ngx_http_hello_world_commands[] = { //commands结构体,设定配置项特定的处理方式,定义该处理项出现时的处理函数 { //配置项的名字 ngx_string("hello_world"), //NGX_HTTP_LOC_CONF表示指令在位置配置部分出现是合法的 ,NGX_CONF_TAKE1: 指令读入一个参数 NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, //遇到该指令名字时调用的函数 ngx_http_hello_world, //存储位置,配置结构体+offset指示变量的具体存储位置 NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_hello_world_loc_conf_t, output_words), NULL }, ngx_null_command }; //实现ngx_http_module_t接口,来管理http模块的配置项,在http框架初始化时,会调用该模块定义的回调方法 static ngx_http_module_t ngx_http_hello_world_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL, ngx_http_hello_world_create_loc_conf, ngx_http_hello_world_merge_loc_conf }; //定义ngx_module_t模块,主要工作是利用前面定义的ngx_http_hello_world_module_ctx和ngx_http_hello_world_commands //来对其中的ctx和commands成员变量进行赋值 ngx_module_t ngx_http_hello_world_module = { NGX_MODULE_V1, &ngx_http_hello_world_module_ctx, ngx_http_hello_world_commands, NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 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[2]; ngx_http_hello_world_loc_conf_t* hlcf; hlcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_world_module); 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[0].buf = b; out[0].next = &out[1]; //建立ngx_buf_t,直接指向原内存地址,不对数据进行复制,节省内存 b->pos = (u_char*)"Hello World"; b->last = b->pos + sizeof("Hello World") - 1; b->memory = 1; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); out[1].buf = b; out[1].next = NULL; b->pos = hlcf->output_words.data; b->last = hlcf->output_words.data + (hlcf->output_words.len); b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = hlcf->output_words.len + sizeof("hello_world, ") - 1; rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } return ngx_http_output_filter(r, &out[0]); } static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf) { ngx_http_hello_world_loc_conf_t* conf; conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t)); if (conf == NULL) { return NGX_CONF_ERROR; } conf->output_words.len = 0; conf->output_words.data = NULL; return conf; } static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child) { ngx_http_hello_world_loc_conf_t* prev = parent; ngx_http_hello_world_loc_conf_t* conf = child; ngx_conf_merge_str_value(conf->output_words, prev->output_words, "Nginx"); return NGX_CONF_OK; } 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); //将ngx_http_core_loc_conf_t结构体的回调函数设为ngx_http_hello_world_handle, //在NGX_HTTP_CONTENT_PHASE阶段,如果请求的主机名,URI与配置项所在的配置块相匹配时,就调用该回调方法 clcf->handler = ngx_http_hello_world_handler; ngx_conf_set_str_slot(cf, cmd, conf); return NGX_CONF_OK; }
添加模块并编译
在编写好我们自己的模块后,需要将其添加到Nginx中并进行编译,具体方法为进入Nginx源代码根目录,执行命令:./configure --add-module=./ngx_http_hello_world_module sudo make sudo make install这三个命令分别完成模块的添加,代码编译,模块安装等功能,添加模块时,在--add-module=后面指定模块的文件路径。
重启Nginx
接下来,启动Nginx就可以通过浏览器测试Hello World模块的效果。nginx默认安装路径为/usr/local/nginx,进入该目录下的sbin文件中,该文件包含了nginx的可执行文件,在该目录下使用下面的命令启动nginx服务。
sudo ./nginx
若在nginx已经启动的情况下添加的模块,那么可以使用下面的命令来平滑重启Nginx,使用新的配置。
sudo ./nginx-s reload
经过上面的一系列操作,基本上就大功告成了,使用浏览器访问http://localhost/hello_world,就可以看到下面的效果。
相关文章推荐
- Nginx编写HTTP模块:第一个Nginx HTTP模块 “Hello World!”
- ubuntu 第一个Hello world模块编译(本机运行)
- 【nginx源码学习与运用 九】搭建第一个nginx的http模块
- linux 第一个内核模块Hello World
- Nginx学习之一-第一个程序Hello World
- linux 第一个内核模块Hello World
- Linux---我的第一个内核模块之Hello World
- 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!
- Nginx学习之一-第一个程序Hello World
- Nginx学习之一-第一个程序Hello World
- 第一个内核模块hello world
- nginx模块开发0(hello world)
- Nginx学习之一-第一个程序Hello World
- 内核开发:第一个hello world 模块
- 10天学会phpWeChat——第二天:hello world!我的第一个功能模块
- 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!
- nginx HTTP模块的hello world
- 第一个模块,hello world 驱动编写
- ubuntu12.04 编译第一个hello world模块!
- Linux设备驱动程序,个人学习,第一个模块hello world