详细资料:<深入理解Nginx>

1.编写第三方模块

建立文件夹hello,里面有两个文件:

ngx_http_mytest_module.c

 #include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static char *
ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf);
static ngx_int_t
ngx_http_mytest_handler(ngx_http_request_t *r); static ngx_command_t ngx_http_mytest_commands[]={
{
//配置项名称
ngx_string("mytest"),
//配置项类型(可出现的位置,参数的个数)
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
//出现了name中指定的配置项后,将会调用该方法处理配置项的参数
ngx_http_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
,
NULL
},
ngx_null_command
}; static char *
ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf)
{
//找到mytest配置项所属的配置块
ngx_http_core_loc_conf_t *clcf;
clcf=ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
/*
HTTP框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时
如果请求的主机域名、URI与mytest配置项所在的配置块相匹配,
就将调用我们事先的ngx_http_mytest_handler方法处理这个请求
*/
clcf->handler=ngx_http_mytest_handler;
return NGX_CONF_OK;
} static ngx_http_module_t ngx_http_mytest_module_ctx={
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
}; ngx_module_t ngx_http_mytest_module={
NGX_MODULE_V1,
//指向ngx_http_module_t结构体
&ngx_http_mytest_module_ctx,
//用来处理nginx.conf中的配置项
ngx_http_mytest_commands,
//表示该模块的类型
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
//判断请求方法是否为GET或者HEAD,否则返回405 Not Allowed
if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))){
return NGX_HTTP_NOT_ALLOWED;
} //丢弃请求中的包体
ngx_int_t rc=ngx_http_discard_request_body(r);
if(rc!=NGX_OK){
return rc;
} //设置相应头
ngx_str_t type=ngx_string("text/plain");
ngx_str_t response=ngx_string("Hello World!");
r->headers_out.status=NGX_HTTP_OK;
r->headers_out.content_length_n=response.len;
r->headers_out.content_type=type; //发送HTTP头部
rc=ngx_http_send_header(r);
if(rc==NGX_ERROR||rc>NGX_OK||r->header_only){
return rc;
} //构造ngx_buf_t结构体准备发送包体
ngx_buf_t *b;
b=ngx_create_temp_buf(r->pool,response.len);
if(b==NULL){
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memcpy(b->pos,response.data,response.len);
b->last=b->pos+response.len;
b->last_buf=; //构造发送时的ngx_chain_t结构体
ngx_chain_t out;
out.buf=b;
out.next=NULL;
return ngx_http_output_filter(r,&out);
}

config

 ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

2.编译第三方模块(nginx源码目录下)

在未安装nginx的情况下安装第三方模块

# ./configure --prefix=/usr/local/nginx \
--add-module=/第三方模块目录

在已安装nginx情况下安装第三方模块

# ./configure --prefix=/usr/local/nginx \
--add-module=/第三方模块目录
# make
# /usr/local/nginx/sbin/nginx -s stop
# cp objs/nginx /usr/local/nginx/sbin/nginx
# /usr/local/nginx/sbin/nginx

3.使用第三方模块

使用一个模块需要根据这个模块定义的配置指令来做。比如我们这个简单的hello handler module的时候就很简单。

在nginx.conf文件中在http里面的默认的server里面加入如下配置

location /test {
mytest;
}

4.使用telnet查看结果

Nigix加入Hello World模块的更多相关文章

  1. Nginx - upstream 模块及参数测试

    目录 - 1. 前言- 2. 配置示例及指令说明    - 2.1 配置示例    - 2.2 指令    - 2.3 upstream相关变量- 3. 参数配置及测试    - 3.1 max_fa ...

  2. Python学习之==>常用模块

    一.string模块 import string print(string.ascii_letters) # 所有大小写字母 print(string.ascii_lowercase) # 所有小写字 ...

  3. Vue+Axios+Nigix+SpringCloud前端和后端搭建及其碰到的问题

    一.Axios.Router的安装和使用 1.如何安装Axios和Router 1).进入到工程所在的文件夹,通过cmd指令,进入到window的dos界面 2).输入:npm install axi ...

  4. npm 私有模块的管理使用

    你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...

  5. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  6. ES6模块import细节

    写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...

  7. Python标准模块--ContextManager

    1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...

  8. Python标准模块--Unicode

    1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...

  9. Python标准模块--Iterators和Generators

    1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...

随机推荐

  1. loj 数列分块入门 5 7 8

    5 题意 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间开方,区间求和. 思路 用\(tag\)记录这一块是否已全为\(1\). 除分块外,还可用 树状数组+并查集(链表) 或者 线 ...

  2. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.Exec

    Error:Execution failed for task ':app:transformClassesWithDexForDebug'.> com.android.build.api.tr ...

  3. studio构建错误Element uses-permission#android.permission.ACCESS_NETWORK_STATE at AndroidManifest.xml:38:5-79 dupli

    今天在项目构建的时候遇到了如下报错: Element uses-permission#android.permission.ACCESS_NETWORK_STATE at AndroidManifes ...

  4. Linux平台用C++实现事件对象,同步线程

    前文在Win32平台上用C++实现了事件对象Event,对线程进行同步,以达到期望目的.这次在Linux平台上实现与之类似的事件对象.与其相关的一组API包括:pthread_mutex_init,p ...

  5. springBoot开启热部署

    springBoot开启热部署 这里使用devtools工具开启热部署 〇.搭建springbboot基础环境 一.添加依赖 <dependency> <groupId>org ...

  6. Sax解析xml及pull解析xml

    sax解析参考:http://www.iteye.com/topic/763895: 说明:测试时报空指针异常,未能读取到数据,关注Sax解析的过程及API即可: pull解析参考:http://ww ...

  7. Selenium2+python自动化1-环境搭建【转载】

    前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以在学习前先要弄清楚版本号,这点非常重要.本系列依然以selenium2为基础,目前selenium3坑比较多,暂 ...

  8. Visual Studio跨平台开发(1):Hello Xamarin!

    前言 应用程序发展的脚步, 从来没有停过. 从早期的Windows 应用程序, 到网络时代的web 应用程序, 再到近几年相当盛行的行动装置应用程序(Mobile Application), 身为C# ...

  9. 如何将离线的PIP安装包快速安装好

    先将已安装的组件通过pip freeze require.txt导出. 将require里需要的安装包放到一个独立目录下. 然后运行命令: pip install --no-index --find- ...

  10. Android studio配置使debug签名和release签名一致

    在module的build.gradle中添加 android { //重要部分 signingConfigs { release { keyAlias 'jxt' keyPassword '1234 ...