這篇文章主要介紹了nginx日誌中添加請求的response日誌,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
由於此功能在nginx內置的功能中沒有,需要安裝第三方模組ngx_lua,由於此模組需要Lua語言,所以需要安裝對應的Lua語言套件
1. 下載安裝LuaJIT
# cd /usr/local/src # wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz # tar -xzvf LuaJIT-2.0.2.tar.gz # cd LuaJIT-2.0.2 # make
出現如下內容表示編譯成功
OK Successfully built LuaJIT make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src' ==== Successfully built LuaJIT 2.0.2 ==== # make install
#出現如下內容,表示安裝成功
==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2. 下載準備nginx lua模組
# cd /usr/local/src # wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz # tar -xzvf v0.8.6
#3. 安裝nginx
# cd /usr/local/src/ # wget http://nginx.org/download/nginx-1.4.2.tar.gz # tar -xzvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 //先导入环境变量,告诉nginx去哪里找luajit # export LUAJIT_LIB=/usr/local/lib # export LUAJIT_INC=/usr/local/include/luajit-2.0 # ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../lua-nginx-module-0.8.6 # make -j2 # make install
#4.測試安裝是否成功
#
# cd /usr/local/nginx-1.4.2/conf/ # vi nginx.conflua指令方式在server 中新增一個localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
##然後啟動nginx
# cd /usr/local/nginx-1.4.2/sbin # ./nginx
瀏覽器存取:
http://127.0.0.1/hello
顯示:hello, lua
注意點:
1.注意每個模組的安裝目錄不能出現錯誤
2.如果之前透過apt- get方式安裝了nginx,需要先刪除nginx,因為此方式下載的軟體無法編譯
3.
到此說明第三方安裝成功,
5.下面開始新增日誌到此便成功新增response日誌。 如果懂Lua語言的,可以對以下程式碼進行修改,來更符合自己的要求。http { log_format mylog 'response_body:$resp_body'; server { #记录nginx请求返回值 lua_need_request_body on; set $resp_body ""; body_filter_by_lua ' local resp_body = string.sub(ngx.arg[1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end '; location / { proxy_pass http://127.0.0.1:5000; access_log /var/log/nginx/access.log mylog; } } }
body_filter_by_lua ' local resp_body = string.sub(ngx.arg[1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end ';日誌輸出時,會發現語言回應結果涉及到字元漢字的時候轉為16進位導致無法辨識。
部分回應日誌如下:
response_body: {\x22code\x22: 404, \x22message\x22: \x22\xE8\xAF\xB7\xE6\xB1x22: \x22\xE8\xAF\xB7\xE6\xB1x822 xE7\x9A\x84\xE8\xB5\x84\xE6\xBA\x90\xE4\xB8\x8D\xE5\xAD\x98\xE5\x9C\xA8\x22, \x22data\x22: {}}
###可透過複製到python腳本來解決。 #########str1=''' {\x22code\x22: 404, \x22message\x22: \x22\xE8\xAF\xB7\xE6\xB1\x82\xE7\x9A\x84\xE8\xB5\x84\xE6\xBA\x90\xE4\xB8\x8D\xE5\xAD\x98\xE5\x9C\xA8\x22, \x22data\x22: {}} ''' print(str1.encode('raw_unicode_escape').decode('utf-8'))#########輸入結果為:#########{"code": 404, "message": "請求的資源不存在" , "data": {}}###################
以上是nginx日誌中新增請求的response日誌的詳細內容。更多資訊請關注PHP中文網其他相關文章!

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

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

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。