步骤

1. 新建模块目录
2. 添加模块配置文件
3. 编写模块源码文件
4. 在主配置文件中配置访问location
5. 编译加入模块文件
6. 测试

新建模块目录

mkdir /opt/nginx/ext/hello_world/

  

添加模块配置文件

vim /opt/nginx/ext/hello_world/config

# 添加如下内容
ngx_addon_name=ngx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
CORE_LIBS="$CORE_LIBS -lpcre" # ngx_addon_name 表示模块名称
# HTTP_MODULES 表示定义HTTP_MODULES模块,多个模块以空格分开,为了不覆盖之前的模块,使用$HTTP_MODULES代表已经存在的
# NGX_ADDON_SRCS 代表模块代码路径
# CORE_LIBS 代表模块需要的库文件 

编写模块文件

文件直接放在模块目录下即可(vim /opt/nginx/ext/hello_world/ngx_http_hello_world_module.c)

定义HTTP模块

static ngx_command_t ngx_http_hello_world_commands[] = {
{
ngx_string("hello_world"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | \
NGX_CONF_NOARGS,
ngx_http_hello_world,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
}; // hello_world: 这个与nginx.conf中定义的location中的名字一致
// NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS: 指定出现的位置,可以出现在location {}中
// nginx_http_hello_world: 这个ngx_command_t中的set成员,当某块位置出现hello_world时候,nginx将会启动ngx_http_hello_world方法,所以需要定义该函数 static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
}; ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; // NGX_MODULE_V1 代表版本号
// ngx_http_hello_world_module_ctx 代表模块上下文结构,HTTP框架要求,固定用法
// ngx_http_hello_world_commands 代表模块的配置文件参数,这个在nginx.conf文件解析
// NGX_HTTP_MODULE 代表这是一个http模块

  

定义模块触发函数

static char * ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf ;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_world_handler;
return NGX_CONF_OK;
} // ngx_http_conf_get_module_loc_conf: 这个函数会首先找到hello_world配置所属的配置块
// handler: http框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段,如果请求的主机域名,URL和hello_world相匹配,那么就调用这handler指向的方法ngx_http_hello_world_handler

  

编写请求处理方法handler

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
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; rc = ngx_http_send_header(r);//发送头部
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
} 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 = 1; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);//向用户发送响应包
}

  

完整的代码汇总

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r); static char* ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_command_t ngx_http_hello_world_commands[] = {
{
ngx_string("hello_world"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | \
NGX_CONF_NOARGS,
ngx_http_hello_world,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
}; static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
}; ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
}; static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf->handler = ngx_http_hello_world_handler; return NGX_CONF_OK;
} static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) {
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; rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
} 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 = 1; ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);
}

  

在主配置文件中配置访问location

vim /etc/nginx

添加 location / {hello_world};
server {
...
location / {
hello_world;
} ...
}

  

编译加入模块文件

./configure --add-module=/opt/nginx/ext/hello_world/

make && make install

  

测试

/usr/local/nginx/sbin/nginx

curl http://localhost

  

1. nginx添加自定义http模块(简单)的更多相关文章

  1. Nginx SPDY Pagespeed模块编译——加速网站载入

    在看<Web性能权威指南>的时候,看到了SPDY这货,于是便开始折腾起了这个了,也顺便把pagespeed加了进去. Nginx SPDY 引自百科~~ SPDY(读作“SPeeDY”)是 ...

  2. OSX安装nginx和rtmp模块(rtmp直播服务器搭建)

    1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  3. nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。

    ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...

  4. nginx源代码分析--模块分类

    ngx-modules Nginx 基本的模块大致能够分为四类: handler – 协同完毕client请求的处理.产生响应数据.比方模块, ngx_http_rewrite_module, ngx ...

  5. Nginx安装echo模块

    echo-nginx-module 模块可以在Nginx中用来输出一些信息,可以用来实现简单接口或者排错. 项目地址:https://github.com/openresty/echo-nginx-m ...

  6. Nginx反向代理的简单实现

    1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...

  7. 开始Nginx的SSL模块

    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...

  8. nginx 增加 lua模块

    Nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: ./configure –wi ...

  9. Nginx开发HTTP模块入门

    Nginx开发HTTP模块入门 我们以一个最简单的Hello World模块为例,学习Nginx的模块编写.假设我们的模块在nginx配置文件中的指令名称为hello_world,那我们就可以在ngi ...

随机推荐

  1. EF的优缺点

    优点: 1.简洁的Linq to Sql语句大大提高了开发人员的效率,不要再写复杂的sql语句: 2.不再需要再管应用程序如何去连接数据库: 3.EF可以用作用于数据服务和OData Service的 ...

  2. hadoop搭建笔记(一)

    环境:mac/linux hadoop版本:3.1.1 安装特性:非HA 准备: 1. jdk8以上 2. ssh 3. 下载hadoop安装包 配置文件,这里都只有简易配置: 1. core-sit ...

  3. Python中常见的正则表达式符号

    ?  匹配零次或一次前面的分组 *   匹配零次或多次前面的分组 +  匹配一次或多次前面的分组 {n} 匹配n次前面的分组 {n,} 匹配n次或更多次前面的分组 {,m} 匹配零次到m次前面的分组 ...

  4. [CentOS_7.4]Linux编译安装ffmpeg

    [CentOS_7.4]Linux编译安装ffmpeg   安装过程: 下载安装源,配置,编译,安装,设置环境变量. # wget http://www.ffmpeg.org/releases/ffm ...

  5. Evaluate X and Y returned from the differential equation solvers using printput frequency in Python的代码

    把内容过程中经常用到的一些内容段做个备份,如下的内容是关于Evaluate X and Y returned from the differential equation solvers using ...

  6. Asp.net core Identity + identity server + angular 学习笔记 (第一篇)

    用了很长一段时间了, 但是一直没有做过任何笔记,感觉 identity 太多东西要写了, 提不起劲. 但是时间一久很多东西都记不清了. 还是写一轮吧. 加深记忆. 这是 0-1 的笔记, 会写好多篇. ...

  7. OO面向对象第一单元总结

    OO面向对象第一单元总结(表达式求导) 写在前面: 魔鬼课程oo第一单元终于结束,当终究要落笔总结,竟不知从何写起…… 回首再去看第一次的作业,你会满足于那时的幸福,或许,这就是成长吧! 千言万语,一 ...

  8. 不安全代码只会在使用 /unsafe 编译的情况下出现

    在你的项目属性页面里面,把是否包含unsafe代码的选项选上

  9. linux命令:使用less从后向前查看日志信息

    线上出问题的时候,我们常用tail-n 或者tail-f或者grep或者vicat等各种命令去查看异常信息,但是日志是在不停地刷屏,tail是动态的在变的,我们往往期望从日志最后一行往前一页一页的翻页 ...

  10. Myeclipse和idea对比

    新入的公司要用myeclipse,没办法,只能跟着队伍走.(myeclipse以下简写为me) 1.myeclipse的快捷键并不能设置鼠标滚轮之类的,之前在idea上配置滚轮下滚展开package, ...