您的位置:首页 > 运维架构 > Nginx

nginx的模块开发

2017-03-23 18:07 162 查看
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

结果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: