Nginx 過濾模組
HTTP過濾模組的開發步驟
- 確定原始碼檔案名稱;
- 建立config腳本,執行configure時將該目錄加入;結構;
- 透過設定ngx_module_t結構中的ngx_command_t數組來處理感興趣的配置項目;
- 實作初始化函數,初始化方法就是將ngx_http_output_header_filter_t和ngx_http_output_body_filter_http_output_header_filter_t和ngx_http_output_body_filter_http_output_header_filter_t和ngx_http_output_body_filter_http_filter_filter_Het; ngx_http_output_body_filter_pt函式;
- 編譯安裝後,修改nginx.conf設定檔中的過濾模組選項,開啟與否。
- 設定腳本
<code>ngx_add HTTP_FILTER_MODULES=<span>"<span>$HTTP_FILTER_MODULES</span> ngx_http_myfilter_module"</span> NGX_ADD>"<span>$NGX_ADDON_SRCS</span><span>$ngx_addon_dir</span>/ngx_http_myfilter_module.c"</code>
<code><span>#include <..></..></span><span>#include <..></..></span><span>#include <..></..></span><span>//Declare</span><span>static</span> ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *); <span>static</span> ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t*,ngx_chain_t*); <span>static</span> ngx_http_output_header_filter_pt ngx_http_next_header_filter; <span>static</span> ngx_http_output_body_filter_pt ngx_http_next_body_filter; <span>//On/Off</span><span>typedef</span><span>struct</span> { ngx_flag_t enable; }ngx_http_myfilter_conf_t; <span>static</span><span>void</span> *ngx_http_myfilter_create_conf(ngx_conf_t *cf){ ngx_http_myfilter_conf_t *mycf; <span>//Allocate memory</span> mycf = (ngx_http_myfilter_conf_t*)ngx_pcalloc(cf->pool,<span>sizeof</span>(ngx_http_myfilter_conf_t)); <span>if</span>(mycf == <span>NULL</span>) <span>return</span><span>NULL</span>; mycf->enable = NGX_CONF_UNSET; <span>return</span> mycf; } <span>static</span><span>char</span> * ngx_http_myfilter_merge_conf(ngx_conf_t *cf,<span>void</span> *parent,<span>void</span> *child){ ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t*)parent; ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t*)child; ngx_conf_merge_value(conf->enable,prev->enable,<span>0</span>); <span>return</span> NGX_CONF_OK; } <span>/* ------------------------------------------- */</span><span>//State for prefix</span><span>typedef</span><span>struct</span> { <span>/* add_prefix = * 0 the filter module is off * 1 can add prefix * 2 has been added prefix already */</span> ngx_int_t add_prefix; } ngx_http_myfilter_ctx_t; <span>//Analyse configure</span><span>static</span> ngx_command_t ngx_http_myfilter_commands[]={ {ngx_string(<span>"add_prefix"</span>), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_myfilter_conf_t,enable), <span>NULL</span>}, ngx_null_command }; <span>static</span> ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf){ <span>//Insert before the first node</span> ngx_http_next_header_filter = ngx_http_top_header_filter; ngx_http_top_header_filter = ngx_http_myfilter_header_filter; ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_myfilter_body_filter; <span>return</span> NGX_OK; } <span>/* ------------------------------------------- */</span><span>//Parse</span><span>static</span> ngx_http_module_t ngx_http_myfilter_module_ctx = { <span>NULL</span>, ngx_http_myfilter_init, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, ngx_http_myfilter_create_conf, ngx_http_myfilter_merge_conf }; <span>/* ------------------------------------------- */</span><span>//Module information</span> ngx_module_t ngx_http_myfilter_module = { NGX_MODULE_V1, &ngx_http_myfilter_module_ctx, ngx_http_myfilter_commands, NGX_HTTP_MODULE, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, <span>NULL</span>, NGX_MODULE_V1_PADDING }; <span>/* ------------------------------------------- */</span><span>static</span> ngx_str_t filter_prefix = ngx_string(<span>"[my filter module]"</span>); <span>//Filter to process the header</span><span>static</span> ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *r){ ngx_http_myfilter_ctx_t *ctx; ngx_http_myfilter_conf_t *conf; <span>if</span>(r->headers_out<span>.status</span> != NGX_HTTP_OK){ <span>return</span> ngx_http_next_header_filter(r); } <span>//Get context</span> ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module); <span>if</span>(ctx){ <span>return</span> ngx_http_next_header_filter(r); } <span>//Get configure by ngx_http_myfilter_conf_t</span> conf = ngx_http_get_module_loc_conf(r,ngx_http_myfilter_module); <span>if</span>(conf->enable == <span>0</span>){<span>//add_prefix off</span><span>return</span> ngx_http_next_header_filter(r); } <span>//Create ngx_http_myfilter_ctx_t</span> ctx = ngx_pcalloc(r->pool,<span>sizeof</span>(ngx_http_myfilter_ctx_t)); <span>if</span>(ctx == <span>NULL</span>){ <span>return</span> NGX_ERROR; } <span>//add_prefix = 0 express do not add prefix</span> ctx->add_prefix = <span>0</span>; <span>//Set context</span> ngx_http_set_ctx(r,ctx,ngx_http_myfilter_module); <span>//myfilter module only figure out Content-Type="text/plain"</span><span>if</span>(r->headers_out<span>.content_type</span><span>.len</span>>=<span>sizeof</span>(<span>"text/plain"</span>) - <span>1</span> && ngx_strncasecmp(r->headers_out<span>.content_type</span><span>.data</span>,(u_char*)<span>"text/plain"</span>,<span>sizeof</span>(<span>"text/plain"</span>)-<span>1</span>)==<span>0</span>){ <span>//Set add_prefix</span> ctx->add_prefix = <span>1</span>; <span>if</span>(r->headers_out<span>.content_length_n</span>><span>0</span>) r->headers_out<span>.content_length_n</span> += filter_prefix<span>.len</span>; } <span>return</span> ngx_http_next_header_filter(r); } <span>//Filter to process the body</span><span>static</span> ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t *r,ngx_chain_t *in){ ngx_http_myfilter_ctx_t *ctx; ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module); <span>//If cannot get context or the add_prefix is 0/2,do not process</span><span>if</span>(ctx == <span>NULL</span> || ctx->add_prefix !=<span>1</span>) <span>return</span> ngx_http_next_body_filter(r,in); <span>//To make sure never add prefix again</span> ctx->add_prefix = <span>2</span>; <span>//Get prefix string</span> ngx_buf_t *b = ngx_create_temp_buf(r->pool,filter_prefix<span>.len</span>); b->start = b->pos = filter_prefix<span>.data</span>; b->last = b->pos + filter_prefix<span>.len</span>; <span>//Get and set ngx_chain_t list at the bginning</span> ngx_chain_t *cl = ngx_alloc_chain_link(r->pool); cl->buf = b; cl->next = in; <span>return</span> ngx_http_next_body_filter(r,cl); }</code>
設定檔nginx.conf
在http{}中加上
add_prefix on;開啟過濾模組
編譯
<code>./configure --<span>add</span>-<span>module</span>=模块路径/ make make install</code>
HTTP過濾模組也是HTTP模組,所以HTTP模組的書寫大致也是這樣。
版權聲明:Pain is just in your mind.

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器