Nginx之一:实现简单HTTP模块helloworld
2017-04-27 22:08
525 查看
查看原文:http://www.wyblog.cn/2017/04/27/nginx%e4%b9%8b%e4%b8%80%ef%bc%9a%e5%ae%9e%e7%8e%b0%e7%ae%80%e5%8d%95http%e6%a8%a1%e5%9d%97helloworld/本篇博文记录如何在Nginx里开发一个简单的HTTP模块,实现响应。
调用过程
修改conf/nginx.conf
在http{}中的server{}里添加 location /test模块,见下图:生成一个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_http_mytest_module.c
代码:#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, //类型,指定配置项可以出现的位置 ngx_http_mytest, //配置项的回调函数 见下文 NGX_HTTP_LOC_CONF_OFFSET, //在配置文件中的偏移量 0, NULL }, ngx_null_command }; //模块上下文 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 = { //#define NGX_MODULE_V1 0,0,0,0,0,0,1 包含了ctx_index 与 index 分别为在一类模块中的序号及所有模块里的序号 NGX_MODULE_V1, &ngx_http_mytest_module_ctx, //ctx http框架的要求 ngx_http_mytest_commands, //commands 处理nginx.conf中的配置项 NGX_HTTP_MODULE, //指定模块类型 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING }; //配置项对应的回调函数 static char * ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf; //clcf指向一个location内的配置块 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); //注:上边有一个 ngx_http_mytest_module 两者结构一样 clcf->handler = ngx_http_mytest_handler; //名称匹配后的回调函数 return NGX_CONF_OK; } //实际完成处理的回调函数 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r) { 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 response = ngx_string("Hello World"); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = response.len; r->headers_out.content_type = type; rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } ngx_buf_t *b; b = ngx_create_temp_buf(r->pool, response.len); if (b == NULL) { 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 out; out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); }
编译
配置好以上所有文件后,将config与ngx_http_mytest_module.c 这两个文件放在同一个文件夹里,比如~/nginx-1.2.0/test。然后编译nginx带上模块地址:
./configure --add-module /home/**/nginx-1.2.0/test
然后make -B,再make install。
测试
启动nginx:sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
浏览器访问:
恩,成功了!
查看原文:http://www.wyblog.cn/2017/04/27/nginx%e4%b9%8b%e4%b8%80%ef%bc%9a%e5%ae%9e%e7%8e%b0%e7%ae%80%e5%8d%95http%e6%a8%a1%e5%9d%97helloworld/
相关文章推荐
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- Nginx之一:实现简单HTTP模块helloworld
- 【Nginx】初识nginx---实现一个简单的http模块
- Nginx开发一个简单的HTTP过滤模块