本文真正的目的,绝对不是告诉大家如何在 nginx lua module 添加新 api 这么点东西。而是以此为例,告诉大家 nginx 模块开发环境搭建、码字编译、编写测试用例、代码提交、申请代码合并等。给大家顺路普及一下 git 的使用。
目前有个应用场景,需要获取当前 nginx worker 数量的需要,所以添加一个新的接口 ngx.config.workers()。由于这个功能实现简单,非常适合大家当做例子。废话不多说, let's fly now !
$ wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz
$ tar -xvf ngx_openresty-1.7.10.1.tar.gz
$ cd ngx_openresty-1.7.10.1
$ ./configure
$ make
注意这里不需要 make install
# ngx_lua-0.9.15/src/ngx_http_lua_config.c
$ rm ./nginx-1.7.10/objs/addon/src/ngx_http_lua_config.o
$ make
$ cpan
cpan[2]> install Test::Nginx::Socket::Lua
$ cat 131-config-workers.t
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket::Lua;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
#repeat_each(1);
plan tests => repeat_each() * (blocks() * 3);
#no_diff();
#no_long_string();
run_tests();
__DATA__
=== TEST 1: content_by_lua
--- config
location /lua {
content_by_lua '
ngx.say("workers: ", ngx.config.workers())
';
}
--- request
GET /lua
--- response_body_like chop
^workers: 1$
--- no_error_log
[error]
$ cat 132-config-workers_5.t
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket::Lua;
#worker_connections(1014);
#master_on();
workers(5);
#log_level('warn');
repeat_each(2);
#repeat_each(1);
plan tests => repeat_each() * (blocks() * 3);
#no_diff();
#no_long_string();
run_tests();
__DATA__
=== TEST 1: content_by_lua
--- config
location /lua {
content_by_lua '
ngx.say("workers: ", ngx.config.workers())
';
}
--- request
GET /lua
--- response_body_like chop
^workers: 5$
--- no_error_log
[error]
$ export PATH=/path/to/your/nginx/sbin:$PATH #设置 nginx 查找路径
$ cd ngx_lua-0.9.15 # 进入你修改的模块
$ prove t/131-config-workers.t # 测试指定脚本
t/131-config-workers.t .. ok
All tests successful.
Files=1, Tests=6, 1 wallclock secs ( 0.04 usr 0.00 sys + 0.18 cusr 0.05 csys = 0.27 CPU)
Result: PASS
$
$ prove t/132-config-workers_5.t # 测试指定脚本
t/132-config-workers_5.t .. ok
All tests successful.
Files=1, Tests=6, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.17 cusr 0.04 csys = 0.24 CPU)
Result: PASS
来看看我们的成果吧:
pull request : 点击查看
commit detail: [点击查看](https://github.com/membphis/lua-nginx-module/commit/9 d991677 c090 e1 f86 fa5840 b19 e02 e56 a4 a17 f86)