{"id":62835,"date":"2025-04-29T12:05:48","date_gmt":"2025-04-29T04:05:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/62835\/"},"modified":"2025-04-29T12:05:48","modified_gmt":"2025-04-29T04:05:48","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0nginx%e7%9a%84%e5%8a%a8%e6%80%81%e6%a8%a1%e5%9d%97%e5%8a%a0%e8%bd%bd%e9%85%8d%e7%bd%ae-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/62835\/","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0Nginx\u7684\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169942459923461.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5b9e\u73b0Nginx\u7684\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\u63d2\u56fe\" alt=\"\u5982\u4f55\u5b9e\u73b0Nginx\u7684\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\u63d2\u56fe\" \/><\/p>\n<p>Nginx\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\u548c\u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6\uff0c\u6700\u8fd1\u53d1\u5e03\u7684Nginx\u7248\u672c1.9.11\u53ca\u4ee5\u540e\u7684\u7248\u672c\u652f\u6301\u4e86\u52a8\u6001\u6a21\u5757\u7684\u52a0\u8f7d\u3002\u8fd9\u4e00\u65b0\u7279\u6027\u4f7f\u5f97\u7528\u6237\u80fd\u591f\u5728\u4e0d\u91cd\u65b0\u7f16\u8bd1\u548c\u91cd\u65b0\u5b89\u88c5Nginx\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u6dfb\u52a0\u65b0\u7684\u52a8\u6001\u6a21\u5757\u5b9e\u73b0\u529f\u80fd\u6269\u5c55\u548c\u5b9a\u5236\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Nginx\u4e2d\u5b9e\u73b0\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>Nginx\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u7684\u539f\u7406\u662f\u901a\u8fc7\u4e00\u4e2a\u65b0\u7684\u7f16\u8bd1\u6a21\u5f0f&#8211;\u201c&#8211;add-dynamic-module=module_path\u201d\u6765\u6253\u5f00\u5bf9\u52a8\u6001\u6a21\u5757\u7684\u652f\u6301\u3002\u5728\u4f7f\u7528\u8fd9\u4e2a\u7f16\u8bd1\u6a21\u5f0f\u65f6\uff0c\u7f16\u8bd1\u751f\u6210\u7684\u662f\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93(.so)\u6587\u4ef6\uff0c\u8fd9\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\u53ef\u4ee5\u5728Nginx\u542f\u52a8\u8fd0\u884c\u65f6\u88ab\u52a8\u6001\u52a0\u8f7d\u548c\u5378\u8f7d\u3002\u76f8\u5bf9\u4e8e\u9759\u6001\u6a21\u5757\uff0c\u52a8\u6001\u6a21\u5757\u5177\u6709\u66f4\u597d\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<p>1.\u7f16\u5199\u52a8\u6001\u6a21\u5757\u6e90\u7801<\/p>\n<p>\u4ee5\u5b9e\u73b0\u6dfb\u52a0Header\u5934\u90e8\u4e3a\u4f8b\uff0cHeader\u5934\u90e8\u6307\u7684\u662fWeb\u5e94\u7528\u4e2d\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5934\u90e8\uff0cNginx\u672c\u8eab\u5e76\u4e0d\u63d0\u4f9b\u6dfb\u52a0Header\u5934\u90e8\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6dfb\u52a0Header\u5934\u90e8\u7684\u793a\u4f8b\uff1a<\/p>\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_add_header_handler(ngx_http_request_t *r) {\n    ngx_table_elt_t *h;\n    h = ngx_list_push(&amp;amp;r-&amp;gt;headers_out.headers);\n    h-&amp;gt;hash = 1;\n    ngx_str_set(&amp;amp;h-&amp;gt;key, \"MyHeader\");\n    ngx_str_set(&amp;amp;h-&amp;gt;value, \"Hello Nginx!\");\n    r-&amp;gt;headers_out.content_length_n = sizeof(\"Hello Nginx!\") - 1;\n    ngx_http_clear_content_length(r);\n    ngx_http_header_filter(r);\n    ngx_http_send_header(r);\n    return NGX_OK;\n}\n\nstatic ngx_int_t ngx_http_add_header_init(ngx_conf_t *cf) {\n    ngx_http_handler_pt *h;\n    ngx_http_core_main_conf_t *cmcf;\n    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\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_add_header_handler;\n    return NGX_OK;\n}\n\nstatic ngx_http_module_t ngx_http_add_header_module_ctx = {\n    NULL, ngx_http_add_header_init, NULL, NULL, NULL, NULL, NULL, NULL\n};\n\nngx_module_t ngx_http_add_header_module = {\n    NGX_MODULE_V1, &amp;amp;ngx_http_add_header_module_ctx, NULL, NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 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>\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u4e86Nginx\u81ea\u5b9a\u4e49\u6a21\u5757ngx_http_add_header_module\uff0c\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757\u6709\u4e24\u4e2a\u51fd\u6570ngx_http_add_header_handler\u548cngx_http_add_header_init\uff0c\u524d\u8005\u7528\u6765\u5b9e\u73b0Header\u5934\u90e8\u6dfb\u52a0\u7684\u5177\u4f53\u903b\u8f91\uff0c\u540e\u8005\u7528\u6765\u521d\u59cb\u5316\u6a21\u5757\u3002\u6bcf\u4e2a\u6a21\u5757\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u6a21\u5757\u4e0a\u4e0b\u6587ngx_http_add_header_module_ctx\uff0c\u4ee5\u53ca\u4e00\u4e2angx_module_t\u7ed3\u6784\u4f53\u53d8\u91cfngx_http_add_header_module\uff0c\u5176\u4e2d\u5305\u542b\u8be5\u6a21\u5757\u7684\u7248\u672c\u53f7\u3001\u4e0a\u4e0b\u6587\u7b49\u4fe1\u606f\u3002<\/p>\n<p>2.\u7f16\u8bd1\u5e76\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93<\/p>\n<p>\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u7f16\u8bd1\u4ee3\u7801\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93:<\/p>\n<pre>.\/configure --prefix=\/usr\/local\/nginx --add-dynamic-module=.\/src\/http\/modules\/my_module &amp;amp;&amp;amp; make &amp;amp;&amp;amp; make install<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4e0a\u9762\u7684\u547d\u4ee4\u4f1a\u5728Nginx\u7684HTTP\u6a21\u5757\u4e0b\u521b\u5efamy_module\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u7740\u7528\u6765\u6784\u5efa\u52a8\u6001\u6a21\u5757\u7684Makefile\u6587\u4ef6\u548c\u4e00\u4e9b\u5fc5\u8981\u7684\u6e90\u7801\u548c\u5934\u6587\u4ef6\uff0c\u751f\u6210\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6(.so)\u5728objs\/\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>3.\u52a0\u8f7d\u52a8\u6001\u6a21\u5757<\/p>\n<p>\u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6.conf\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff0c\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\uff1a<\/p>\n<pre>load_module modules\/ngx_http_add_header_module.so;\n\nhttp {\n    server {\n        listen 80;\n        server_name myweb.com;\n        location \/ {\n            add_header MyHeader \"Hello Nginx!\";\n            root \/var\/www\/html;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728http\u6a21\u5757\u4e0b\u901a\u8fc7load_module\u6307\u4ee4\u52a0\u8f7dngx_http_add_header_module.so\u52a8\u6001\u94fe\u63a5\u5e93\u6587\u4ef6\uff0c\u63a5\u4e0b\u6765\u5b9a\u4e49\u4e00\u4e2aserver\u5757\uff0c\u5728server\u5757\u4e2d\u7684location\u914d\u7f6e\u9879\u6307\u5b9a\u6dfb\u52a0Header\u5934\u90e8\u3002<\/p>\n<p>4.\u91cd\u65b0\u52a0\u8f7dNginx<\/p>\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u52a0\u8f7dNginx\u670d\u52a1\u5668\uff1a<\/p>\n<pre>nginx -s reload<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u53ef\u4ee5\u5b9e\u73b0\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u3001\u91cd\u542fNginx\u3001\u5173\u95edNginx\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u5c31\u5b8c\u6210\u4e86Nginx\u4e2d\u5b9e\u73b0\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u4e86\u89e3Nginx 1.9.11\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u7684\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u5b9e\u73b0\u65b9\u6cd5\u3002\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u9700\u8981\u6309\u7167\u987a\u5e8f\u7f16\u5199\u52a8\u6001\u6a21\u5757\u6e90\u7801\u3001\u7f16\u8bd1\u751f\u6210\u52a8\u6001\u94fe\u63a5\u5e93\u3001\u52a0\u8f7d\u6a21\u5757\u5e93\u5e76\u4e14\u91cd\u65b0\u52a0\u8f7dNginx\u3002\u4f7f\u7528\u52a8\u6001\u6a21\u5757\u80fd\u591f\u4f7f\u5f97\u7528\u6237\u5728post-release\u9636\u6bb5\u624d\u5f00\u53d1\u67d0\u4e2a\u529f\u80fd\uff0c\u4f7f\u5f97\u4ea7\u54c1\u4e0a\u7ebf\u53d8\u5f97\u66f4\u52a0\u5feb\u901f\u3001\u7075\u6d3b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5b9e\u73b0Nginx\u7684\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\u548c\u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6\uff0c\u6700\u8fd1\u53d1\u5e03\u7684Nginx\u7248\u672c1.9.11\u53ca\u4ee5\u540e\u7684\u7248\u672c\u652f\u6301\u4e86\u52a8\u6001\u6a21\u5757\u7684\u52a0\u8f7d\u3002\u8fd9\u4e00\u65b0\u7279\u6027\u4f7f\u5f97\u7528\u6237\u80fd\u591f\u5728\u4e0d\u91cd\u65b0\u7f16\u8bd1\u548c\u91cd\u65b0\u5b89\u88c5Nginx\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u6dfb\u52a0\u65b0\u7684\u52a8\u6001\u6a21\u5757\u5b9e\u73b0\u529f\u80fd\u6269\u5c55\u548c\u5b9a\u5236\u5316\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Nginx\u4e2d\u5b9e\u73b0\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u914d\u7f6e\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 Nginx\u52a8\u6001\u6a21\u5757\u52a0\u8f7d\u7684\u539f\u7406\u662f\u901a\u8fc7\u4e00\u4e2a\u65b0\u7684\u7f16\u8bd1\u6a21\u5f0f&#8211;\u201c&#8211;add-dynamic-module=module_path\u201d\u6765\u6253\u5f00\u5bf9\u52a8\u6001\u6a21\u5757\u7684\u652f\u6301\u3002\u5728\u4f7f\u7528\u8fd9\u4e2a\u7f16\u8bd1\u6a21\u5f0f\u65f6\uff0c\u7f16\u8bd1\u751f\u6210\u7684\u662f\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93(.so)\u6587\u4ef6\uff0c\u8fd9\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\u53ef\u4ee5\u5728Nginx\u542f\u52a8\u8fd0\u884c\u65f6\u88ab\u52a8\u6001\u52a0\u8f7d\u548c\u5378\u8f7d\u3002\u76f8\u5bf9\u4e8e\u9759\u6001\u6a21\u5757\uff0c\u52a8\u6001\u6a21\u5757\u5177\u6709\u66f4\u597d\u7684\u7075\u6d3b\u6027\u3002 \u4e0b\u9762\u5c31\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a 1.\u7f16\u5199\u52a8\u6001\u6a21\u5757\u6e90\u7801 \u4ee5\u5b9e\u73b0\u6dfb\u52a0Header\u5934\u90e8\u4e3a\u4f8b\uff0cHeader\u5934\u90e8\u6307\u7684\u662fWeb\u5e94\u7528\u4e2d\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5934\u90e8\uff0cNginx\u672c\u8eab\u5e76\u4e0d\u63d0\u4f9b\u6dfb\u52a0Header\u5934\u90e8\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6dfb\u52a0Header\u5934\u90e8\u7684\u793a\u4f8b\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_add_header_handler(ngx_http_request_t *r) { ngx_table_elt_t *h; h = ngx_list_push(&amp;amp;r-&amp;gt;headers_out.headers); h-&amp;gt;hash = 1; ngx_str_set(&amp;amp;h-&amp;gt;key, &#8220;MyHeader&#8221;); ngx_str_set(&amp;amp;h-&amp;gt;value, &#8220;Hello Nginx!&#8221;); r-&amp;gt;headers_out.content_length_n = sizeof(&#8220;Hello Nginx!&#8221;) &#8211; 1; ngx_http_clear_content_length(r); ngx_http_header_filter(r); ngx_http_send_header(r); return NGX_OK; } static ngx_int_t ngx_http_add_header_init(ngx_conf_t *cf) { ngx_http_handler_pt *h; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); h [&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-62835","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62835","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=62835"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62835\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=62835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=62835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=62835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}