{"id":5425,"date":"2023-04-14T19:30:41","date_gmt":"2023-04-14T11:30:41","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5425"},"modified":"2023-04-14T19:30:41","modified_gmt":"2023-04-14T11:30:41","slug":"nginx%e6%97%a5%e5%bf%97%e7%9a%84%e4%b8%80%e4%ba%9b%e7%ae%80%e5%8d%95%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5425.html","title":{"rendered":"Nginx\u65e5\u5fd7\u7684\u4e00\u4e9b\u7b80\u5355\u8bb0\u5f55"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u5185\u90e8\u6570\u636e\u5b89\u5168\u5206\u6790\u4f9d\u8d56\u7684\u6570\u636e\u6e90\u4e2dNginx\u7684\u65e5\u5fd7\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5e38\u89c4\u7684\u5b57\u6bb5\u6709\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unix_timestamp\nuser_name\nremote_addr\nrequest_method\nscheme\ndomain\nuri\nargs\nhttp_user_agent\nhttp_referer\nstatus\nhttp_x_forwarded_for\nhttp_content_type<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u5404\u79cd\u539f\u56e0\uff08\u5b89\u5168\u9690\u79c1\u3001\u5b58\u50a8\u6210\u672c\u3001\u6027\u80fd\u635f\u8017\u3001\u2026\u2026\uff09\uff0cNginx\u65e5\u5fd7\u4e2d\u8bf7\u6c42body\u548c\u54cd\u5e94body\u4e00\u822c\u6ca1\u6709\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request_body\nresponse_body<\/code><\/pre>\n\n\n\n<p>\u5728\u524d\u671f\u6ca1\u6709\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u80fd\u5148\u7528\u4ee5\u4e0b\u5b57\u6bb5\u505a\u66ff\u4ee3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request_length\nupstream_response_length\nbytes_sent<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0bNginx\u65e5\u5fd7\u4e2d\u76f8\u5173\u5b57\u6bb5\u7684\u542b\u4e49\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b98\u65b9\u8bf4\u660e\n\n$request\nfull original request line\n\u5b8c\u6574\u7684\u539f\u59cb\u8bf7\u6c42\u884c\n\n$request_body\nrequest body\n\u8bf7\u6c42\u4f53\n\n$request_length\nrequest length (including request line, header, and request body)\n\u8bf7\u6c42\u7684\u5b57\u8282\u6570\uff08\u5305\u62ec\u8bf7\u6c42\u884c\u3001\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u4f53\uff09\u3002\n\n$bytes_sent\nnumber of bytes sent to a client (1.3.8, 1.2.5)\nnginx\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u5b57\u8282\u6570\uff0c\u5305\u62ec\u54cd\u5e94\u5934\u548c\u54cd\u5e94\u4f53\u3002\n\n$body_bytes_sent\nnumber of bytes sent to a client, not counting the response header; this variable is compatible with the \u201c%B\u201d parameter of the mod_log_config Apache module\nnginx\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u54cd\u5e94\u4f53\u7684\u5b57\u8282\u6570\uff08\u4e0d\u542b\u54cd\u5e94\u5934\uff09\uff0c\u8fd9\u4e2a\u53d8\u91cf\u548capache\u6a21\u5757\u4e2d\u7684 %B \u662f\u517c\u5bb9\u7684\u3002\n\n$upstream_response_length\nkeeps the length of the response obtained from the upstream server (0.7.27); the length is kept in bytes. Lengths of several responses are separated by commas and colons like addresses in the $upstream_addr variable.\n\u4fdd\u5b58\u7684\u662f\u4ece\u4e0a\u6e38upstream\u670d\u52a1\u5668\u83b7\u5f97\u7684\u54cd\u5e94\u7684\u957f\u5ea6(0.7.27)\uff0c\u957f\u5ea6\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u3002\u591a\u4e2a\u54cd\u5e94\u7684\u957f\u5ea6\u7531\u9017\u53f7\u548c\u5192\u53f7\u5206\u9694\uff0c\u5c31\u50cf$upstream_addr\u53d8\u91cf\u4e2d\u7684\u5730\u5740\u4e00\u6837\u3002<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>$bytes_sent\uff081.3.8, 1.2.5\uff09\uff1anginx\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u5b57\u8282\u6570\uff0c\u5305\u62ec\u54cd\u5e94\u5934\u548c\u54cd\u5e94\u4f53\u3002<\/p>\n\n\n\n<p>$body_bytes_sent\uff1anginx\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u54cd\u5e94\u4f53\u7684\u5b57\u8282\u6570\uff08\u5373\u4e0d\u542b\u54cd\u5e94\u5934\uff09\u3002<\/p>\n\n\n\n<p>$content_length\uff1a\u201cContent-Length\u201d\u8bf7\u6c42\u5934\u7684\u503c\uff0c\u7b49\u540c\u4e8e$http_content_length\u3002\u5373\uff0cnginx\u4ece\u5ba2\u6237\u7aef\u6536\u5230\u7684\u8bf7\u6c42\u5934\u4e2dContent-Length\u5b57\u6bb5\u7684\u503c\uff0c\u4e0d\u662fnginx\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u54cd\u5e94\u4e2d\u7684Content-Length\u5b57\u6bb5\uff08$sent_http_content_length\uff09\u7684\u503c\u3002<\/p>\n\n\n\n<p>$request_length\uff081.3.12, 1.2.7\uff09\uff1a\u8bf7\u6c42\u7684\u5b57\u8282\u6570\uff08\u5305\u62ec\u8bf7\u6c42\u884c\u3001\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u4f53\uff09\u3002\u6ce8\u610f\uff0c\u7531\u4e8e$request_length\u662f\u8bf7\u6c42\u89e3\u6790\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u7d2f\u52a0\u7684\uff0c\u5982\u679c\u89e3\u6790\u8bf7\u6c42\u65f6\u51fa\u73b0\u5f02\u5e38\u6216\u63d0\u524d\u5b8c\u6210\uff0c\u5219$request_length\u53ea\u662f\u5df2\u7ecf\u7d2f\u52a0\u90e8\u5206\u7684\u957f\u5ea6\uff0c\u5e76\u4e0d\u662fnginx\u4ece\u5ba2\u6237\u7aef\u6536\u5230\u7684\u5b8c\u6574\u8bf7\u6c42\u7684\u603b\u5b57\u8282\u6570\uff08\u5305\u62ec\u8bf7\u6c42\u884c\u3001\u8bf7\u6c42\u5934\u3001\u8bf7\u6c42\u4f53\uff09\u3002\u4f8b\u5982\uff0c\u5411nginx\u7684\u9759\u6001\u6587\u4ef6\u7684URL POST\u6570\u636e\uff0c\u5219POST\u7684\u6570\u636e\uff08\u5373\u8bf7\u6c42\u4f53\uff09\u4e0d\u4f1a\u8ba1\u7b97\u5728\u5185\u3002<\/p>\n\n\n\n<p>$upstream_response_length\uff080.7.27\uff09\uff1a\u4fdd\u5b58\u4eceupstream\u670d\u52a1\u5668\u83b7\u5f97\u7684\u54cd\u5e94\u4f53\u7684\u5b57\u8282\u6570\uff0c\u4e0d\u5305\u62ec\u54cd\u5e94\u5934\uff0c1.11.4\u4e2d\u589e\u52a0\u7684$upstream_bytes_received\u53d8\u91cf\u662f\u5305\u62ec\u54cd\u5e94\u5934\u7684\u3002\u5982\u679c\u6709\u591a\u4e2a\u54cd\u5e94\u957f\u5ea6\u7684\u8bdd\u4f7f\u7528\u9017\u53f7\u548c\u5192\u53f7\u5206\u9694\uff0c\u5c31\u50cf$upstream_addr\u4e2d\u7684\u5730\u5740\u90a3\u6837\u3002<\/p>\n<\/blockquote>\n\n\n\n<h5 class=\"wp-block-heading\">\u5982\u679c\u60f3\u8bb0\u5f55\u8bf7\u6c42\u5934\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>lua_need_request_body on;\nlog_format log_body '&#91;$time_local] \"$request\" \"$request_body\"';\n\nserver {\n  location \/ {\n    content_by_lua 'ngx.say(\"ok\")';\n    access_log \/var\/log\/nginx\/lua.log log_body;\n  }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5982\u679c\u60f3\u8bb0\u5f55\u54cd\u5e94\u4f53\u5927\u5c0f\uff0c\u9700\u8981\u8bb0\u5f55\u7684\u5b57\u6bb5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$bytes_sent\n$upstream_response_length\n\n# \u5982\u679cNginx\u662f\u4f5c\u4e3a\u4ee3\u7406\u670d\u52a1\u5668\u4f7f\u7528\u7684\uff0c\u4e00\u822c bytes_sent \u7684\u503c\u4f1a\u6bd4 upstream_response_length \u8981\u5927\uff0c\u56e0\u4e3a\u5728\u4ee3\u7406\u5c42\u53ef\u80fd\u4f1a\u6ce8\u5165\u4e00\u4e9b\u5185\u5bb9<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>log_format combined '$remote_addr - $remote_user &#91;$time_local] '\n                    '\"$request\" $status $body_bytes_sent '\n                    '\"$http_referer\" \"$http_user_agent\"';\n\n\n# \u4f7f\u7528 body_filter_by_lua \u8bb0\u5f55\u54cd\u5e94\u5185\u5bb9\u7684\u914d\u7f6e\u6837\u4f8b\nworker_processes  1;\nerror_log logs\/error.log;\nevents {\n    worker_connections 1024;\n}\nhttp {\n    log_format log_req_resp '$remote_addr - $remote_user &#91;$time_local] '\n        '\"$request\" $status $body_bytes_sent '\n        '\"$http_referer\" \"$http_user_agent\" $request_time req_body:\"$request_body\" resp_body:\"$resp_body\"';\n\n    server {\n        listen 8082;\n        access_log logs\/access.log log_req_resp;\n\n        lua_need_request_body on;\n\n        set $resp_body \"\";\n        body_filter_by_lua '\n            local resp_body = string.sub(ngx.arg&#91;1], 1, 1000)\n            ngx.ctx.buffered = (ngx.ctx.buffered or \"\") .. resp_body\n            if ngx.arg&#91;2] then\n                ngx.var.resp_body = ngx.ctx.buffered\n            end\n        ';\n\n        location \/ {\n            echo \"Hello World!\";\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1. \u83b7\u53d6\u5230\u6240\u6709\u8bf7\u6c42\u7684\u54cd\u5e94\u6570\u636e\n2. \u5bf9\u6240\u6709\u8bf7\u6c42\u7684\u54cd\u5e94\u6570\u636e\u8fdb\u884c\u654f\u611f\u4fe1\u606f\u68c0\u6d4b\n3. \u68c0\u6d4b\u5230\u654f\u611f\u6570\u636e\u54cd\u5e94\uff0c\u8fdb\u884c\u8131\u654f\u5904\u7406\u540e\u518d\u8fdb\u884c\u54cd\u5e94\n\n-- access_by_lua_file\uff1a\nngx.say(\"123\")\nngx.say(\"456\")\n\n-- body_filter_by_lua_file:\nlocal chunk, eof = ngx.arg&#91;1], ngx.arg&#91;2]\nprint(chunk, eof)\n\n-- \u7ed3\u679c\n-- body_filter_by_lua*\u9996\u6b21\u8c03\u7528\u65f6\uff1a123 false\n-- body_filter_by_lua*\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\uff1a456 false\n-- body_filter_by_lua*\u7b2c\u4e09\u6b21\u8c03\u7528\u65f6: \u7a7a true<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>$bytes_sent\u3001$body_bytes_sent\u3001$content_length\u3001$request_length\u3001$upstream_response_length\u8be6\u89e3<br><a href=\"https:\/\/www.jianshu.com\/p\/11ec806f3c42\">https:\/\/www.jianshu.com\/p\/11ec806f3c42<\/a><\/p>\n\n\n\n<p>Module ngx_http_log_module<br><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_log_module.html\">https:\/\/nginx.org\/en\/docs\/http\/ngx_http_log_module.html<\/a><\/p>\n\n\n\n<p>Module ngx_http_core_module &#8211; Embedded Variables<br><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_core_module.html#var_bytes_sent\">https:\/\/nginx.org\/en\/docs\/http\/ngx_http_core_module.html#var_bytes_sent<\/a><\/p>\n\n\n\n<p>Module ngx_http_upstream_module &#8211; Embedded Variables<br><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_upstream_module.html\">https:\/\/nginx.org\/en\/docs\/http\/ngx_http_upstream_module.html<\/a><\/p>\n\n\n\n<p>Logging Request\/Response size in access_log of Nginx<br><a href=\"https:\/\/serverfault.com\/questions\/346853\/logging-request-response-size-in-access-log-of-nginx\">https:\/\/serverfault.com\/questions\/346853\/logging-request-response-size-in-access-log-of-nginx<\/a><\/p>\n\n\n\n<p>\u6d45\u8c08OpenResty\u4e2d\u7684body_filter_by_lua*<br><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/67904411\">https:\/\/zhuanlan.zhihu.com\/p\/67904411<\/a><\/p>\n\n\n\n<p>OpenResty &#8211; \u6267\u884c\u9636\u6bb5\u6982\u5ff5<br><a href=\"https:\/\/moonbingbing.gitbooks.io\/openresty-best-practices\/content\/ngx_lua\/phase.html\">https:\/\/moonbingbing.gitbooks.io\/openresty-best-practices\/content\/ngx_lua\/phase.html<\/a><\/p>\n\n\n\n<p>body_filter_by_lua<br><a href=\"https:\/\/github.com\/openresty\/lua-nginx-module#body_filter_by_lua\">https:\/\/github.com\/openresty\/lua-nginx-module#body_filter_by_lua<\/a><\/p>\n\n\n\n<p>body_filter_by_lua_file<br><a href=\"https:\/\/github.com\/openresty\/lua-nginx-module#body_filter_by_lua_file\">https:\/\/github.com\/openresty\/lua-nginx-module#body_filter_by_lua_file<\/a><\/p>\n\n\n\n<p>Is it possible to log the response data in nginx access log?<br><a href=\"https:\/\/serverfault.com\/questions\/361556\/is-it-possible-to-log-the-response-data-in-nginx-access-log\">https:\/\/serverfault.com\/questions\/361556\/is-it-possible-to-log-the-response-data-in-nginx-access-log<\/a><\/p>\n\n\n\n<p>Full request\/response body logging in nginx<br><a href=\"https:\/\/gist.github.com\/morhekil\/1ff0e902ed4de2adcb7a\">https:\/\/gist.github.com\/morhekil\/1ff0e902ed4de2adcb7a<\/a><\/p>\n\n\n\n<p>NGINX Plus &#8211; Admin Guide &#8211; Monitoring &#8211; Configuring Logging<br><a href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/monitoring\/logging\/\">https:\/\/docs.nginx.com\/nginx\/admin-guide\/monitoring\/logging\/<\/a><\/p>\n\n\n\n<p>\u542c\u8bf4\u4f60\u7684Nginx\u8fd8\u4e0d\u4f1a\u8bb0\u5f55Response Body?<br><a href=\"https:\/\/juejin.cn\/post\/6844904147570032648\">https:\/\/juejin.cn\/post\/6844904147570032648<\/a><\/p>\n\n\n\n<p>How to change Content-length in body_filter_by_lua* in openresty<br><a href=\"https:\/\/stackoverflow.com\/questions\/45356766\/how-to-change-content-length-in-body-filter-by-lua-in-openresty\">https:\/\/stackoverflow.com\/questions\/45356766\/how-to-change-content-length-in-body-filter-by-lua-in-openresty<\/a><\/p>\n\n\n\n<p>How to log request body<br><a href=\"https:\/\/onelinerhub.com\/nginx-lua\/how-to-log-request-body\">https:\/\/onelinerhub.com\/nginx-lua\/how-to-log-request-body<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5185\u90e8\u6570\u636e\u5b89\u5168\u5206\u6790\u4f9d\u8d56\u7684\u6570\u636e\u6e90\u4e2dNginx\u7684\u65e5\u5fd7\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5e38\u89c4\u7684\u5b57\u6bb5\u6709\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,25],"tags":[1935,1933,1934,75,1932,1936],"class_list":["post-5425","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-security","tag-body_bytes_sent","tag-body_filter_by_lua","tag-bytes_sent","tag-nginx","tag-openresty","tag-upstream_response_length"],"views":1740,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5425"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5425\/revisions"}],"predecessor-version":[{"id":5426,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5425\/revisions\/5426"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}