nginx刚刚在国内开始流行的时候, 我就把它引入公司技术体系,用来替代apache主要做动静分离。

nginx的并发处理能力和稳定性,以及优秀的软件架构深深得吸引了我, 让我跨入了高性能服务器开发的大门。

正巧当时要基于flash技术开发一套行情系统, 而且要支持 代理环境, 而当时并没有什么好的办法让flash通过socket并穿过代理访问行情服务器,

后来只能采用http技术去访问行情服务器。为了开发一个健 壮的,高并发的行情服务器,在技术选型时想到 了nginx的模块化机制,最终基于nginx开发了第一版行情系统。

本文主要简单举例如何开发一个nginx模块, 让我们从hello world开始吧。

模块代码ngx_http_cssweb_module.c

#include <stdio.h>
#include <stdlib.h>#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>static char *ngx_http_cssweb_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_cssweb_init_master(ngx_log_t *log);
static ngx_int_t ngx_http_cssweb_init_module(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_cssweb_init_process(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_cssweb_init_thread(ngx_cycle_t *cycle);
static void ngx_http_cssweb_exit_thread(ngx_cycle_t *cycle);
static void ngx_http_cssweb_exit_process(ngx_cycle_t *cycle);
static void ngx_http_cssweb_exit_master(ngx_cycle_t *cycle);static ngx_int_t ngx_http_cssweb_handler(ngx_http_request_t *r);// 配置文件参数
static ngx_command_t  ngx_http_cssweb_commands[] = {{ ngx_string("ngx_cssweb_module"),NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,ngx_http_cssweb_set,NGX_HTTP_LOC_CONF_OFFSET,0,NULL },ngx_null_command
};static ngx_http_module_t  ngx_http_cssweb_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_cssweb_module = {NGX_MODULE_V1,&ngx_http_cssweb_module_ctx,              /* module context */ngx_http_cssweb_commands,                 /* module directives */NGX_HTTP_MODULE,                       /* module type */ngx_http_cssweb_init_master,                                  /* init master */ngx_http_cssweb_init_module,                                  /* init module */ngx_http_cssweb_init_process,             /* init process */ngx_http_cssweb_init_thread,                                  /* init thread */ngx_http_cssweb_exit_thread,                                  /* exit thread */ngx_http_cssweb_exit_process,             /* exit process */ngx_http_cssweb_exit_master,                                  /* exit master */NGX_MODULE_V1_PADDING
};// 读取配置项
static char * ngx_http_cssweb_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);/* register hanlder */clcf->handler = ngx_http_cssweb_handler;return NGX_CONF_OK;
}static ngx_int_t ngx_http_cssweb_init_master(ngx_log_t *log)
{fprintf(stderr, "\n ngx_http_cssweb_init_master \n");return NGX_OK;
}static ngx_int_t ngx_http_cssweb_init_module(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_init_module \n");return NGX_OK;
}static ngx_int_t ngx_http_cssweb_init_process(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_init_process \n");return NGX_OK;
}static ngx_int_t ngx_http_cssweb_init_thread(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_init_thread \n");return NGX_OK;
}static void ngx_http_cssweb_exit_thread(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_exit_thread \n");return;
}static void ngx_http_cssweb_exit_process(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_exit_process \n");return;
}static void ngx_http_cssweb_exit_master(ngx_cycle_t *cycle)
{fprintf(stderr, "\n ngx_http_cssweb_exit_master \n");return;
}static ngx_int_t ngx_http_cssweb_handler(ngx_http_request_t *r)
{ngx_int_t     rc;ngx_buf_t    *b;ngx_chain_t   out;//只处理GET和HEAD请求if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {return NGX_HTTP_NOT_ALLOWED;}//丢失请求包内容 rc = ngx_http_discard_request_body(r);if (rc != NGX_OK) {return rc;}//构造响应内容 ngx_str_t response = ngx_string("hello the world");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;out.buf = b;out.next = NULL;//构造响应头ngx_str_t type = ngx_string("text/plain");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;}//发送响应内容 return ngx_http_output_filter(r, &out);
}

编译配置文件config

ngx_addon_name=ngx_http_cssweb_module

HTTP_MODULES="$HTTP_MODULES ngx_http_cssweb_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_cssweb_module.c"

ngx_http_cssweb_module.c和config文件位于/usr/local/src/nginx_module目录下面。

编译自定义模块

./configure --prefix=/usr/local/nginx-1.10.3 --add-module=/usr/local/src/nginx_module

make

sudo make install

编辑配置文件nginx.conf, 加入/cssweb的定义

location / {
            root   html;
            index  index.html index.htm;
        }

location /cssweb {
             ngx_cssweb_module;
        }

运行服务器

sudo ./nginx

结果如下

