{"id":185837,"date":"2022-01-30T06:03:06","date_gmt":"2022-01-29T22:03:06","guid":{"rendered":"http:\/\/www.idc.net\/help\/185837\/"},"modified":"2022-01-30T06:03:06","modified_gmt":"2022-01-29T22:03:06","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91nginx%e6%95%99%e7%a8%8b%ef%bc%9anginx-%e8%bf%87%e6%bb%a4%e6%a8%a1%e5%9d%97%e7%9a%84%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/185837\/","title":{"rendered":"\u540e\u6d6a\u4e91nginx\u6559\u7a0b\uff1aNginx \u8fc7\u6ee4\u6a21\u5757\u7684\u5206\u6790"},"content":{"rendered":"<h2>\u8fc7\u6ee4\u6a21\u5757\u7684\u5206\u6790<\/h2>\n<h2>\u76f8\u5173\u7ed3\u6784\u4f53<\/h2>\n<p>ngx_chain_t \u7ed3\u6784\u975e\u5e38\u7b80\u5355\uff0c\u662f\u4e00\u4e2a\u5355\u5411\u94fe\u8868\uff1a<\/p>\n<pre><code>        typedef struct ngx_chain_s ngx_chain_t;\n\n        struct ngx_chain_s {\n            ngx_buf_t    *buf;\n            ngx_chain_t  *next;\n        };<\/code><\/pre>\n<p>\u5728\u8fc7\u6ee4\u6a21\u5757\u4e2d\uff0c\u6240\u6709\u8f93\u51fa\u7684\u5185\u5bb9\u90fd\u662f\u901a\u8fc7\u4e00\u6761\u5355\u5411\u94fe\u8868\u6240\u7ec4\u6210\u3002\u8fd9\u79cd\u5355\u5411\u94fe\u8868\u7684\u8bbe\u8ba1\uff0c\u6b63\u597d\u5e94\u548c\u4e86 Nginx \u6d41\u5f0f\u7684\u8f93\u51fa\u6a21\u5f0f\u3002\u6bcf\u6b21 Nginx \u90fd\u662f\u8bfb\u5230\u4e00\u90e8\u5206\u7684\u5185\u5bb9\uff0c\u5c31\u653e\u5230\u94fe\u8868\uff0c\u7136\u540e\u8f93\u51fa\u51fa\u53bb\u3002\u8fd9\u79cd\u8bbe\u8ba1\u7684\u597d\u5904\u662f\u7b80\u5355\uff0c\u975e\u963b\u585e\uff0c\u4f46\u662f\u76f8\u5e94\u7684\u95ee\u9898\u5c31\u662f\u8de8\u94fe\u8868\u7684\u5185\u5bb9\u64cd\u4f5c\u975e\u5e38\u9ebb\u70e6\uff0c\u5982\u679c\u9700\u8981\u8de8\u94fe\u8868\uff0c\u5f88\u591a\u65f6\u5019\u90fd\u53ea\u80fd\u7f13\u5b58\u94fe\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5355\u94fe\u8868\u8d1f\u8f7d\u7684\u5c31\u662f ngx_buf_t\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4f7f\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5148\u8ba9\u6211\u4eec\u770b\u4e0b\u8be5\u7ed3\u6784\u4f53\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>        struct ngx_buf_s {\n            u_char          *pos;       \/* \u5f53\u524dbuffer\u771f\u5b9e\u5185\u5bb9\u7684\u8d77\u59cb\u4f4d\u7f6e *\/\n            u_char          *last;      \/* \u5f53\u524dbuffer\u771f\u5b9e\u5185\u5bb9\u7684\u7ed3\u675f\u4f4d\u7f6e *\/\n            off_t            file_pos;  \/* \u5728\u6587\u4ef6\u4e2d\u771f\u5b9e\u5185\u5bb9\u7684\u8d77\u59cb\u4f4d\u7f6e   *\/\n            off_t            file_last; \/* \u5728\u6587\u4ef6\u4e2d\u771f\u5b9e\u5185\u5bb9\u7684\u7ed3\u675f\u4f4d\u7f6e   *\/\n\n            u_char          *start;    \/* buffer\u5185\u5b58\u7684\u5f00\u59cb\u5206\u914d\u7684\u4f4d\u7f6e *\/\n            u_char          *end;      \/* buffer\u5185\u5b58\u7684\u7ed3\u675f\u5206\u914d\u7684\u4f4d\u7f6e *\/\n            ngx_buf_tag_t    tag;      \/* buffer\u5c5e\u4e8e\u54ea\u4e2a\u6a21\u5757\u7684\u6807\u5fd7 *\/\n            ngx_file_t      *file;     \/* buffer\u6240\u5f15\u7528\u7684\u6587\u4ef6 *\/\n\n            \/* \u7528\u6765\u5f15\u7528\u66ff\u6362\u8fc7\u540e\u7684buffer\uff0c\u4ee5\u4fbf\u5f53\u6240\u6709buffer\u8f93\u51fa\u4ee5\u540e\uff0c\n             * \u8fd9\u4e2a\u5f71\u5b50buffer\u53ef\u4ee5\u88ab\u91ca\u653e\u3002\n             *\/\n            ngx_buf_t       *shadow; \n\n            \/* the buf's content could be changed *\/\n            unsigned         temporary:1;\n\n            \/*\n             * the buf's content is in a memory cache or in a read only memory\n             * and must not be changed\n             *\/\n            unsigned         memory:1;\n\n            \/* the buf's content is mmap()ed and must not be changed *\/\n            unsigned         mmap:1;\n\n            unsigned         recycled:1; \/* \u5185\u5b58\u53ef\u4ee5\u88ab\u8f93\u51fa\u5e76\u56de\u6536 *\/\n            unsigned         in_file:1;  \/* buffer\u7684\u5185\u5bb9\u5728\u6587\u4ef6\u4e2d *\/\n            \/* \u9a6c\u4e0a\u5168\u90e8\u8f93\u51fabuffer\u7684\u5185\u5bb9, gzip\u6a21\u5757\u91cc\u9762\u7528\u5f97\u6bd4\u8f83\u591a *\/\n            unsigned         flush:1;\n            \/* \u57fa\u672c\u4e0a\u662f\u4e00\u6bb5\u8f93\u51fa\u94fe\u7684\u6700\u540e\u4e00\u4e2abuffer\u5e26\u7684\u6807\u5fd7\uff0c\u6807\u793a\u53ef\u4ee5\u8f93\u51fa\uff0c\n             * \u6709\u4e9b\u96f6\u957f\u5ea6\u7684buffer\u4e5f\u53ef\u4ee5\u7f6e\u8be5\u6807\u5fd7\n             *\/\n            unsigned         sync:1;\n            \/* \u6240\u6709\u8bf7\u6c42\u91cc\u9762\u6700\u540e\u4e00\u5757buffer\uff0c\u5305\u542b\u5b50\u8bf7\u6c42 *\/\n            unsigned         last_buf:1;\n            \/* \u5f53\u524d\u8bf7\u6c42\u8f93\u51fa\u94fe\u7684\u6700\u540e\u4e00\u5757buffer         *\/\n            unsigned         last_in_chain:1;\n            \/* shadow\u94fe\u91cc\u9762\u7684\u6700\u540ebuffer\uff0c\u53ef\u4ee5\u91ca\u653ebuffer\u4e86 *\/\n            unsigned         last_shadow:1;\n            \/* \u662f\u5426\u662f\u6682\u5b58\u6587\u4ef6 *\/\n            unsigned         temp_file:1;\n\n            \/* \u7edf\u8ba1\u7528\uff0c\u8868\u793a\u4f7f\u7528\u6b21\u6570 *\/\n            \/* STUB *\/ int   num;\n        };<\/code><\/pre>\n<p>\u4e00\u822c buffer \u7ed3\u6784\u4f53\u53ef\u4ee5\u8868\u793a\u4e00\u5757\u5185\u5b58\uff0c\u5185\u5b58\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\u5206\u522b\u7528 start \u548c end \u8868\u793a\uff0cpos \u548c last \u8868\u793a\u5b9e\u9645\u7684\u5185\u5bb9\u3002\u5982\u679c\u5185\u5bb9\u5df2\u7ecf\u5904\u7406\u8fc7\u4e86\uff0cpos \u7684\u4f4d\u7f6e\u5c31\u53ef\u4ee5\u5f80\u540e\u79fb\u52a8\u3002\u5982\u679c\u8bfb\u53d6\u5230\u65b0\u7684\u5185\u5bb9\uff0clast \u7684\u4f4d\u7f6e\u5c31\u4f1a\u5f80\u540e\u79fb\u52a8\u3002\u6240\u4ee5 buffer \u53ef\u4ee5\u5728\u591a\u6b21\u8c03\u7528\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u3002\u5982\u679c last \u7b49\u4e8e end\uff0c\u5c31\u8bf4\u660e\u8fd9\u5757\u5185\u5b58\u5df2\u7ecf\u7528\u5b8c\u4e86\u3002\u5982\u679c pos \u7b49\u4e8e last\uff0c\u8bf4\u660e\u5185\u5b58\u5df2\u7ecf\u5904\u7406\u5b8c\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u610f\u56fe\uff0c\u8bf4\u660e buffer \u4e2d\u6307\u9488\u7684\u7528\u6cd5\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/atts.w3cschool.cn\/attachments\/image\/wk\/nginx\/chapter-4-2.png\" class=\"aligncenter\"><\/p>\n<h2>\u54cd\u5e94\u5934\u8fc7\u6ee4\u51fd\u6570<\/h2>\n<p>\u54cd\u5e94\u5934\u8fc7\u6ee4\u51fd\u6570\u4e3b\u8981\u7684\u7528\u5904\u5c31\u662f\u5904\u7406 HTTP \u54cd\u5e94\u7684\u5934\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5bf9\u4e8e\u54cd\u5e94\u5934\u8fdb\u884c\u4fee\u6539\u6216\u8005\u6dfb\u52a0\u5220\u9664\u3002\u54cd\u5e94\u5934\u8fc7\u6ee4\u51fd\u6570\u5148\u4e8e\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570\uff0c\u800c\u4e14\u53ea\u8c03\u7528\u4e00\u6b21\uff0c\u6240\u4ee5\u4e00\u822c\u53ef\u4f5c\u8fc7\u6ee4\u6a21\u5757\u7684\u521d\u59cb\u5316\u5de5\u4f5c\u3002<\/p>\n<p>\u54cd\u5e94\u5934\u8fc7\u6ee4\u51fd\u6570\u7684\u5165\u53e3\u53ea\u6709\u4e00\u4e2a\uff1a<\/p>\n<pre><code>        ngx_int_t\n        ngx_http_send_header(ngx_http_request_t *r)\n        {\n            ...\n\n            return ngx_http_top_header_filter(r);\n        }<\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u56de\u590d\u7684\u65f6\u5019\u8c03\u7528\uff0c\u7136\u540e\u6309\u524d\u4e00\u8282\u6240\u8ff0\u7684\u6267\u884c\u987a\u5e8f\u3002\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e00\u822c\u662f NGX_OK\uff0cNGX_ERROR \u548c NGX_AGAIN\uff0c\u5206\u522b\u8868\u793a\u5904\u7406\u6210\u529f\uff0c\u5931\u8d25\u548c\u672a\u5b8c\u6210\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u628a HTTP \u54cd\u5e94\u5934\u7684\u5b58\u50a8\u65b9\u5f0f\u60f3\u8c61\u6210\u4e00\u4e2a hash \u8868\uff0c\u5728 Nginx \u5185\u90e8\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u67e5\u627e\u548c\u4fee\u6539\u5404\u4e2a\u54cd\u5e94\u5934\u90e8\uff0cngx_http_header_filter_module \u8fc7\u6ee4\u6a21\u5757\u628a\u6240\u6709\u7684 HTTP \u5934\u7ec4\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684 buffer\uff0c\u6700\u7ec8 ngx_http_write_filter_module \u8fc7\u6ee4\u6a21\u5757\u628a buffer \u8f93\u51fa\u3002<\/p>\n<p>\u6309\u7167\u524d\u4e00\u8282\u8fc7\u6ee4\u6a21\u5757\u7684\u987a\u5e8f\uff0c\u4f9d\u6b21\u8bb2\u89e3\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left\">filter module<\/th>\n<th style=\"text-align: left\">description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left\">ngx_http_not_modified_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u6253\u5f00\uff0c\u5982\u679c\u8bf7\u6c42\u7684 if-modified-since \u7b49\u4e8e\u56de\u590d\u7684 last-modified \u95f4\u503c\uff0c\u8bf4\u660e\u56de\u590d\u6ca1\u6709\u53d8\u5316\uff0c\u6e05\u7a7a\u6240\u6709\u56de\u590d\u7684\u5185\u5bb9\uff0c\u8fd4\u56de 304\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_range_body_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u6253\u5f00\uff0c\u53ea\u662f\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570\uff0c\u652f\u6301 range \u529f\u80fd\uff0c\u5982\u679c\u8bf7\u6c42\u5305\u542brange\u8bf7\u6c42\uff0c\u90a3\u5c31\u53ea\u53d1\u9001range\u8bf7\u6c42\u7684\u4e00\u6bb5\u5185\u5bb9\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_copy_filter_module<\/td>\n<td style=\"text-align: left\">\u59cb\u7ec8\u6253\u5f00\uff0c\u53ea\u662f\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570\uff0c \u4e3b\u8981\u5de5\u4f5c\u662f\u628a\u6587\u4ef6\u4e2d\u5185\u5bb9\u8bfb\u5230\u5185\u5b58\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u5904\u7406\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_headers_filter_module<\/td>\n<td style=\"text-align: left\">\u59cb\u7ec8\u6253\u5f00\uff0c\u53ef\u4ee5\u8bbe\u7f6e expire \u548c Cache-control \u5934\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4efb\u610f\u540d\u79f0\u7684\u5934<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_userid_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u5173\u95ed\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7edf\u8ba1\u7528\u7684\u8bc6\u522b\u7528\u6237\u7684 cookie\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_charset_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u5173\u95ed\uff0c\u53ef\u4ee5\u6dfb\u52a0 charset\uff0c\u4e5f\u53ef\u4ee5\u5c06\u5185\u5bb9\u4ece\u4e00\u79cd\u5b57\u7b26\u96c6\u8f6c\u6362\u5230\u53e6\u5916\u4e00\u79cd\u5b57\u7b26\u96c6\uff0c\u4e0d\u652f\u6301\u591a\u5b57\u8282\u5b57\u7b26\u96c6\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_ssi_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u5173\u95ed\uff0c\u8fc7\u6ee4 SSI \u8bf7\u6c42\uff0c\u53ef\u4ee5\u53d1\u8d77\u5b50\u8bf7\u6c42\uff0c\u53bb\u83b7\u53d6include\u8fdb\u6765\u7684\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_postpone_filter_module<\/td>\n<td style=\"text-align: left\">\u59cb\u7ec8\u6253\u5f00\uff0c\u7528\u6765\u5c06\u5b50\u8bf7\u6c42\u548c\u4e3b\u8bf7\u6c42\u7684\u8f93\u51fa\u94fe\u5408\u5e76<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_gzip_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u5173\u95ed\uff0c\u652f\u6301\u6d41\u5f0f\u7684\u538b\u7f29\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_range_header_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u6253\u5f00\uff0c\u53ea\u662f\u54cd\u5e94\u5934\u8fc7\u6ee4\u51fd\u6570\uff0c\u7528\u6765\u89e3\u6790range\u5934\uff0c\u5e76\u4ea7\u751frange\u54cd\u5e94\u7684\u5934\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_chunked_filter_module<\/td>\n<td style=\"text-align: left\">\u9ed8\u8ba4\u6253\u5f00\uff0c\u5bf9\u4e8e HTTP\/1.1 \u548c\u7f3a\u5c11 content-length \u7684\u56de\u590d\u81ea\u52a8\u6253\u5f00\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_header_filter_module<\/td>\n<td style=\"text-align: left\">\u59cb\u7ec8\u6253\u5f00\uff0c\u7528\u6765\u5c06\u6240\u6709 header \u7ec4\u6210\u4e00\u4e2a\u5b8c\u6574\u7684 HTTP \u5934\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">ngx_http_write_filter_module<\/td>\n<td style=\"text-align: left\">\u59cb\u7ec8\u6253\u5f00\uff0c\u5c06\u8f93\u51fa\u94fe\u62f7\u8d1d\u5230 r-&gt;out\u4e2d\uff0c\u7136\u540e\u8f93\u51fa\u5185\u5bb9\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570<\/h2>\n<p>\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570\u662f\u8fc7\u6ee4\u54cd\u5e94\u4e3b\u4f53\u7684\u51fd\u6570\u3002ngx_http_top_body_filter \u8fd9\u4e2a\u51fd\u6570\u6bcf\u4e2a\u8bf7\u6c42\u53ef\u80fd\u4f1a\u88ab\u6267\u884c\u591a\u6b21\uff0c\u5b83\u7684\u5165\u53e3\u51fd\u6570\u662f ngx_http_output_filter\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code>        ngx_int_t\n        ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)\n        {\n            ngx_int_t          rc;\n            ngx_connection_t  *c;\n\n            c = r-&gt;connection;\n\n            rc = ngx_http_top_body_filter(r, in);\n\n            if (rc == NGX_ERROR) {\n                \/* NGX_ERROR may be returned by any filter *\/\n                c-&gt;error = 1;\n            }\n\n            return rc;\n        }<\/code><\/pre>\n<p>ngx_http_output_filter \u53ef\u4ee5\u88ab\u4e00\u822c\u7684\u9759\u6001\u5904\u7406\u6a21\u5757\u8c03\u7528\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5728 upstream \u6a21\u5757\u91cc\u9762\u88ab\u8c03\u7528\uff0c\u5bf9\u4e8e\u6574\u4e2a\u8bf7\u6c42\u7684\u5904\u7406\u9636\u6bb5\u6765\u8bf4\uff0c\u4ed6\u4eec\u5904\u4e8e\u7684\u7528\u5904\u90fd\u662f\u4e00\u6837\u7684\uff0c\u5c31\u662f\u628a\u54cd\u5e94\u5185\u5bb9\u8fc7\u6ee4\uff0c\u7136\u540e\u53d1\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u5177\u4f53\u6a21\u5757\u7684\u54cd\u5e94\u4f53\u8fc7\u6ee4\u51fd\u6570\u7684\u683c\u5f0f\u7c7b\u4f3c\u8fd9\u6837\uff1a<\/p>\n<pre><code>        static int \n        ngx_http_example_body_filter(ngx_http_request_t *r, ngx_chain_t *in)\n        {\n            ...\n\n            return ngx_http_next_body_filter(r, in);\n        }<\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e00\u822c\u662f NGX_OK\uff0cNGX_ERROR \u548c NGX_AGAIN\uff0c\u5206\u522b\u8868\u793a\u5904\u7406\u6210\u529f\uff0c\u5931\u8d25\u548c\u672a\u5b8c\u6210\u3002<\/p>\n<h2>\u4e3b\u8981\u529f\u80fd\u4ecb\u7ecd<\/h2>\n<p>\u54cd\u5e94\u7684\u4e3b\u4f53\u5185\u5bb9\u5c31\u5b58\u4e8e\u5355\u94fe\u8868 in\uff0c\u94fe\u8868\u4e00\u822c\u4e0d\u4f1a\u592a\u957f\uff0c\u6709\u65f6 in \u53c2\u6570\u53ef\u80fd\u4e3a NULL\u3002in\u4e2d\u5b58\u6709buf\u7ed3\u6784\u4f53\u4e2d\uff0c\u5bf9\u4e8e\u9759\u6001\u6587\u4ef6\uff0c\u8fd9\u4e2abuf\u5927\u5c0f\u9ed8\u8ba4\u662f 32K\uff1b\u5bf9\u4e8e\u53cd\u5411\u4ee3\u7406\u7684\u5e94\u7528\uff0c\u8fd9\u4e2abuf\u53ef\u80fd\u662f4k\u6216\u80058k\u3002\u4e3a\u4e86\u4fdd\u6301\u5185\u5b58\u7684\u4f4e\u6d88\u8017\uff0cNginx\u4e00\u822c\u4e0d\u4f1a\u5206\u914d\u8fc7\u5927\u7684\u5185\u5b58\uff0c\u5904\u7406\u7684\u539f\u5219\u662f\u6536\u5230\u4e00\u5b9a\u7684\u6570\u636e\uff0c\u5c31\u53d1\u9001\u51fa\u53bb\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u53ef\u4ee5\u770b\u770bNginx\u7684chunked_filter\u6a21\u5757\uff0c\u5728\u6ca1\u6709 content-length \u7684\u60c5\u51b5\u4e0b\uff0cchunk \u6a21\u5757\u53ef\u4ee5\u6d41\u5f0f\uff08stream\uff09\u7684\u52a0\u4e0a\u957f\u5ea6\uff0c\u65b9\u4fbf\u6d4f\u89c8\u5668\u63a5\u6536\u548c\u663e\u793a\u5185\u5bb9\u3002<\/p>\n<p>\u5728\u54cd\u5e94\u4f53\u8fc7\u6ee4\u6a21\u5757\u4e2d\uff0c\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f buf \u7684\u6807\u5fd7\u4f4d\uff0c\u5b8c\u6574\u63cf\u8ff0\u53ef\u4ee5\u5728\u201c\u76f8\u5173\u7ed3\u6784\u4f53\u201d\u8fd9\u4e2a\u8282\u4e2d\u770b\u5230\u3002\u5982\u679c buf \u4e2d\u5305\u542b last \u6807\u5fd7\uff0c\u8bf4\u660e\u662f\u6700\u540e\u4e00\u5757 buf\uff0c\u53ef\u4ee5\u76f4\u63a5\u8f93\u51fa\u5e76\u7ed3\u675f\u8bf7\u6c42\u4e86\u3002\u5982\u679c\u6709 flush \u6807\u5fd7\uff0c\u8bf4\u660e\u8fd9\u5757 buf \u9700\u8981\u9a6c\u4e0a\u8f93\u51fa\uff0c\u4e0d\u80fd\u7f13\u5b58\u3002\u5982\u679c\u6574\u5757 buffer \u7ecf\u8fc7\u5904\u7406\u5b8c\u4ee5\u540e\uff0c\u6ca1\u6709\u6570\u636e\u4e86\uff0c\u4f60\u53ef\u4ee5\u628a buffer \u7684 sync \u6807\u5fd7\u7f6e\u4e0a\uff0c\u8868\u793a\u53ea\u662f\u540c\u6b65\u7684\u7528\u5904\u3002<\/p>\n<p>\u5f53\u6240\u6709\u7684\u8fc7\u6ee4\u6a21\u5757\u90fd\u5904\u7406\u5b8c\u6bd5\u65f6\uff0c\u5728\u6700\u540e\u7684 write_fitler \u6a21\u5757\u4e2d\uff0cNginx \u4f1a\u5c06 in \u8f93\u51fa\u94fe\u62f7\u8d1d\u5230 r-&gt;out \u8f93\u51fa\u94fe\u7684\u672b\u5c3e\uff0c\u7136\u540e\u8c03\u7528 sendfile \u6216\u8005 writev \u63a5\u53e3\u8f93\u51fa\u3002\u7531\u4e8e Nginx \u662f\u975e\u963b\u585e\u7684 socket \u63a5\u53e3\uff0c\u5199\u64cd\u4f5c\u5e76\u4e0d\u4e00\u5b9a\u4f1a\u6210\u529f\uff0c\u53ef\u80fd\u4f1a\u6709\u90e8\u5206\u6570\u636e\u8fd8\u6b8b\u5b58\u5728 r-&gt;out\u3002\u5728\u4e0b\u6b21\u7684\u8c03\u7528\u4e2d\uff0cNginx \u4f1a\u7ee7\u7eed\u5c1d\u8bd5\u53d1\u9001\uff0c\u76f4\u81f3\u6210\u529f\u3002<\/p>\n<h2>\u53d1\u51fa\u5b50\u8bf7\u6c42<\/h2>\n<p>Nginx \u8fc7\u6ee4\u6a21\u5757\u4e00\u5927\u7279\u8272\u5c31\u662f\u53ef\u4ee5\u53d1\u51fa\u5b50\u8bf7\u6c42\uff0c\u4e5f\u5c31\u662f\u5728\u8fc7\u6ee4\u54cd\u5e94\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u53d1\u9001\u65b0\u7684\u8bf7\u6c42\uff0cNginx \u4f1a\u6839\u636e\u4f60\u8c03\u7528\u7684\u5148\u540e\u987a\u5e8f\uff0c\u5c06\u591a\u4e2a\u56de\u590d\u7684\u5185\u5bb9\u62fc\u63a5\u6210\u6b63\u5e38\u7684\u54cd\u5e94\u4e3b\u4f53\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u53ef\u4ee5\u53c2\u8003 addition \u6a21\u5757\u3002<\/p>\n<p>Nginx \u662f\u5982\u4f55\u4fdd\u8bc1\u7236\u8bf7\u6c42\u548c\u5b50\u8bf7\u6c42\u7684\u987a\u5e8f\u5462\uff1f\u5f53 Nginx \u53d1\u51fa\u5b50\u8bf7\u6c42\u65f6\uff0c\u5c31\u4f1a\u8c03\u7528 ngx_http_subrequest \u51fd\u6570\uff0c\u5c06\u5b50\u8bf7\u6c42\u63d2\u5165\u7236\u8bf7\u6c42\u7684 r-&gt;postponed \u94fe\u8868\u4e2d\u3002\u5b50\u8bf7\u6c42\u4f1a\u5728\u4e3b\u8bf7\u6c42\u6267\u884c\u5b8c\u6bd5\u65f6\u83b7\u5f97\u4f9d\u6b21\u8c03\u7528\u3002\u5b50\u8bf7\u6c42\u540c\u6837\u4f1a\u6709\u4e00\u4e2a\u8bf7\u6c42\u6240\u6709\u7684\u751f\u5b58\u671f\u548c\u5904\u7406\u8fc7\u7a0b\uff0c\u4e5f\u4f1a\u8fdb\u5165\u8fc7\u6ee4\u6a21\u5757\u6d41\u7a0b\u3002<\/p>\n<p>\u5173\u952e\u70b9\u662f\u5728 postpone_filter \u6a21\u5757\u4e2d\uff0c\u5b83\u4f1a\u62fc\u63a5\u4e3b\u8bf7\u6c42\u548c\u5b50\u8bf7\u6c42\u7684\u54cd\u5e94\u5185\u5bb9\u3002r-&gt;postponed \u6309\u6b21\u5e8f\u4fdd\u5b58\u6709\u7236\u8bf7\u6c42\u548c\u5b50\u8bf7\u6c42\uff0c\u5b83\u662f\u4e00\u4e2a\u94fe\u8868\uff0c\u5982\u679c\u524d\u9762\u4e00\u4e2a\u8bf7\u6c42\u672a\u5b8c\u6210\uff0c\u90a3\u540e\u4e00\u4e2a\u8bf7\u6c42\u5185\u5bb9\u5c31\u4e0d\u4f1a\u8f93\u51fa\u3002\u5f53\u524d\u4e00\u4e2a\u8bf7\u6c42\u5b8c\u6210\u65f6\u5e76\u8f93\u51fa\u65f6\uff0c\u540e\u4e00\u4e2a\u8bf7\u6c42\u624d\u53ef\u8f93\u51fa\uff0c\u5f53\u6240\u6709\u7684\u5b50\u8bf7\u6c42\u90fd\u5b8c\u6210\u65f6\uff0c\u6240\u6709\u7684\u54cd\u5e94\u5185\u5bb9\u4e5f\u5c31\u8f93\u51fa\u5b8c\u6bd5\u4e86\u3002<\/p>\n<h2>\u4e00\u4e9b\u4f18\u5316\u63aa\u65bd<\/h2>\n<p>Nginx \u8fc7\u6ee4\u6a21\u5757\u6d89\u53ca\u5230\u7684\u7ed3\u6784\u4f53\uff0c\u4e3b\u8981\u5c31\u662f chain \u548c buf\uff0c\u975e\u5e38\u7b80\u5355\u3002\u5728\u65e5\u5e38\u7684\u8fc7\u6ee4\u6a21\u5757\u4e2d\uff0c\u8fd9\u4e24\u7c7b\u7ed3\u6784\u4f7f\u7528\u975e\u5e38\u9891\u7e41\uff0cNginx\u91c7\u7528\u7c7b\u4f3c freelist \u91cd\u590d\u5229\u7528\u7684\u539f\u5219\uff0c\u5c06\u4f7f\u7528\u5b8c\u6bd5\u7684 chain \u6216\u8005 buf \u7ed3\u6784\u4f53\uff0c\u653e\u7f6e\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684\u7a7a\u95f2\u94fe\u8868\u91cc\uff0c\u4ee5\u5f85\u4e0b\u6b21\u4f7f\u7528\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u5728\u901a\u7528\u5185\u5b58\u6c60\u7ed3\u6784\u4f53\u4e2d\uff0cpool-&gt;chain \u53d8\u91cf\u91cc\u9762\u5c31\u4fdd\u5b58\u7740\u91ca\u653e\u7684 chain\u3002\u800c\u4e00\u822c\u7684 buf \u7ed3\u6784\u4f53\uff0c\u6ca1\u6709\u6a21\u5757\u95f4\u516c\u7528\u7684\u7a7a\u95f2\u94fe\u8868\u6c60\uff0c\u90fd\u662f\u4fdd\u5b58\u5728\u5404\u6a21\u5757\u7684\u7f13\u5b58\u7a7a\u95f2\u94fe\u8868\u6c60\u91cc\u9762\u3002\u5bf9\u4e8e buf \u7ed3\u6784\u4f53\uff0c\u8fd8\u6709\u4e00\u79cd busy \u94fe\u8868\uff0c\u8868\u793a\u8be5\u94fe\u8868\u4e2d\u7684 buf \u90fd\u5904\u4e8e\u8f93\u51fa\u72b6\u6001\uff0c\u5982\u679c buf \u8f93\u51fa\u5b8c\u6bd5\uff0c\u8fd9\u4e9b buf \u5c31\u53ef\u4ee5\u91ca\u653e\u5e76\u91cd\u590d\u5229\u7528\u4e86\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left\">\u529f\u80fd<\/th>\n<th style=\"text-align: left\">\u51fd\u6570\u540d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left\">chain \u5206\u914d<\/td>\n<td style=\"text-align: left\">ngx_alloc_chain_link<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">chain \u91ca\u653e<\/td>\n<td style=\"text-align: left\">ngx_free_chain<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">buf \u5206\u914d<\/td>\n<td style=\"text-align: left\">ngx_chain_get_free_buf<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\">buf \u91ca\u653e<\/td>\n<td style=\"text-align: left\">ngx_chain_update_chains<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u8fc7\u6ee4\u5185\u5bb9\u7684\u7f13\u5b58<\/h2>\n<p>\u7531\u4e8e Nginx \u8bbe\u8ba1\u6d41\u5f0f\u7684\u8f93\u51fa\u7ed3\u6784\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u54cd\u5e94\u5185\u5bb9\u4f5c\u5168\u6587\u8fc7\u6ee4\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u7f13\u5b58\u90e8\u5206\u7684 buf \u5185\u5bb9\u3002\u8be5\u7c7b\u8fc7\u6ee4\u6a21\u5757\u5f80\u5f80\u6bd4\u8f83\u590d\u6742\uff0c\u6bd4\u5982 sub\uff0cssi\uff0cgzip \u7b49\u6a21\u5757\u3002\u8fd9\u7c7b\u6a21\u5757\u7684\u8bbe\u8ba1\u975e\u5e38\u7075\u6d3b\uff0c\u6211\u7b80\u5355\u8bb2\u4e00\u4e0b\u8bbe\u8ba1\u539f\u5219\uff1a<\/p>\n<ol>\n<li>\n<p>\u8f93\u5165\u94fe in \u9700\u8981\u62f7\u8d1d\u64cd\u4f5c\uff0c\u7ecf\u8fc7\u7f13\u5b58\u7684\u8fc7\u6ee4\u6a21\u5757\uff0c\u8f93\u5165\u8f93\u51fa\u94fe\u5f80\u5f80\u5df2\u7ecf\u5b8c\u5168\u4e0d\u4e00\u6837\u4e86\uff0c\u6240\u4ee5\u9700\u8981\u62f7\u8d1d\uff0c\u901a\u8fc7 ngx_chain_add_copy \u51fd\u6570\u5b8c\u6210\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e00\u822c\u6709\u81ea\u5df1\u7684 free \u548c busy \u7f13\u5b58\u94fe\u8868\u6c60\uff0c\u53ef\u4ee5\u63d0\u9ad8 buf \u5206\u914d\u6548\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u9700\u8981\u5206\u914d\u5927\u5757\u5185\u5bb9\uff0c\u4e00\u822c\u5206\u914d\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5361\uff0c\u5e76\u8bbe\u7f6e recycled \u6807\u5fd7\uff0c\u8868\u793a\u53ef\u4ee5\u91cd\u590d\u5229\u7528\u3002<\/p>\n<\/li>\n<li>\u539f\u6709\u7684\u8f93\u5165 buf \u88ab\u66ff\u6362\u7f13\u5b58\u65f6\uff0c\u5fc5\u987b\u5c06\u5176 buf-&gt;pos \u8bbe\u4e3a buf-&gt;last\uff0c\u8868\u660e\u539f\u6709\u7684 buf \u5df2\u7ecf\u88ab\u8f93\u51fa\u5b8c\u6bd5\u3002\u6216\u8005\u5728\u65b0\u5efa\u7acb\u7684 buf\uff0c\u5c06 buf-&gt;shadow \u6307\u5411\u65e7\u7684 buf\uff0c\u4ee5\u4fbf\u8f93\u51fa\u5b8c\u6bd5\u65f6\u53ca\u65f6\u91ca\u653e\u65e7\u7684 buf\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u8fc7\u6ee4\u6a21\u5757\u7684\u5206\u6790 \u76f8\u5173\u7ed3\u6784\u4f53 ngx_chain_t \u7ed3\u6784\u975e\u5e38\u7b80\u5355\uff0c\u662f\u4e00\u4e2a\u5355\u5411\u94fe\u8868\uff1a typedef struc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":185838,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-185837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=185837"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185837\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/185838"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=185837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=185837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=185837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}