View
1.360
Download
6
Embed Size (px)
DESCRIPTION
nginx入门和使用
Citation preview
Nginx 使用和模块开发
Web 服务器Apache
Tomcat
Microsoft IIS
Lighttpd
Nginx
......
谁在用 Nginx
......
Nginx 的发展
活跃网站: Nginx 的占有率达到了 12.18% ,比上月增长 0.57% ;所有网站: Nginx 的占有率达到 9.63%
Netcraft 的报告
Nginx 的优势epoll 模型,支持高并发连接
使用 libevent 进行 epoll 、 kqueue 、 select 等网络 I/O 模型性能测试对比图
Nginx 的还能做什么
反向代理
负载均衡
模块开发 -- 开发自己想要的功能
Nginx 的模块开发
Nginx 模块主要有 3 种角色:
• handlers( 处理模块 ) 用于处理 HTTP 请求,然后产生输出
• filters (过滤模块) 过滤 handler 产生的输出
• load-balancers (负载均衡模块)当有多于一台的后端备选服 务器时,选择一台转发 HTTP 请求
模块开发我们的需求
Nginx 模块我们想得到:
• 访问的 url , ip 地址等
• 设置配置文件中的访问变量
• ......
模块开发我们的需求
安装 nginx 模块的一般步骤如下:
1 、编写模块 config 文件和模块
2 、编译安装
3 、修改 nginx 的配置文件
4 、启动 nginx
以 Empty Gif 模块为例
config 文件
ngx_addon_name=ngx_http_circle_gif_moduleHTTP_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`"
•
配置文件中,参数的配置
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}; NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_num_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_circle_gif_loc_conf_t, min_radius), NULL },
•
ngx_http_circle_gif_module 模块
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; }
....}
•ngx_http_circle_gif_handler(ngx_http_request_t *r)•{
编译
nginx 编译
./configure (如缺少 PCRE ,先安装)
安装sudo makesudo make install执行 curl -i http://localhost 或浏览器访问 http://localhost/ 就可以看到 welcome to nginx 的欢迎字幕 表示安装成功
模块编译./configure --prefix= 安装目录 --add-module= 模块源代码文件目录 --with-debugmake sudo make install
配置文件的修改
location /circles { circle_gif; circle_gif_min_radius 10 ; circle_gif_max_radius 20 ; }
启动 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
模块是如何注册
在 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 主结构变量名。
第三方模块使用 --- 直接与 mysql 通信
原文地址: http://ri0day.blogbus.com/logs/61820056.html
需要的模块是 ( 两位中国人写的 ,taobao 的 ):http://github.com/chaoslawful/drizzle-nginx-modulehttp://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 在这方面做了大量的工作。