转载地址:https://blog.csdn.net/zhangxiao93/article/details/52836128
武侠世界的高手一般都是从扫地僧做起的,程序员学一门技术都是从hello world开始。
本文介绍开发一个简单的hello world模块。
(一) 如何将自己的http模块编译进Nginx
首先创建源码目录:
mkdir /home/zhangxiao/nginx/nginx-1.0.15/src/test/
在编译Nginx之前,执行configure脚本时,添加--add-module=PATH例如:
./configure --add-module=/home/zhangxiao/nginx/nginx-1.0.15/src/test
1.编写config文件
config文件在上述目录中,这个文件将通知如何编译本模块。
config文件其实是一个可执行的Shell脚本,如果只想开发一个HTTP模块,需要定义三个变量:
(1) ngx_addon_name
仅在configure执行时使用,一般设置为模块名称。
(2) HTTP_MODULES
保存所有的HTTP模块名称。每个模块间由空格相连。在重新设置这个变量时,不要直接覆盖,因此要如下设置:
"$HTTP_MODULES ngx_http_mytest_module"
(3) NGX_ADDON_SRCS
用于指定新模块的源代码,多个待编译的源代码之间可以用空格相连。
注意,在设置这个变量时可以使用$ngx_addon_dir变量,它等价于configure执行时--add-module=PATH的PATH参数
因此,我们的config文件如下:
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
2.修改nginx.conf配置文件
sudo vi /usr/local/nginx/conf/nginx.conf
在server配置块下添加location配置块,并在新添加的location配置块中添加mytest配置项:
#nginx.conf删去一部分内容
http {
server {
listen 80;
server_name localhost;
#########添加部分#########
location=/zxtest{
#mytest配置项
mytest;
}
#########################
location / {
proxy_pass http://localhost:8080;
}
}
3.定义http模块处理用户请求
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
//处理配置项
static ngx_command_t ngx_http_mytest_commands[] = {
{
ngx_string("mytest"), //配置项名称
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF |
NGX_CONF_NOARGS, //类型type
ngx_http_mytest, //set 函数
NGX_HTTP_LOC_CONF_OFFSET, //偏移
0,
NULL
},
ngx_null_command
};
//回调函数,出现my_test配置项后调用
static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
//找到mytest所属配置块
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
//主机域名、URI与mytest所在配置块匹配时,将调用handler
clcf->handler = ngx_http_mytest_handler;
return NGX_CONF_OK;
}
//ngx_http_module_t接口,模块上下文
//如果没有什么工作是必须在HTTP框架初始化时完成,则不必实现8个回调
static ngx_http_module_t ngx_http_mytest_module_ctx={
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
//新模块定义
ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
//处理实际用户请求
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
//请求方法必须是GET或者HEAD,否则返回405/NOT ALLOWED
if (!(r->method &(NGX_HTTP_GET|NGX_HTTP_HEAD)))
{
return NGX_HTTP_NOT_ALLOWED;
}
//丢弃请求中的包体
ngx_int_t rc = ngx_http_discard_request_body(r);
if (NGX_OK != rc)
{
return rc;
}
//设置返回的Content-Type ngx_string是一个宏可以初始化data字段和len字段
ngx_str_t type = ngx_string("text/plain");
ngx_str_t response = ngx_string("Hello ZhangXiao World");
//响应包体内容和状态码设置
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.len;
r->headers_out.content_type = type;
//发送http头部
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc>NGX_OK || r->header_only) {
return rc;
}
//构造ngx_buf_t结构体准备发送报文
ngx_buf_t *b;
b = ngx_create_temp_buf(r->pool, response.len);
if (NULL == b) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
//拷贝响应报文
ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos+response.len;
//声明这是最后一块缓冲区
b->last_buf = 1;
//构造发送时的ngx_chain_t结构体
ngx_chain_t out;
out.buf = b;
out.next = NULL;
//发送响应,结束后HTTP框架会调用ngx_http_finalize_request方法结束请求
return ngx_http_output_filter(r, &out);
}
4.编译测试
./configure --add-module=/home/zhangxiao/nginx/nginx-1.0.15/src/test
make
sudo make install
重启nginx
./usr/local/nginx/sbin
在浏览器输入localhost/zxtest
进行测试验证
(二)参考
1.深入理解Nginx
2.http://blog.csdn.net/xiajun07061225/article/details/9130237
3.http://kb.cnblogs.com/page/98352/