生活随笔
收集整理的這篇文章主要介紹了
nginx的模块开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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的模块开发的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。