初识nginx helloworld模块

摘要:1.nginx模块 首先nginx和apache最大的不同就是nginx的模块 vi /opt/nginx_hello_word/config 不能够动态添加,需要在编译时,指定要添加的模块路径,与nginx源码一起编译。 nginx模块的处理流程: a.客户端发送http请求道nginx服务器 b.nginx基于配置文件中的位置选择一个合适的处理模块 c.负载均衡模块选择一台后端服务器(反

 

1.nginx模块

首先nginx和apache最大的不同就是nginx的模块

vi /opt/nginx_hello_word/config

不能够动态添加,需要在编译时,指定要添加的模块路径,与nginx源码一起编译。
nginx模块的处理流程:
a.客户端发送http请求道nginx服务器
b.nginx基于配置文件中的位置选择一个合适的处理模块
c.负载均衡模块选择一台后端服务器(反向代理情况下)
d.处理模块进行处理并把输出缓冲放到第一个过滤模块上
e.第一个过滤模块处理后输出给第二个过滤模块
f.然后第二个过滤模块又到第三个过滤模块
g.第N个过滤模块。。。
h.发处理结果发给客户端

1 (1).jpg

 

2.nginx模块编写

a、创建模块文件夹

mkdir -p /opt/nginx_hello_world cd /opt/nginx_hello_world

 

b、创建模块配置文件

vi /opt/nginx_hello_word/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"

 

c、创建模块主文件

vi /opt/nginx_hello_world/ngx_http_hello_world_module.c

写入内容如下:

#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); /* Commands */ static ngx_command_t ngx_http_hello_world_commands[] = { { ngx_string("hello_world"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello_world, 0, 0, NULL }, ngx_null_command }; static u_char ngx_hello_world[] = "hello world"; 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 location configuration */ }; /* hook */ 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_int_t rc; */ ngx_buf_t *b; ngx_chain_t out; /* Http Output Buffer */ r->headers_out.content_type.len = sizeof("text/html;charset=utf-8") - 1; r->headers_out.content_type.data = (u_char *) "text/html;charset=utf-8"; 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); } static char * ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf ; /* register hanlder */ clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_hello_world_handler; return NGX_CONF_OK; }

 

d、编译

我的加了nginx_lua模块,其他模块等的。

./configure --prefix=/usr/local/nginx-1.9.6 --with-pcre=/root/pcre-8.36 --with-zlib=/root/zlib-1.2.8 --with-openssl=/root/openssl-1.0.2a --with-stream --with-stream_ssl_module --with-cpp_test_module --with-http_stub_status_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-http_realip_module --with-http_v2_module --with-http_ssl_module --with-threads --add-module=/root/ngx_devel_kit-master --add-module=/root/lua-nginx-module-master --add-module=/root/echo-nginx-module-master --add-module=/root/stream-lua-nginx-module-master --add-module=/root/lua-upstream-nginx-module-master --add-module=/root/headers-more-nginx-module-master --add-module=/opt/nginx_hello_world

默认只需要

./configure --prefix=/usr/local/nginx --add-module=/opt/nginx_hello_world/

make make install

 

e、配置nginx.conf

只需要在server域增加location

server { listen 83; server_name _; charset utf-8; access_log logs/tttt.access.log; location /helloworld { hello_world; } }

 

访问:http://192.168.26.61:83/helloworld即可得到helloworld

 

 

3.hello world模块分析

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

static ngx_command_t ngx_http_hello_world_commands[] = { { ngx_string("hello_world"), //设置指令名称字符串,注意不能包含空格,数据类型ngx_str_t之后会详细讲解。 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, //配置指令的合法位置,这里表示:location部分合法,并且指令没有参数。 ngx_http_hello_world,//回调函数,三个参数(ngx_conf_t *cf,ngx_command_t *cmd, void *conf) 0,//后面的参数有待发掘,我还没有用到 0, NULL }, ngx_null_command };

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

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

static ngx_http_module_t ngx_http_hello_world_module_ctx = { NULL, /* 读入配置前调用*/ NULL, /* 读入配置后调用*/ NULL, /* 创建全局部分配置时调用 */ NULL, /* 初始化全局部分的配置时调用*/ NULL, /* 创建虚拟主机部分的配置时调用*/ NULL, /* 与全局部分配置合并时调用 */ NULL, /* 创建位置部分的配置时调用 */ NULL /* 与主机部分配置合并时调用*/ };

d.ngx_module_t定义结构体ngx_http_hello_world_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 };

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

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

static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)//ngx_http_request_t *r 可以访问到客户端的头部和不久要发送的回复头部 { /* ngx_int_t rc; */ ngx_buf_t *b; ngx_chain_t out; /* Http Output Buffer */ 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); }