NGINX(六)扩展
前言
nginx模块化设计, 添加扩展模块变得容易, 下面开发一个非常简单的扩展模块, 实现返回http请求的头部内容, 配置标记是ping_pong, 配置在NGX_HTTP_LOC_CONF中.
HTTP处理阶段
nginx处理http请求分为很多的阶段, 下面列出了所有阶段, 服务器接收到完http请求头部内容后, 会依次执行各个阶段, 执行顺序按照全局ngx_modules数组中的顺序进行. ngxin如何知道我们的模块是http处理的一个阶段呢, 阶段模块在初始化时, 要将自己注册进模块列表中, 后面我们自己模块代码会有体现.
typedef enum {
NGX_HTTP_POST_READ_PHASE = 0,
NGX_HTTP_SERVER_REWRITE_PHASE,
NGX_HTTP_FIND_CONFIG_PHASE,
NGX_HTTP_REWRITE_PHASE,
NGX_HTTP_POST_REWRITE_PHASE,
NGX_HTTP_PREACCESS_PHASE,
NGX_HTTP_ACCESS_PHASE,
NGX_HTTP_POST_ACCESS_PHASE,
NGX_HTTP_TRY_FILES_PHASE,
/*http请求内容处理, 生成内容, 过滤操作, 我们要实现的简单扩展就注册在这个阶段*/
NGX_HTTP_CONTENT_PHASE,
NGX_HTTP_LOG_PHASE
} ngx_http_phases;
/*
*http请求头部接收完之后, 会执行ngx_http_core_run_phases函数, 函数遍历所有阶段进行执行.
*/
void
ngx_http_core_run_phases(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_phase_handler_t *ph;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
ph = cmcf->phase_engine.handlers;
while (ph[r->phase_handler].checker) {
/*执行每个阶段的回调函数*/
rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
/*一旦有阶段返回NGX_OK标记, 则立即返回, 后面的阶段不再执行*/
if (rc == NGX_OK) {
return;
}
}
}
ps : 各个阶段具体处理什么参考博客 http://blog.csdn.net/fengmo_q/article/details/8594610
编译选项
--with-http_ping_pong_module
编译配置:
1.1.auto/options中添加
--with-http_ping_pong_module) HTTP_PING_PONG=YES ;;
2.2.auto/modules中添加
if [ $HTTP_PING_PONG = YES ]; then
HTTP_MODULES="$HTTP_MODULES $HTTP_PING_PONG_MODULE"
HTTP_SRCS="$HTTP_SRCS src/http/modules/ngx_http_ping_pong_module.c"
fi
3.3.auto/sources中添加
HTTP_PING_PONG_MODULE=ngx_http_ping_pong_module
HTTP_PING_PONG_SRCS=src/http/modules/ngx_http_ping_pong_module.c
nginx.conf中配置示例
server {
listen 88;
location /test {
ping_pong on;
}
}
完整ngx_http_ping_pong_module.c
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_ping_pong_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_ping_pong_init(ngx_conf_t *cf);
static char* ngx_http_ping_pong_set_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void* ngx_http_ping_pong_create_loc_conf(ngx_conf_t *cf);
typedef struct{
ngx_int_t ping_pong;
}ngx_http_ping_pong_loc_conf_t;
static ngx_command_t ngx_http_ping_pong_commands[] = {
{ ngx_string("ping_pong"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_http_ping_pong_set_conf,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_ping_pong_loc_conf_t, ping_pong),
NULL },
ngx_null_command
};
ngx_http_module_t ngx_http_ping_pong_module_ctx = {
NULL, /* preconfiguration */
ngx_http_ping_pong_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_ping_pong_create_loc_conf, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_ping_pong_module = {
NGX_MODULE_V1,
&ngx_http_ping_pong_module_ctx, /* module context */
ngx_http_ping_pong_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/*
* 阶段处理的回调函数
*/
static ngx_int_t
ngx_http_ping_pong_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t* b;
ngx_chain_t out;
ngx_uint_t content_length = 0;
u_char ngx_ping_pong_string[1024] = {0};
ngx_http_ping_pong_loc_conf_t* lcf;
/*获取并检查是否配置了ping_pong标记*/
lcf = ngx_http_get_module_loc_conf(r, ngx_http_ping_pong_module);
if (lcf->ping_pong == NGX_CONF_UNSET)
{
/*如果没有进行配置, 则继续执行后面阶段*/
return NGX_DECLINED;
}
/* 格式化输出的内容 */
ngx_sprintf(ngx_ping_pong_string, "%V:%V,%V,%V %V:%V", &r->method_name, &r->uri, &r->args, &r->request_line, &r->headers_in.host->key, &r->headers_in.host->value);
content_length = ngx_strlen(ngx_ping_pong_string);
/*丢弃请求内容, 用不到*/
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK)
return rc;
ngx_str_set(&r->headers_out.content_type, "text/html");
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
out.buf = b;
out.next = NULL;
/*构造一个输出buf*/
b->pos = ngx_ping_pong_string;
b->last = ngx_ping_pong_string + content_length;
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = content_length;
/*http响应头部返回给客户端*/
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
return rc;
/*http响应内容返回给客户端*/
return ngx_http_output_filter(r, &out);
}
/*
*由于我们配置非常简单, 因此这里其实不用单独写set函数的, 可以直接将ngx_conf_set_flag_slot配置到command配置中
*/
static char*
ngx_http_ping_pong_set_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char* rv = ngx_conf_set_flag_slot(cf, cmd, conf);
return rv;
}
/*
*初始化配置
*/
static void*
ngx_http_ping_pong_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_ping_pong_loc_conf_t* lcf = NULL;
lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ping_pong_loc_conf_t));
if (lcf == NULL)
{
return NULL;
}
lcf->ping_pong = NGX_CONF_UNSET;
return lcf;
}
/*
*模块初始化时, 将阶段处理的回调函数注册进相应的阶段
*/
static ngx_int_t
ngx_http_ping_pong_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;
}
*h = ngx_http_ping_pong_handler;
return NGX_OK;
}
NGINX(六)扩展的更多相关文章
- nginx安装扩展 sub_filter&http_ssl_module
nginx之所以轻,因为默认没有安装各种各样的扩展: nginx安装扩展插件: 下面列出两个可能会用到的插件 一.sub_filter内容过滤器,这个在nginx做http转发的时候会很常用 1.下载 ...
- nginx+php+扩展件安装实践版
一.安装各种软件包 yum -y install wget git vim lrzsz unzip zip gcc make gd-devel bzip2 bzip2-devel libcurl li ...
- Centos安装Memcached和(Nginx)Memcache扩展详细教程
下载memadmin,下载地址:http://www.junopen.com/memadmin/ 并在IIS新建站点. 测试地址:http://wap.yousawang.com/mem , 1.重启 ...
- Nginx(四)-- Nginx的扩展-OpenRestry
1. OpenResty 安装及使用 OpenResty 是一个通过 Lua 扩展 Nginx 实现的可伸缩的 Web 平台,内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地 ...
- Nginx Google 扩展
安装配置详见: https://github.com/cuber/ngx_http_google_filter_module/blob/master/README.zh-CN.md tenginx详见 ...
- nginx功能扩展整理
0.基本负载均衡配置 编辑/etc/nginx/nginx.conf,加入负载平衡配置: http { upstream tomcat { server localhost:8080; server ...
- Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化
一.什么是负载均衡和反向代理 随着网站访问量的快速增长,单台服务器已经无法承担大量用户的并发访问,必须釆用多台服务器协同工作,以提高计算机系统的处理能力和计算强度,满足当前业务量的需求.而如何在完成同 ...
- iOS学习笔记46-Swift(六)扩展
一.Swift扩展 扩展就是向一个已有的类.结构体或枚举类型添加新功能,这包括在没有权限获取原始源代码的情况下扩展类型的能力.扩展和 Objective-C中的分类(category)类似,但是它要比 ...
- tomcat+nginx 横向扩展
1.分别在电脑上部署两个tomcat tomcat1 tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...
随机推荐
- linux 历史命令用法(转)
许多使用过Linux一段时间的人通过一些基础操作已经能够把Linux各方面基本玩转,但是如果没有经过系统学习的话就容易缺乏一些实战技巧.这系列文章介绍一些关于bash的能够提高效率的技巧,主要是关于历 ...
- python学习笔记10(函数一): 函数使用、调用、返回值
一.函数的定义 在某些编程语言当中,函数声明和函数定义是区分开的(在这些编程语言当中函数声明和函数定义可以出现在不同的文件中,比如C语言),但是在Python中,函数声明和函数定义是视为一体的.在Py ...
- Code for the Homework1
作业要求: http://www.cnblogs.com/bingc/p/4919692.html 代码(未使用Eigen): #include <iostream> #include & ...
- 微软Hololens设备 浅分析
微软Hololens的定位是一款MR 设备(Mixed reality).MR与AR的不同我认为是MR能够将真实环境的场景信息与虚拟对象进行完美的融合,它是基于SLAM(SimultaneousLoc ...
- 应该如何入门deep learning呢?从UFLDL开始!
抱歉,大家,这里不是要分享如何学习deep learning,而是想要记录自己学习deep learning的小历程,算是给自己的一点小动力吧,希望各位业内前辈能够多多指教! 看到有网友提到,Andr ...
- Hibernate - SQLQuery
使用SQLQuery 对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口.下面来描述如何使用这个API进行查询. 标量查询 ...
- sql删除wordpress没用的postmeta记录
支持多作者的wordpress博客,有时需要审核他们的文章内容,虽然UGC(User-generated content)整体是好的,但是也要控制一下质量,实在不相关或spam的文章就要毫不手软的删除 ...
- fiddler 插件开发二
本篇主要讲解Fildder插件开发中的涉及到的主要接口与类. 1.IFiddlerExtension 接口 如果要开发的自定义插件有UI界面,则需要实现IFiddlerExtension 接口.你程序 ...
- SDUT2191Calendar
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2191 题意:给你两个年月日,让你算出其中经历了 ...
- ECNU1101-Dinic
题意:从起点到终点有几条特殊路径. 特殊路径指的是:对于任意两条路径,他们的与起点相连的点是不同的点 && 与终点的相连的点是不同的点. /* 题意:从起点到终点有几条特殊路径. 特殊 ...