More Related Content
Similar to Nginx使用和模块开发 (20)
Nginx使用和模块开发
- 5. Nginx 的优势
epoll 模型,支持高并发连接
使用 libevent 进行 epoll 、 kqueue 、 select 等网络 I/O 模型性能测试对
比图
- 7. Nginx 的模块开发
Nginx 模块主要有 3 种角色:
• handlers( 处理模块 ) 用于处理 HTTP 请求,然后产生输
出
• filters (过滤模块) 过滤 handler 产生的输出
• load-balancers (负载均衡模块)当有多于一台的后端备
选服 务器时,选择一台转发 HTTP 请求
- 9. 模块开发我们的需求
安装 nginx 模块的一般步骤如下:
1 、编写模块 config 文件和模块
2 、编译安装
3 、修改 nginx 的配置文件
4 、启动 nginx
- 10. 以 Empty Gif 模块为例
config 文件
ngx_addon_name=ngx_http_circle_gif_module
HTTP_MODULES="$HTTP_MODULES ngx_http_circle_gif_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_circle_gif_module.c"
CORE_LIBS="$CORE_LIBS -lMagickWand"
CFLAGS="$CFLAGS `MagickWand-config --cflags`"
- 11. ngx_http_circle_gif_module 模块
配置文件中,参数的配置
static ngx_command_t ngx_http_circle_gif_commands[] = {
{ ngx_string("circle_gif"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_circle_gif,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("circle_gif_min_radius"),
......
ngx_null_command
};
- 12. ngx_http_circle_gif_module 模块
url 等参数的获取
static ngx_int_t
....
digit = (char *)r->uri.data + r->uri.len - 1;
if (!(*digit-- == 'f' && *digit-- == 'i' && *digit-- == 'g' && *digit-- ==
'.'))
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Invalid extension with
%s", digit);
return NGX_HTTP_NOT_FOUND;
}
....
}
- 13. 编译
nginx 编译
./configure (如缺少 PCRE ,先安装)
安装
sudo make
sudo make install
执行 curl -i http://localhost 或浏览器访问 http://localhost/
就可以看到 welcome to nginx 的欢迎字幕 表示安装成功
模块编译
./configure --prefix= 安装目录 --add-module= 模块源代码
文件目录 --with-debug
make
sudo make install
- 15. 启动 Nginx
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
其中 url 的格式如下:
/circles/ffffff/000000/20.gif
/circles/<background color>/<foreground color>/<size>.gif
- 16. 模块是如何注册
在 nginx 代码的 auto 目录中,有一个名为 sources 的文件,根据编
译选项( configure 的参数)的不同, m4 宏变量 HTTP_MODULES
的值会发生变化:
如果指定了使用 empty gif 模块(默认就是使用了),则最终 m4 宏
变量 HTTP_MODULES 的值可能如下:
HTTP_MODULES="ngx_http_module /
ngx_http_core_module /
ngx_http_log_module /
ngx_http_upstream_module /
ngx_http_empty_gif_module "
注意:这里的 ngx_http_empty_gif_module 字符串对应了
ngx_http_empty_gif_module.c 文件中的 Module 主结构变量名。
- 17. 第三方模块使用 --- 直接与 mysql 通信
原文地址: http://ri0day.blogbus.com/logs/61820056.html
需要的模块是 ( 两位中国人写的 ,taobao 的 ):
http://github.com/chaoslawful/drizzle-nginx-module
http://github.com/agentzh/rds-json-nginx-module
[root@localhost conf]# curl http://127.0.0.1/mysql?2
[{"id":2,"num":null},{"id":2,"num":"430922198610020038"},
{"id":2,"num":"430922198610020037"},
{"id":2,"num":"430922198610020034"}]
在 http://wiki.nginx.org/3rdPartyModules 中有很多关于第三方模
块的开发,国人 agentzh 在这方面做了大量的工作。