apache的插件以动态库方式存在,通过配置动态加载;nginx插件则需要编译至nginx可执行程序中,openresty也是如此,直接将lua解释器嵌入至nginx,使其具备解析lua脚本能力。nginx 内置插件目录在 src/http/modules 下。
编写nginx插件过程比较麻复杂,需要对nginx源码及数据结构有一定的了解。nginx实现了字符串,定时器,容器(HashTable,Queue,红黑树),内存池,日志,网络处理,线程等常用函数,学习价值非常高。
本文是来自淘宝Tengine文档,根据自己理解做了一些注释。
一. hello world模块
#include <ngx_config.h> #include <ngx_core.h> #include <ngx_http.h> // hello 配置结构 typedef struct { ngx_int_t hello_counter; // 计数配置 } ngx_http_hello_loc_conf_t; // 上下文回调函数 static ngx_int_t ngx_http_hello_init( ngx_conf_t *cf ); static void *ngx_http_hello_create_loc_conf( ngx_conf_t *cf ); // 配置处理函数 static char *ngx_hello_set( ngx_conf_t *, ngx_command_t *, void * ); // 回调函数 static ngx_int_t ngx_http_hello_handler( ngx_http_request_t * ); // 配置项 static ngx_command_t ngx_http_hello_commands[] = { { ngx_string("hello_counter"), // 配置名称hello_counter NGX_HTTP_LOC_CONF | NGX_CONF_FLAG, // 配置为bool类型,取值为on/off ngx_hello_set, // 配置处理函数 NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, ngx_null_command // 以null结尾 }; // 模块上下文 static ngx_http_module_t ngx_hello_ctx = { NULL, ngx_http_hello_init, // 读取该模块配置后调用 NULL, NULL, NULL, NULL, ngx_http_hello_create_loc_conf, // 读取到location配置后调用(每个location创建一个) NULL }; // 模块定义 ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_hello_ctx, ngx_http_hello_commands, NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING }; static char *ngx_hello_set( ngx_conf_t *cf, ngx_command_t *cmd, void *conf ) { ngx_http_hello_loc_conf_t *local_conf; local_conf = conf; char *rv = NULL; // 读取NGX_CONF_FLAG类型参数 rv = ngx_conf_set_flag_slot(cf, cmd, conf); ngx_conf_log_error( NGX_LOG_INFO, cf, 0, "hello_counter:%d", local_conf->hello_counter ); return rv; } static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf) { ngx_http_hello_loc_conf_t* local_conf = NULL; local_conf = ngx_pcalloc( cf->pool, sizeof(ngx_http_hello_loc_conf_t) ); if ( local_conf == NULL ) { return NULL; } // 初始设置默认值 local_conf->hello_counter = NGX_CONF_UNSET; return local_conf; } static ngx_int_t ngx_http_hello_init(ngx_conf_t *cf) { ngx_http_handler_pt *h; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_conf_get_module_main_conf( cf, ngx_http_core_module ); h = ngx_array_push( &cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers ); if (h == NULL) { return NGX_ERROR; } // 在 NGX_HTTP_CONTENT_PHASE 阶段设置回调函数 *h = ngx_http_hello_handler; return NGX_OK; } static int ngx_hello_visited_times = 0; // 访问次数 static ngx_int_t ngx_http_hello_handler( ngx_http_request_t *r ) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; ngx_http_hello_loc_conf_t *my_conf; ngx_uint_t content_length = 0; u_char ngx_hello_string[1024] = {0}; ngx_log_error( NGX_LOG_EMERG, r->connection->log, 0, "ngx_http_hello_handler is called!" ); // 获取配置值 my_conf = ngx_http_get_module_loc_conf( r, ngx_http_hello_module ); if ( my_conf->hello_counter == NGX_CONF_UNSET || my_conf->hello_counter == 0 ) { ngx_sprintf( ngx_hello_string, "<h1>Non counter</h1>" ); } else { ngx_sprintf( ngx_hello_string, "<h1>Visited Times:%d</h1>", ++ngx_hello_visited_times ); } ngx_log_error( NGX_LOG_EMERG, r->connection->log, 0, "hello_string:%s", ngx_hello_string ); content_length = ngx_strlen( ngx_hello_string ); // 分配响应缓冲区 b = ngx_pcalloc( r->pool, sizeof(ngx_buf_t) ); out.buf = b; // attach out.next = NULL; b->pos = ngx_hello_string; b->last = ngx_hello_string + content_length; b->memory = 1; b->last_buf = 1; // 设置响应 ngx_str_set( &r->headers_out.content_type, "text/html" ); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = content_length; // 发送响应 rc = ngx_http_send_header( r ); // 传递至其它过滤器处理 return ngx_http_output_filter( r, &out ); }
二. 编译
插件目录:
~/hlmodule
-- config
-- ngx_http_hello_module.c
准备config文件,内容如下:
ngx_addon_name=ngx_http_hello_module HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"
编译:
./configure --prefix=~/nginx --add-module=~/hlmodule make && make install
三. 配置启用hello_counter
vi conf/nginx.conf
location /hello { hello_counter on; // 启用计数 }
四. 测试
hello_counter设置on:
hello_counter设置off:
参考链接:
淘宝nginx模块开发
http://tengine.taobao.org/book/chapter_03.html#hello-handler
官方reference
http://nginx.org/en/docs/dev/development_guide.html#core_modules
相关推荐
但如果希望通过阅读本书的第二、第三两部分,来学习Nginx的模块开发和架构设计 技巧时,则必须了解C语言的基本语法。在阅读本书第三部分时,需要读者对TCP有一个基 本的了解,同时对Linux操作系统也应该有简单的了解...
Nginx模块开发OpenResty简单使用笔记整理 ### Nginx简介 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中。与Apache相比。 同时,大量的第三方扩展模块也令...
Nginx 是由俄罗斯工程师Igor Sysoev 开发的一个高性能Web ...《Nginx 模块开发指南:使用C++11 和Boost 程序库》结构严谨、脉络清晰、论述精确、详略得当,值得广大软件开发工程师、系统运维工程师和编程爱好者拥有。
一个用于研究和演示简单的Nginx echo模块
nginx web server的扩展模块开发文档。
nginx的内部结构是由核心部分和...这样划分是为了使得每个模块的功能相对简单,便于开发,同时也便于对系统进行功能扩展。这样的模块化设计类似于面向对象中的接口类,它增强了nginx源码的可读性、可扩充性和可维护性。
Nginx 是由俄罗斯工程师Igor Sysoev 开发的一个高性能Web ...《Nginx 模块开发指南:使用C++11 和Boost 程序库》结构严谨、脉络清晰、论述精确、详略得当,值得广大软件开发工程师、系统运维工程师和编程爱好者拥有。
❑希望通过开发特定的HTTP模块实现高性能Web服务器的读者。❑希望了解Nginx的架构设计,学习怎样充分使用服务器上的硬件资源的读者。❑了解如何快速定位、修复Nginx中深层次Bug的读者。❑希望利用Nginx提供的框架,...
Nginx[ 1] 是由 俄罗斯 工程师 Igor Sysoev 开发 的 一个 高性能 Web 服务器, 各方 面的 表现 均 远 超 传统 的 ... Nginx模块开发指南:使用C++11和Boost程序库 (Kindle 位置 348-357). 电子工业出版社. Kindle 版本.
Nginx 扩展版:提供了一个带有标准模块的 nginx 版本,加上额外的功能和模块,这个容器基于 ubuntu:14.04 (trusty) 和官方 PPA。 标签: 稳定:NGINX 的稳定版本。 开发:NGINX 的开发(又名主线)版本。 标准 ...
希望通过开发特定的HTTP模块实现高性能Web服务器的读者。 希望了解Nginx的架构设计,学习其怎样充分使用服务器上的硬件资源的读者。 了解如何快速定位、修复Nginx中深层次Bug的读者。 希望利用Nginx提供的框架,设计...
而目前市面上用的非常成熟的扩展是由章亦春将Lua和Nginx粘合的ngx_lua模块,并且将Nginx核心、LuaJIT、ngx_lua模块、许多有用的Lua库和常用的第三方Nginx模块组合在一起成为OpenResty,这样开发人员就可以安装...
并且支持很多第三方的模块扩展。 Nginx的稳定性、功能集、示例配置文件和低系统资源的消耗让他后来居上,在全球活跃的网站中有12.18%的使用比率,大约为2220万个网站。 牛逼吹的差不多啦,如果你还不过瘾,你可以...
支持带有Cython扩展的标准Python包。可自动构建NGINX动态模块,用于当前的NGINX安装。在NGINX工作进程中运行嵌入式的Python。用Python或Cython编写NGINX模块。
当前nginx/Windows只使用select作为通知方法,所以不要期待它有很高的性能和扩展性。鉴于这点和一些已知问题,nginx/Windows目前还处于beta阶段。nginx/Windows和Unix版本相比,功能几乎已经齐全,除了XSLT过滤器、...
Redis、Lua、Nginx、OpenResty开发、Lua案例、Nginx模块学习以及性能优化、PHP7性能优化以及详细配置总结等。
2. **模块化**:NGINX的设计使其易于扩展和定制。它支持许多第三方模块,这些模块可以提供各种功能,如SSL支持、缓存、负载均衡等。 3. **可配置**:NGINX具有强大的配置能力,可以适应各种环境和需求。它支持许多...
最近项目中开发用到了nginx-lua扩展模块,nginx中的lua模块方便了基于nginx的功能定制。文档中包含了详细的安装教程。
支持自定义扩展UP功能命令窗口; 支持高级服务清理工具; 支持UPUPW安全防护功能; 支持PHP功能组件开启关闭; 支持主机性能适配功能; 支持生产开发环境选项; 支持FTP服务器端管理模块; 支持UPUPW配置文件...