步骤

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. 动态生成具有嵌套属性的linq选择(select)

    class SelectItem { public string Item { get; set; } } class SelectList { public int ID { get; set; } ...

  2. OpenVPN部署,实现访问云服务器的内网

    本教程不描述如何FQ 一.OpenVPN服务端部署 $ yum -y install net-tools lzo lzo-devel openssl-devel pam-devel gcc gcc-c ...

  3. loadrunner场景之集合点设置技巧

    在loadrunner的虚拟用户中,术语concurrent(并发)和simultaneous(同时)存在一些区别,concurrent 是指虚拟场景中参于运行的虚拟用户. 而simultaneous ...

  4. PCA和PCoA

    讲解很详细:http://blog.genesino.com/2016/10/PCA/ PCA分析一般流程: 中心化(centering, 均值中心化,或者中位数中心化),定标(scale,如果数据没 ...

  5. Lucene配置环境变量

    更详细的内容请参考:http://www.cnblogs.com/itcsl/p/6804954.html 以下是参照上面的操作方式来说明的,首先下载lucene-6.2.1.zip文件,这个网上有的 ...

  6. 浙江省住房和城乡建设厅 http://www.zjjs.com.cn/ 漏洞提示

    http://220.189.211.52/zjjsgbxx/FileAttach/96dcdf11-c45e-4455-a443-f6dea8a44e23.html 可以下载,浏览 改修,

  7. Chrome的Copy Selector中带有\3

    F12,右键copy selector就是这个结果. 查询到如下资料: https://stackoverflow.com/questions/20043347/css-css-coding-that ...

  8. 用servlet验证密码2

    function createXMLHttpRequest() { var XMLHttpRequest1; if (window.XMLHttpRequest) { XMLHttpRequest_t ...

  9. mock js使用方法简单记录

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 关于C++中Hash的应用

    本文只介绍我们在C++中如何使用Hash这种数据结构达到我们编程的目的,有关Hash的概念和实现不做详谈. C++11新增了一类散列容器包括unordered_set, unordered_map, ...