博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx的模块开发
阅读量:2389 次
发布时间:2019-05-10

本文共 5528 字,大约阅读时间需要 18 分钟。

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

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

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

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

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

模块代码ngx_http_cssweb_module.c

#include 
#include
#include
#include
#include
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

结果如下

你可能感兴趣的文章
SAP GUI里Screen Painter的工作原理
查看>>
如何处理错误消息Please install the Linux kernel header files
查看>>
使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm
查看>>
使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据
查看>>
如何处理错误消息Please install the gcc make perl packages
查看>>
让您的Eclipse具有千变万化的外观
查看>>
Red Hat Enterprise Linux(RHEL)中yum的repo文件详解
查看>>
安装指定版本的minikube
查看>>
ABAP的Package interface, 安卓的manifest.xml和Kubernetes的Capabilities
查看>>
webpack最简单的入门教程里bundle.js之运行单步调试的原理解析
查看>>
在Linux系统里安装Virtual Box的详细步骤
查看>>
ABAP和Java单例模式的攻防
查看>>
在Ubuntu操作系统里安装Docker
查看>>
用代码判断当前系统是否支持某个版本的feature
查看>>
ABAP system landscape和vue项目webpack构建的最佳实践
查看>>
如何处理VirtualBox启动错误消息:The vboxdrv kernel module is not loaded
查看>>
使用Kubernetes里的job计算圆周率后2000位
查看>>
C4C Product Price List的模型中和有效期相关的两个字段
查看>>
SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI
查看>>
如何在CRM WebClient UI里使用HANA Live Report
查看>>