nginx的模組開發

2021-07-29 10:26:57 字數 3412 閱讀 5264

nginx剛剛在國內開始流行的時候, 我就把它引入公司技術體系,用來替代apache主要做動靜分離。

nginx的併發處理能力和穩定性,以及優秀的軟體架構深深得吸引了我, 讓我跨入了高效能伺服器開發的大門。

正巧當時要基於flash技術開發一套**系統, 而且要支援 **環境, 而當時並沒有什麼好的辦法讓flash通過socket並穿過**訪問**伺服器,

後來只能採用http技術去訪問**伺服器。為了開發乙個健 壯的,高併發的**伺服器,在技術選型時想到 了nginx的模組化機制,最終基於nginx開發了第一版**系統。

本文主要簡單舉例如何開發乙個nginx模組, 讓我們從hello world開始吧。

模組**ngx_http_cssweb_module.c

// 配置檔案引數

// 定義模組

// 讀取配置項

//丟失請求包內容

//構造響應內容

ngx_str_t response = ngx_string("hello the world");

b = ngx_create_temp_buf(r->pool, response.len);

if (b == null)

ngx_memcpy(b->pos, response.data, response.len);

b->last = b->pos + response.len;

b->last_buf = 1;

out.buf = b;

out.next = null;

//構造響應頭

//傳送響應頭

//傳送響應內容

}編譯配置檔案config

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 /

location /cssweb

執行伺服器

sudo ./nginx

結果如下

Nginx 模組開發 Hello World

最近在研究如何利用 nginx 實現高效能閘道器,這裡記錄一下開發 nginx 擴充套件模組 hello world。wget tar zvxf nginx 1.13.10.tar.gz cd nginx 1.13.10編譯,安裝 nginx 到指定目錄 configure prefix home ...

Nginx模組開發(1) 初探模組開發流程

一 準備工作 1 安裝pcre庫 cd usr local wget tar zxvf pcre 8.38.tar.gz cd pcre 8.38 configure make make install 2.安裝zlib cd usr local wget tar zxvf zlib 1.2.8.t...

開發記錄 Nginx模組開發 二

引言 nginx模組提供了upstream 上游伺服器 的非同步 處理模組開發,以memcached 模組為例 commands結構的初始化為相應的命令新增 函式,用來處理 memcached pass 這個命令 看看 ngx http memcached pass 的實現 此函式會對 conf 做...