{"id":10006188,"date":"2022-08-12T17:42:36","date_gmt":"2022-08-12T09:42:36","guid":{"rendered":"https:\/\/ityet.com\/?p=10006188"},"modified":"2022-08-12T17:42:36","modified_gmt":"2022-08-12T09:42:36","slug":"%e7%94%a8-http-middleware-proxy-%e9%80%a0%e4%ba%86%e4%b8%aa%e4%bb%a3%e7%90%86%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006188","title":{"rendered":"\u7528 http-middleware-proxy \u9020\u4e86\u4e2a\u4ee3\u7406\u5de5\u5177"},"content":{"rendered":"<p><code>easy-proxy<\/code> \u4e00\u4e2a\u7b80\u5355\u3001\u5b9e\u7528\u3001\u8d85\u8f7b\u91cf\u7684\u4ee3\u7406\u5de5\u5177\u3002<\/p>\n<h2>\u53cd\u5411\u4ee3\u7406<\/h2>\n<p>\u53cd\u5411\u4ee3\u7406\u662f\u4e2a\u8f7b\u91cf\u670d\u52a1\uff0c\u4f60\u5411\u4ed6\u8bf7\u6c42\uff0c\u4ed6\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u5176\u4ed6\u670d\u52a1\u5e76\u5c06\u54cd\u5e94\u8fd4\u56de\u7ed9\u4f60\u3002<\/p>\n<p>\u5927\u806a\u660e\u7ed9\u4f60\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u4f60\u53ef\u4ee5\u5c06\u53cd\u5411\u4ee3\u7406\u7c7b\u6bd4\u4e3a\u4e92\u8054\u7f51\u5927\u5382\u7684\u5c0f\u7ec4\u957f\u3002<\/p>\n<p>\u6709\u5565\u7528\uff1f\uff1f\u53ef\u4ee5\u7528\u4ee3\u7406\u5de5\u5177\u968f\u610f\u7684\u642d\u914d\u5f00\u53d1\u73af\u5883\u3002<br \/>`<br \/>\u4e0b\u56fe\u4e2d\u53ef\u4ee5\u5c06\u670d\u52a1A\u3001\u670d\u52a1B\u3001Web Server\u4efb\u610f\u4e00\u4e2a\u66ff\u6362\u6210\u672c\u5730\u670d\u52a1\uff0c\u7136\u540e\u901a\u8fc7\u8bbf\u95ee <code>\u4ee3\u7406\u670d\u52a1\u5730\u5740<\/code> \u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042311839\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>easy-proxy \u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>step 1: \u4e0b\u8f7d\u8d44\u6e90\u5305 <\/p>\n<p>step 2: \u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff0ceasy-proxy.yml<\/p>\n<pre><code class=\"yml\">proxy:\n  # \u540e\u7aef\u670d\u52a1 A \u7684\u516c\u5171\u8def\u5f84, \u4f7f\u7528 \/api \u8fdb\u884c\u5339\u914d\n  &quot;\/api&quot;:\n    # \u672c\u5730\u670d\u52a1\u5730\u5740\n    target: http:\/\/127.0.0.1:8085\/\n    changeOrigin: true\n    # \u63a5\u53e3\u524d\u7f00\u548c\u7ebf\u4e0a\u4fdd\u6301\u4e00\u81f4\u3002\u8868\u793a\u63a5\u53e3\u4e0a\u7ebf\u540e\u52a0\u4e86\u7edf\u4e00\u524d\u7f00 \/api, \u672c\u5730\u670d\u52a1\u9700\u8981\u66ff\u6362\u6389\n    pathRewrite:\n      &quot;^\/api&quot;: ''\n\n  # \u540e\u7aef\u670d\u52a1 B \u7684\u516c\u5171\u8def\u5f84, \u4f7f\u7528 \/api-2 \u8fdb\u884c\u5339\u914d\n  &quot;\/api-2&quot;:\n    target: http:\/\/xxxx.dev.com.1\/api2\n    changeOrigin: true\n  # \u524d\u7aef\u9759\u6001\u8d44\u6e90\u5730\u5740\n  &quot;\/&quot;: \n    target: http:\/\/xxxx.online.com\/\n    changeOrigin: true\n# easy-proxy \u670d\u52a1\u7aef\u53e3\uff0c\u542f\u52a8\u540e\uff0c\u4f7f\u7528 http:\/\/localhost:8888\/ \nport: 8888\n## headers \u662f request header, \u53ef\u4ee5\u5b9e\u73b0\u514d\u767b\u5f55\u3002\u7528\u4e0d\u4e0a\u8bf7\u628a headers \u6ce8\u91ca\u6389\nheaders:\n  Authorization: replace this token string<\/code><\/pre>\n<p>step 3: \u542f\u52a8\u7a0b\u5e8f\uff0c\u4fdd\u6301\u8fd0\u884c\u4e0d\u8981\u5173\u95ed\u3002<\/p>\n<p>\u5199\u4e0d\u4e0b\u53bb\u4e86\uff0c\u518d\u8d34\u5f20\u56fe\u51d1\u4e0b\u7bc7\u5e45\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042311840\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<p>\u573a\u666f\u4e00\uff1a \u540e\u7aef\u6539\u9020\uff0c\u590d\u6742\u8bf7\u6c42\u60f3\u7528\u4e0b\u524d\u7aef\u754c\u9762\u8bf7\u6c42\u672c\u5730\u3002\u4f7f\u7528 easy-proxy \u914d\u7f6e\u4e24\u6761\u89c4\u5219\uff08\u524d\u7aef\u9759\u6001\u8d44\u6e90\uff0c\u672c\u5730\u670d\u52a1\uff09\u5373\u53ef<\/p>\n<p>\u573a\u666f\u4e8c\uff1a \u524d\u7aef\u5de8\u578b\u9879\u76ee npm run dev \u8d85\u6162\uff0c\u548c\u4e0d\u540c\u540e\u7aef\u5730\u5740\u8054\u8c03\u65f6\u9700\u8981\u9891\u7e41\u7b49\u5f85 npm run dev\u3002\u4f7f\u7528 easy-proxy \u53ea\u9700\u6539\u4e0b\u914d\u7f6e\uff0c\u79d2\u8d77\u3002\u5176\u5b9e easy-proxy \u7684\u539f\u7406\u5c31\u662f\u5c06 webpack \u7684 proxy \u89e3\u8026\u4e86\u51fa\u6765\u3002\u524d\u7aef\u7684\u5c0f\u4f19\u4f34\u5e94\u8be5\u53ef\u4ee5\u79d2\u61c2\u3002<\/p>\n<p>\u573a\u666f\u4e09\uff1a\u7eaf\u540e\u7aef\u63a5\u53e3\u573a\u666f\uff0cA\u670d\u52a1\u60f3\u8bf7\u6c42 B\u670d\u52a1\u7684\uff08dev, staging, production, \u6cf3\u9053A\u3001B\u3001C\uff09\u5047\u60f3\u7684\u573a\u666f\u3002\ud83d\ude02<\/p>\n<p>\u6e90\u7801\u5730\u5740\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=cSCU5OBTAKLlX6PYLcVStQ%3D%3D.anoJutxaEm2HhCbYee3cptwSfJELg4IElkaXMN%2BhwH%2BOaS64J%2B7uxT1fe7o1Z31K\" rel=\"nofollow\">https:\/\/github.com\/xiyuanyuan&#8230;<\/a><\/p>\n<p>\u5de5\u5177\u5730\u5740\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=pIepeWO7En4%2B8joAXZ8TfQ%3D%3D.BquV9MIcViFexRtLZZKAdm%2FiRhSLwSOhgYIQv1li%2F%2BfV0xdsmzLwxk8rK6RraxRzLCg6XlYI7uAWhL2mHyp%2F%2FA%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/xiyuanyuan&#8230;<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042311837\">https:\/\/segmentfault.com\/a\/1190000042311837<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>easy-proxy \u4e00\u4e2a\u7b80\u5355\u3001\u5b9e\u7528\u3001\u8d85\u8f7b\u91cf\u7684\u4ee3\u7406\u5de5\u5177\u3002 \u53cd\u5411\u4ee3\u7406 \u53cd\u5411\u4ee3\u7406\u662f\u4e2a\u8f7b\u91cf\u670d\u52a1\uff0c\u4f60\u5411\u4ed6\u8bf7\u6c42\uff0c\u4ed6\u5c06\u8bf7\u6c42 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006188","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006188"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006188\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}