nginx的模块开发相关推荐

  1. Nginx服务模块开发

    纵观网上各种关于nginx的模块开发的资料都是基于HTTP的模块开发,这里就整理一篇是基于TCP协议的服务模块开发,并且给出一套自定义服务模块的源码,该模块主要实现的功能是监听某一端口,然后把接收到的 ...

  2. 《深入理解Nginx:模块开发与架构解析》一1.2 为什么选择Nginx

    1.2 为什么选择Nginx 为什么选择Nginx?因为它具有以下特点: (1)更快 这表现在两个方面:一方面,在正常情况下,单次请求会得到更快的响应:另一方面,在高峰期(如有数以万计的并发请求),N ...

  3. 《深入理解Nginx:模块开发与架构解析》一3.3 如何将自己的HTTP模块编译进Nginx...

    3.3 如何将自己的HTTP模块编译进Nginx Nginx提供了一种简单的方式将第三方的模块编译到Nginx中.首先把源代码文件全部放到一个目录下,同时在该目录中编写一个文件用于通知Nginx如何编 ...

  4. 《深入理解Nginx:模块开发与架构解析》一1.6 Nginx的命令行控制

    1.6 Nginx的命令行控制 在Linux中,需要使用命令行来控制Nginx服务器的启动与停止.重载配置文件.回滚日志文件.平滑升级等行为.默认情况下,Nginx被安装在目录/usr/local/n ...

  5. 推荐我的新书《深入理解Nginx:模块开发与架构解析》

    http://www.china-pub.com/STATIC/zt_mb/zt_huodong_2013_3.asp?filename=2013_jsj_nginx_20130401 目录 < ...

  6. Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  7. 深入理解Nginx 模块开发与架构解析-陶辉 读书笔记

    前言 1. nginx是一个优秀的事件驱动框架,nginx非常适合开发在传输层以TCP对外提供服务的服务器程序.基于nginx框架开发程序有5个优势: * nginx将网络.磁盘及定时器等异步事件的驱 ...

  8. Nginx 模块开发

    Nginx 模块概述 Nginx 模块有三种角色: 处理请求并产生输出的 Handler 模块: 处理由 Handler 产生的输出的 Filter(滤波器)模块: 当出现多个后台服务器时,Load- ...

  9. 《深入理解NGINX 模块开发与架构解析》之摘抄学习

    1.基于Nginx框架开发程序有5个优势: (1).Nginx将网络.磁盘及定时器等异步事件的驱动都做了非常好的封装,基于它开发将可以忽略这些事件处理的细节; (2).Nginx封装了许多平台无关的接 ...

最新文章

  1. python3.x安装cv2失败
  2. Python下ImportError: DLL load failed: 找不到指定的模块
  3. IoT Studio 有大事宣布
  4. 第八十四节,css布局小技巧及font-awesome图标使用
  5. 【渝粤题库】国家开放大学2021春1009离散数学(本)题目
  6. maple 2018 窗口关闭提示乱码_如果解决SOLIDWORKS工程图转CAD字体出现乱码的问题_SolidWorks生信科技...
  7. Python中dir,hasattr,getattr,setattr,vars的使用
  8. 利用PCF8591进行AD转换
  9. js获取本月或指定月份的最后一天
  10. 论文阅读:A Progressive Architecture With Knowledge Review Network for Salient Object Detection
  11. 失去老罗,张一鸣的坚果手机多了什么?
  12. 【Python学习日记】迭代器
  13. tomcat session共享问题
  14. 人工智能终将自我进化?排除无用功能,让自己更加完美?
  15. 游戏开发物语方案点数分配_《游戏开发物语》官方超全新手攻略和名词解析!...
  16. 揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
  17. 好佳居软装十大品牌 软装可以提升格调
  18. 超级详细Ubuntu20.04.5系统下编译安装OpenCV with ffmpeg4.2.7、NVIDIA显卡驱动515、cuda11.5、cudnn8.3.3并编译使用darknet-yolov4
  19. 将Virtualbox虚拟机转换格式并导入Hyper-V
  20. hexo solar主题 + github搭建个人博客(二)----基本信息、头像图标、联系方式、导航栏

热门文章

  1. PHP 杂谈《重构-改善既有代码的设计》之二 对象之间搬移特性
  2. c++中std::set自定义去重和排序函数
  3. 2020ICPC(上海) - Sum of Log(数位dp)
  4. CodeForces - 618D Hamiltonian Spanning Tree(思维+贪心)
  5. 打开服务器数据库文件,如何打开服务器中的数据库文件
  6. java 妖魔道-倩女幽魂_《倩女幽魂I-妖魔道》地图新手功略
  7. 程序员初试和复试_程序员因肌肉发达面试被质疑能力,网友:这做程序员有啥关系呢?...
  8. python制作gif动画_实用的Python(2)利用Python制作gif动图
  9. UVA11212Editing aBook 编辑书稿
  10. 数据挖掘竞赛-美国King County房价预测训练赛