【nginx源码学习与运用】系列博客中的示例代码在csdn的代码托管服务器CODE上,地址https://code.csdn.net/u012819339/nginx_study ,你可以将其自由的下载到本地,或者通过Git来实时获取更新
本篇博客将简单展示在nginx上开发一个简单的模块来处理请求的整个流程,并给出实例。至于细节部分还需各位童鞋多多参考其他资料,多多领悟
创建自己的代码目录
在nginx的src目录下新建一个目录mymodule,并在该目录下创建两个文件config
文件和ngx_http_mytest_module.c
文件
编写config
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"
ngx_addon_name:是代指模块名称,仅在configure执行时使用
HTTP_MODULES:保存nginx中所有的HTTP模块名称,名称之间空格隔开
NGX_ADDON_SRCS:指定编译模块的源代码
ngx_addon_dir:等价于执行configure的时候–add-module=PATH的PATH参数
编写自己的模块源码
ngx_http_mytest_module.c
源码如下:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_http.h>
//处理用户请求的方法
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
//设定mytest模块只处理GET或HEAD请求
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(rc != NGX_OK)
return rc;
ngx_str_t type = ngx_string("text/plain");
ngx_str_t respone = ngx_string("hello world, arvik test!");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = respone.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, respone.len);
if(b == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_memcpy(b->pos, respone.data, respone.len);
b->last = b->pos + respone.len;
b->last_buf = 1;
//构造ngx_chain_t传给filter使用
ngx_chain_t out;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static char *ngx_http_mytest(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_mytest_handler;
return NGX_CONF_OK;
}
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,
ngx_http_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
};
//模块接口描述的8个阶段,函数指针都可置NULL
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
};
生成Makefile
执行以下命令:
./configure --add-module=./src/mymodule/
编译、安装nginx源码
#make
#make install
编译安装好后自己的模块也就嵌在nginx模块里了
修改配置
打开/usr/local/nginx/conf/nginx.conf文件
对应位置增加几行代码如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
//以下3行内容为增加内容
location /test {
mytest;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
运行结果
本地浏览器地址输入http://localhost/test,结果如下:
代码已经存上传到这个位置 https://code.csdn.net/u012819339/nginx_study ,在项目的test9目录中,童鞋们可自行下载演示