{"id":49860,"date":"2024-10-08T08:06:15","date_gmt":"2024-10-08T00:06:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49860\/"},"modified":"2024-10-08T08:06:15","modified_gmt":"2024-10-08T00:06:15","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%af%a6%e7%8f%be-ngx_http_sub_module-%e9%80%b2%e8%a1%8c%e9%9f%bf%e6%87%89%e9%81%8e%e6%bf%be","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49860\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u5be6\u73fe ngx_http_sub_module \u9032\u884c\u97ff\u61c9\u904e\u6ffe"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%af%a6%e7%8f%be-ngx_http_sub_module-%e9%80%b2%e8%a1%8c%e9%9f%bf%e6%87%89%e9%81%8e%e6%bf%be-LuUaEwwcSG\">Nginx \u6280\u5de7\uff1a\u5be6\u73fe ngx_http_sub_module \u9032\u884c\u97ff\u61c9\u904e\u6ffe<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5176\u6a21\u7d44\u5316\u7684\u8a2d\u8a08\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u6839\u64da\u9700\u6c42\u64f4\u5c55\u529f\u80fd\uff0c\u5176\u4e2d <code>ngx_http_sub_module<\/code> \u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u6a21\u7d44\uff0c\u80fd\u5920\u5c0d HTTP \u97ff\u61c9\u9032\u884c\u904e\u6ffe\u548c\u4fee\u6539\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 <code>ngx_http_sub_module<\/code> \u4f86\u5be6\u73fe\u97ff\u61c9\u904e\u6ffe\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6280\u5de7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ngx_http_sub_module%ef%bc%9f-LuUaEwwcSG\">\u4ec0\u9ebc\u662f ngx_http_sub_module\uff1f<\/h2>\n<p><code>ngx_http_sub_module<\/code> \u662f Nginx \u7684\u4e00\u500b\u5167\u5efa\u6a21\u7d44\uff0c\u4e3b\u8981\u7528\u65bc\u5728 HTTP \u97ff\u61c9\u4e2d\u9032\u884c\u5b57\u4e32\u66ff\u63db\u3002\u9019\u500b\u6a21\u7d44\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u5167\u5bb9\u7684\u60c5\u6cc1\u4e0b\uff0c\u52d5\u614b\u5730\u4fee\u6539\u97ff\u61c9\u6578\u64da\uff0c\u9019\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5728\u7db2\u7ad9\u9077\u79fb\u6216\u5167\u5bb9\u66f4\u65b0\u6642\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%95%9f%e7%94%a8-ngx_http_sub_module-LuUaEwwcSG\">\u5982\u4f55\u555f\u7528 ngx_http_sub_module<\/h2>\n<p>\u5728\u5927\u591a\u6578\u60c5\u6cc1\u4e0b\uff0c<code>ngx_http_sub_module<\/code> \u662f\u96a8 Nginx \u9810\u8a2d\u5b89\u88dd\u7684\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u81ea\u5b9a\u7fa9\u7de8\u8b6f\u7684\u7248\u672c\uff0c\u8acb\u78ba\u4fdd\u5728\u7de8\u8b6f\u6642\u5305\u542b\u4e86\u8a72\u6a21\u7d44\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5 Nginx \u662f\u5426\u5df2\u7d93\u555f\u7528\u8a72\u6a21\u7d44\uff1a<\/p>\n<pre><code>nginx -V<\/code><\/pre>\n<p>\u5728\u8f38\u51fa\u7684\u6a21\u7d44\u5217\u8868\u4e2d\u67e5\u627e <code>--with-http_sub_module<\/code>\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5247\u8868\u793a\u8a72\u6a21\u7d44\u5df2\u555f\u7528\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae%e7%a4%ba%e4%be%8b-LuUaEwwcSG\">\u57fa\u672c\u914d\u7f6e\u793a\u4f8b<\/h2>\n<p>\u8981\u4f7f\u7528 <code>ngx_http_sub_module<\/code>\uff0c\u60a8\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u76f8\u61c9\u7684\u8a2d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        proxy_pass http:\/\/backend_server;\n        sub_filter '\u539f\u59cb\u5b57\u4e32' '\u66ff\u63db\u5f8c\u7684\u5b57\u4e32';\n        sub_filter_once off;  # \u8a2d\u7f6e\u70ba off \u4ee5\u66ff\u63db\u6240\u6709\u51fa\u73fe\u7684\u5b57\u4e32\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u7576 Nginx \u63a5\u6536\u5230\u4f86\u81ea <code>backend_server<\/code> \u7684\u97ff\u61c9\u6642\uff0c\u6703\u5c07\u6240\u6709\u7684\u300c\u539f\u59cb\u5b57\u4e32\u300d\u66ff\u63db\u70ba\u300c\u66ff\u63db\u5f8c\u7684\u5b57\u4e32\u300d\u3002<\/p>\n<h2 id=\"%e9%80%b2%e9%9a%8e%e9%85%8d%e7%bd%ae%e6%8a%80%e5%b7%a7-LuUaEwwcSG\">\u9032\u968e\u914d\u7f6e\u6280\u5de7<\/h2>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f-LuUaEwwcSG\">\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f<\/h3>\n<p>\u5982\u679c\u60a8\u9700\u8981\u66f4\u9748\u6d3b\u7684\u5b57\u4e32\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>location \/ {\n    proxy_pass http:\/\/backend_server;\n    sub_filter_types text\/html text\/css;  # \u6307\u5b9a\u8981\u904e\u6ffe\u7684\u5167\u5bb9\u985e\u578b\n    sub_filter '\u539f\u59cb(.*)' '\u66ff\u63db\u5f8c$1';  # \u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u9032\u884c\u66ff\u63db\n}<\/code><\/pre>\n<h3 id=\"%e8%99%95%e7%90%86%e5%a4%9a%e5%80%8b%e6%9b%bf%e6%8f%9b-LuUaEwwcSG\">\u8655\u7406\u591a\u500b\u66ff\u63db<\/h3>\n<p>\u5982\u679c\u9700\u8981\u9032\u884c\u591a\u500b\u5b57\u4e32\u66ff\u63db\uff0c\u53ef\u4ee5\u5728\u914d\u7f6e\u4e2d\u6dfb\u52a0\u591a\u500b <code>sub_filter<\/code> \u6307\u4ee4\uff1a<\/p>\n<pre><code>location \/ {\n    proxy_pass http:\/\/backend_server;\n    sub_filter '\u5b57\u4e321' '\u66ff\u63db1';\n    sub_filter '\u5b57\u4e322' '\u66ff\u63db2';\n    sub_filter_once off;  # \u78ba\u4fdd\u6240\u6709\u51fa\u73fe\u7684\u5b57\u4e32\u90fd\u88ab\u66ff\u63db\n}<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-LuUaEwwcSG\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 <code>ngx_http_sub_module<\/code> \u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u8a72\u6a21\u7d44\u50c5\u652f\u6301\u5c0d\u6587\u672c\u5167\u5bb9\u9032\u884c\u66ff\u63db\uff0c\u5c0d\u65bc\u4e8c\u9032\u5236\u6578\u64da\uff08\u5982\u5716\u7247\u3001\u8996\u983b\u7b49\uff09\u7121\u6cd5\u9032\u884c\u8655\u7406\u3002<\/li>\n<li>\u4f7f\u7528 <code>sub_filter_once off<\/code> \u6703\u5f71\u97ff\u6027\u80fd\uff0c\u56e0\u70ba Nginx \u9700\u8981\u904d\u6b77\u6574\u500b\u97ff\u61c9\u5167\u5bb9\u3002<\/li>\n<li>\u78ba\u4fdd\u66ff\u63db\u7684\u5b57\u4e32\u4e0d\u6703\u5f71\u97ff\u5230 HTML \u7d50\u69cb\uff0c\u907f\u514d\u9020\u6210\u9801\u9762\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LuUaEwwcSG\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528 <code>ngx_http_sub_module<\/code>\uff0cNginx \u7528\u6236\u53ef\u4ee5\u9748\u6d3b\u5730\u5c0d HTTP \u97ff\u61c9\u9032\u884c\u904e\u6ffe\u548c\u4fee\u6539\uff0c\u9019\u5c0d\u65bc\u7db2\u7ad9\u7684\u5167\u5bb9\u7ba1\u7406\u548c\u66f4\u65b0\u975e\u5e38\u6709\u5e6b\u52a9\u3002\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u5b57\u4e32\u66ff\u63db\u9084\u662f\u8907\u96dc\u7684\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff0c\u9019\u500b\u6a21\u7d44\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u7684 ngx_http_sub_module\uff0c\u5b78\u7fd2\u5982\u4f55\u5be6\u73fe\u97ff\u61c9\u904e\u6ffe\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u5167\u5bb9\u7ba1\u7406\u7684\u6280\u5de7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-49860","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=49860"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49860\/revisions"}],"predecessor-version":[{"id":49861,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49860\/revisions\/49861"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}