文章目錄
- 一、handler簡介
- 二、Nginx handler模塊開發
- 2.1、示例代碼
- 2.2、編寫config文件
- 2.3、編譯模塊到Nginx源碼中
- 2.4、修改conf文件
- 2.5、執行效果
- 三、Nginx的熱更新
- 總結
一、handler簡介
Handler模塊就是接受來自客戶端的請求并產生輸出的模塊。
配置文件中使用location指令可以配置content handler模塊,當Nginx系統啟動的時候,每個handler模塊都有一次機會把自己關聯到對應的location上。
如果有多個handler模塊都關聯了同一個location,那么實際上只有一個handler模塊真正會起作用。
所以在開發階段應該避免多個handler模塊關聯同一個location的情況發生。
handler模塊處理的結果通常有三種情況:
- 處理成功。
- 處理失敗:處理的時候發生了錯誤。
- 拒絕處理:這個location的處理就會由默認的handler模塊來進行處理。
例如,當請求一個靜態文件的時候,如果關聯到這個location上的一個handler模塊拒絕處理,就會由默認的ngx_http_static_module模塊進行處理,該模塊是一個典型的handler模塊。
二、Nginx handler模塊開發
2.1、示例代碼
#include <ngx_config.h>
#include <ngx_http.h>
#include <ngx_core.h>#include <arpa/inet.h>
#include <netinet/in.h>/*
* ip的訪問次數存放在一個key-value數據結構里面,ip是key,value是統計的次數
* 可用的數據結構:
* hash
* rbtree
* 最簡單的是數組
*/
typedef struct {int count;struct in_addr addr;
}ngx_pv_table;ngx_pv_table pv_table[256] = { 0 }; //這只適合局域網內存儲,正在的數據結構最好用rbtree// 重新組織網頁 (網頁組包)
void ngx_encode_http_page(char *html)
{sprintf(html, "<h1>Hello, NGX handler! I am FLY.</h1>");strcat(html, "<h2>");int i = 0;for (i = 0; i < 256; i++) {if (pv_table[i].count != 0) {char str[INET_ADDRSTRLEN] = { 0 };char buffer[128] = { 0 };sprintf(buffer, "req from : %s, count: %d <br/>",inet_ntop(AF_INET, &pv_table[i].addr, str, sizeof(str)),pv_table[i].count);strcat(html, buffer);}}strcat(html, "</h2>");
}ngx_int_t ngx_http_count_handler(ngx_http_request_t *r)
{// 這里做統計功能// 獲取ip地址struct sockaddr_in *cliaddr = (struct sockaddr_in *)r->connection->sockaddr;// 地址和我們看到的是反著的,通過右移得到ip地址的末尾.符號后面那個位數int idx = cliaddr->sin_addr.s_addr >> 24;pv_table[idx].count++;memcpy(&pv_table[idx].addr, &cliaddr->sin_addr, sizeof(cliaddr->sin_addr));// 重新組織網頁u_char html[1024] = { 0 };int len = sizeof(html);ngx_encode_http_page((char*)html);/** 發送http響應*/r->headers_out.status = 200;ngx_str_set(&r->headers_out.content_type, "text/html");// 發送http 頭ngx_http_send_header(r);// 內存池拿出一個buffer的內存空間ngx_buf_t *b = ngx_palloc(r->pool, sizeof(ngx_buf_t));b->pos = html;b->last = html + len;b->memory = 1;//內存里操作b->last_buf = 1;//最后內存塊// 緩沖鏈ngx_chain_t out;out.buf = b;out.next = NULL;return ngx_http_output_filter(r, &out);}char *ngx_http_handler_count_set(ngx_conf_t *cf,ngx_command_t *cmd,void *conf)
{ngx_http_core_loc_conf_t *ccf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);// 設置handler的入口函數ccf->handler = ngx_http_count_handler;memset(pv_table, 0, sizeof(pv_table));return NGX_OK;
}// conf文件中的每一行都是一個指令指令
ngx_command_t ngx_http_handler_module_cmd[] = {{//命令名稱,比如listen,定義了就可以在conf文件中使用,注意不能和其他的起沖突ngx_string("count"),// 指示name命令放的位置在哪里以及可以帶多少個參數,NGX_CONF_FLAGE表示開關標志// predix on/offNGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,// 命令解析,可以使用nginx內部的也可以自己實現ngx_http_handler_count_set,//ngx_http_handler_set_slot,NGX_HTTP_LOC_CONF_OFFSET,0,NULL,},ngx_null_command
};// 用來解析對應的conf文件
static ngx_http_module_t ngx_http_handler_module_ctx = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
};// 模塊定義
ngx_module_t ngx_http_handler_module = {NGX_MODULE_V1,&ngx_http_handler_module_ctx,ngx_http_handler_module_cmd,// http的ascii值,指示是什么模塊NGX_HTTP_MODULE,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NGX_MODULE_V1_PADDING // 填充};
說明:
1、handler模塊必須提供一個真正的處理函數(即上文中的ngx_http_count_handler
),這個函數負責對來自客戶端請求的真正處理。這個函數的處理,既可以選擇自己直接生成內容,也可以選擇拒絕處理,由后續的handler去進行處理,或者是選擇丟給后續的filter進行處理。
這個處理函數的原型如下:
// r是http的請求,里面包含請求所有的信息
// 該函數處理成功返回NGX_OK,處理發生錯誤返回NGX_ERROR,拒絕處理(留給后續的handler進行處理)返回NGX_DECLINE。
// 返回NGX_OK也就代表給客戶端的響應已經生成好了,否則返回NGX_ERROR就發生錯誤了。
typedef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);
2.2、編寫config文件
創建:
touch config
內容:
ngx_addon_name=ngx_http_handler_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_handler_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_handler_module.c"
注意,config文件要和模塊的代碼在相同目錄。
2.3、編譯模塊到Nginx源碼中
(1)配置中添加模塊:
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_addition_module
--with-http_gzip_static_module --with-http_secure_link_module
--with-http_stub_status_module --with-stream --with-pcre=/home/fly/workspace/pcre-8.41
--with-zlib=/home/fly/workspace/zlib-1.2.11 --with-openssl=/home/fly/workspace/openssl-1.1.0g
--add-module=/mnt/hgfs/sourcecode_learning/ngx_http_handler_module
注意模塊路徑要正確。出現如下表示成功:
configuring additional modules
adding module in /mnt/hgfs/sourcecode_learning/ngx_http_handler_module+ ngx_http_handler_module was configured
creating objs/Makefile
(2)查看是否添加模塊到動態代碼中:
cat objs/ngx_modules.c
(3)編譯安裝:
make
sudo make install
2.4、修改conf文件
conf文件添加count;
worker_processes 4;events {worker_connections 1024;
}http {upstream backend {server 192.168.7.146:8889;server 192.168.7.146:8890;}server {listen 8888;location / {proxy_pass http://backend;}}server {listen 8889;location / {count;}}server {listen 8890;}server {listen 8891;}}
2.5、執行效果
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fly.conf
在網頁輸入IP和端口,執行效果如下:
可以看到,返回的網頁中多出了訪問次數統計。
三、Nginx的熱更新
(1)conf文件熱更新:通過reload指令進行重新加成conf文件。reload過程中是重新開啟新的進程來加載新的conf文件;比如原來有4個進程在運行,加載新的conf文件時就重新開啟4個進程來加載新的配置文件。
(2)可執行程序的熱更新:編譯安裝新的nginx,會把原來的nginx重命名為nginx.old,然后調用nginx reload就會更新。
總結
- 上述代碼雖然實現了IP訪問服務器的流量統計;但是,Nginx是多進程的,上述示例代碼沒有實現統計數在進程間的共享,這回造成其他進程是重新計數的問題。解決這個問題可以使用共享內存的方式在進程間通信。
- 上述代碼使用了最簡單的數據結構:數組。這不是好的決策,可以將其改為紅黑樹。
- nginx的handler模塊開發也可以用在黑白名單的處理(比如當判斷到同一個ip發送多個無效請求,可以將其加入到黑名單中)。