{"id":16719,"date":"2024-11-18T15:21:10","date_gmt":"2024-11-18T07:21:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16719\/"},"modified":"2024-11-18T15:21:10","modified_gmt":"2024-11-18T07:21:10","slug":"%e8%a7%a3%e8%af%bbnginx%e7%9a%84%e6%a8%a1%e5%9d%97%e5%bc%80%e5%8f%91%e5%92%8c%e6%89%a9%e5%b1%95%e6%9c%ba%e5%88%b6%e7%9a%84%e5%ba%95%e5%b1%82%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16719\/","title":{"rendered":"\u89e3\u8bfbNginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406"},"content":{"rendered":"<p>\u89e3\u8bfbnginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406<\/p>\n<p>Nginx\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9ad8\u6027\u80fdWeb\u670d\u52a1\u5668\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5b83\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6269\u5c55Nginx\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u89e3\u6790Nginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>Nginx\u6a21\u5757\u7684\u7ed3\u6784<br \/>\u4e00\u4e2a\u6807\u51c6\u7684Nginx\u6a21\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7684\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u4f1a\u5728Nginx\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5e94\u65f6\u673a\u88ab\u8c03\u7528\u3002\u4e00\u4e2aNginx\u6a21\u5757\u7684\u7ed3\u6784\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>#include &lt;ngx_config.h&gt;\n#include &lt;ngx_core.h&gt;\n#include &lt;ngx_http.h&gt;\n\nstatic ngx_int_t ngx_http_example_handler(ngx_http_request_t *r);\n\nstatic ngx_http_module_t ngx_http_example_module_ctx = {\n    NULL,                          \/* preconfiguration *\/\n    NULL,                          \/* postconfiguration *\/\n\n    NULL,                          \/* create main configuration *\/\n    NULL,                          \/* init main configuration *\/\n\n    NULL,                          \/* create server configuration *\/\n    NULL,                          \/* merge server configuration *\/\n\n    NULL,                          \/* create location configuration *\/\n    NULL                           \/* merge location configuration *\/\n};\n\nngx_module_t ngx_http_example_module = {\n    NGX_MODULE_V1,\n    &amp;amp;ngx_http_example_module_ctx,  \/* module context *\/\n    NULL,                          \/* module directives *\/\n    NGX_HTTP_MODULE,               \/* module type *\/\n    NULL,                          \/* init master *\/\n    NULL,                          \/* init module *\/\n    NULL,                          \/* init process *\/\n    NULL,                          \/* init thread *\/\n    NULL,                          \/* exit thread *\/\n    NULL,                          \/* exit process *\/\n    NULL,                          \/* exit master *\/\n    NGX_MODULE_V1_PADDING\n};\n\nstatic ngx_command_t ngx_http_example_commands[] = {\n    { ngx_string(\"example\"),\n      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,\n      ngx_http_example_command,\n      NGX_HTTP_LOC_CONF_OFFSET,\n      0,\n      NULL },\n    \n    ngx_null_command\n};\n\nstatic ngx_http_module_t ngx_http_example_module_ctx = {\n    NULL,                          \/* preconfiguration *\/\n    NULL,                          \/* postconfiguration *\/\n\n    NULL,                          \/* create main configuration *\/\n    NULL,                          \/* init main configuration *\/\n\n    NULL,                          \/* create server configuration *\/\n    NULL,                          \/* merge server configuration *\/\n\n    NULL,                          \/* create location configuration *\/\n    NULL                           \/* merge location configuration *\/\n};\n\nngx_module_t ngx_http_example_module = {\n    NGX_MODULE_V1,\n    &amp;amp;ngx_http_example_module_ctx,  \/* module context *\/\n    ngx_http_example_commands,     \/* module directives *\/\n    NGX_HTTP_MODULE,               \/* module type *\/\n    NULL,                          \/* init master *\/\n    NULL,                          \/* init module *\/\n    NULL,                          \/* init process *\/\n    NULL,                          \/* init thread *\/\n    NULL,                          \/* exit thread *\/\n    NULL,                          \/* exit process *\/\n    NULL,                          \/* exit master *\/\n    NGX_MODULE_V1_PADDING\n};&lt;\/ngx_http.h&gt;&lt;\/ngx_core.h&gt;&lt;\/ngx_config.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230ngx_module_t\u7ed3\u6784\u5b9a\u4e49\u4e86\u4e00\u4e2aNginx\u6a21\u5757\uff0c\u5e76\u6307\u5b9a\u4e86\u8be5\u6a21\u5757\u7684\u4e0a\u4e0b\u6587\u548c\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002ngx_http_module_t\u7ed3\u6784\u5219\u662f\u7528\u4e8eHTTP\u6a21\u5757\u7684\u5b9a\u4e49\u3002<\/p>\n<ol>\n<li>Nginx\u6a21\u5757\u7684\u6838\u5fc3\u56de\u8c03\u51fd\u6570<br \/>Nginx\u6a21\u5757\u7684\u6838\u5fc3\u56de\u8c03\u51fd\u6570\u901a\u8fc7ngx_http_module_t\u7ed3\u6784\u4e2d\u7684\u6307\u9488\u6307\u5411\u76f8\u5e94\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6838\u5fc3\u56de\u8c03\u51fd\u6570\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>static ngx_int_t ngx_http_example_handler(ngx_http_request_t *r)\n{\n    ngx_int_t rc;\n    ngx_buf_t *b;\n    ngx_chain_t out;\n\n    \/* \u521b\u5efa\u4e00\u4e2a\u8f93\u51fa\u7f13\u51b2\u533a *\/\n    b = ngx_pcalloc(r-&amp;gt;pool, sizeof(ngx_buf_t));\n    if (b == NULL) {\n        return NGX_HTTP_INTERNAL_SERVER_ERROR;\n    }\n    out.buf = b;\n    out.next = NULL;\n\n    \/* \u8bbe\u7f6e\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u5185\u5bb9 *\/\n    b-&amp;gt;pos = (u_char *) \"Hello, Nginx!\";\n    b-&amp;gt;last = b-&amp;gt;pos + sizeof(\"Hello, Nginx!\") - 1;\n    b-&amp;gt;memory = 1;\n    b-&amp;gt;last_buf = 1;\n\n    \/* \u8bbe\u7f6e\u54cd\u5e94\u5934\u90e8 *\/\n    r-&amp;gt;headers_out.status = NGX_HTTP_OK;\n    r-&amp;gt;headers_out.content_length_n = sizeof(\"Hello, Nginx!\") - 1;\n    rc = ngx_http_send_header(r);\n\n    \/* \u53d1\u9001\u54cd\u5e94\u5185\u5bb9 *\/\n    if (rc == NGX_ERROR || rc &amp;gt; NGX_OK || r-&amp;gt;header_only) {\n        return rc;\n    }\n    return ngx_http_output_filter(r, &amp;amp;out);\n}\n\nstatic ngx_int_t ngx_http_example_init(ngx_conf_t *cf)\n{\n    \/* \u83b7\u53d6http\u6a21\u5757\u7684ngx_http_core_module\u4e0a\u4e0b\u6587 *\/\n    ngx_http_core_main_conf_t *cmcf;\n    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n\n    \/* \u5728ngx_http_core_module\u7684\u5904\u7406\u8bf7\u6c42\u7684\u56de\u8c03\u51fd\u6570\u6570\u7ec4handlers\u4e2d\u52a0\u5165\u81ea\u5b9a\u4e49\u56de\u8c03\u51fd\u6570 *\/\n    ngx_http_handler_pt *h;\n    h = ngx_array_push(&amp;amp;cmcf-&amp;gt;phases[NGX_HTTP_CONTENT_PHASE].handlers);\n    if (h == NULL) {\n        return NGX_ERROR;\n    }\n    *h = ngx_http_example_handler;\n\n    return NGX_OK;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0cngx_http_example_handler\u51fd\u6570\u662f\u5b9e\u9645\u5904\u7406HTTP\u8bf7\u6c42\u7684\u51fd\u6570\u3002\u6b64\u5916\uff0cngx_http_example_init\u51fd\u6570\u7528\u4e8e\u5c06ngx_http_example_handler\u6dfb\u52a0\u5230Nginx\u7684\u8bf7\u6c42\u5904\u7406\u56de\u8c03\u51fd\u6570\u6570\u7ec4\u4e2d\u3002<\/p>\n<ol>\n<li>Nginx\u6a21\u5757\u7684\u7f16\u8bd1\u548c\u52a0\u8f7d<br \/>\u7f16\u8bd1Nginx\u6a21\u5757\u7684\u65f6\u5019\uff0c\u9700\u8981\u5728configure\u547d\u4ee4\u4e2d\u52a0\u5165&#8211;add-module=\/path\/to\/module\/directory\u53c2\u6570\uff0c\u5c06\u6a21\u5757\u7684\u6e90\u7801\u76ee\u5f55\u4f20\u9012\u7ed9configure\u811a\u672c\u3002\u7136\u540e\u4f7f\u7528make\u547d\u4ee4\u7f16\u8bd1Nginx\u3002<\/li>\n<\/ol>\n<p>\u52a0\u8f7dNginx\u6a21\u5757\uff0c\u53ef\u4ee5\u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528load_module\u6307\u4ee4\uff0c\u6307\u5b9a\u6a21\u5757\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>load_module \/path\/to\/module.so;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u603b\u7ed3<br \/>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Nginx\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff0c\u5e76\u7ed9\u51fa\u4e86\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u5bf9Nginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u4e3a\u81ea\u5df1\u7684\u9879\u76ee\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u89e3\u8bfbNginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u8bfbnginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406 Nginx\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u9ad8\u6027\u80fdWeb\u670d\u52a1\u5668\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5b83\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6269\u5c55Nginx\u7684\u529f\u80fd\u3002\u672c\u6587\u5c06\u89e3\u6790Nginx\u7684\u6a21\u5757\u5f00\u53d1\u548c\u6269\u5c55\u673a\u5236\u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002 Nginx\u6a21\u5757\u7684\u7ed3\u6784\u4e00\u4e2a\u6807\u51c6\u7684Nginx\u6a21\u5757\u662f\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7684\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u4f1a\u5728Nginx\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5e94\u65f6\u673a\u88ab\u8c03\u7528\u3002\u4e00\u4e2aNginx\u6a21\u5757\u7684\u7ed3\u6784\u793a\u4f8b\u5982\u4e0b\uff1a #include &lt;ngx_config.h&gt; #include &lt;ngx_core.h&gt; #include &lt;ngx_http.h&gt; static ngx_int_t ngx_http_example_handler(ngx_http_request_t *r); static ngx_http_module_t ngx_http_example_module_ctx = { NULL, \/* preconfiguration *\/ NULL, \/* postconfiguration *\/ NULL, \/* create main configuration *\/ NULL, \/* init main configuration *\/ NULL, \/* create server configuration *\/ NULL, \/* merge server configuration *\/ NULL, \/* create location configuration [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-16719","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=16719"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16719\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}