{"id":61106,"date":"2026-01-17T00:33:21","date_gmt":"2026-01-16T16:33:21","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/61106.html"},"modified":"2026-01-17T00:33:21","modified_gmt":"2026-01-16T16:33:21","slug":"%e3%80%90nginx-%e5%ae%9e%e6%88%98%e7%bb%8f%e9%aa%8c%e3%80%91nginx%e9%83%a8%e7%bd%b2vue3vite%e5%89%8d%e7%ab%af%e5%ba%94%e7%94%a8%e9%98%b2%e8%b8%a9%e5%9d%91%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/61106.html","title":{"rendered":"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357"},"content":{"rendered":"<h2>nginx\u90e8\u7f72vue3&#043;vite\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357<\/h2>\n<p>\u9879\u76ee\u7ed3\u6784:<\/p>\n<p>\u8fd9\u91cc\u6211\u7528\u73af\u5883\u53d8\u91cf\u6765\u5b58\u7cfb\u7edf\u914d\u7f6e\u4fe1\u606f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163318-696a684e19958.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>1. \u5728vite.config\u4e2d\u83b7\u53d6\u4e4b\u524d\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u7684\u9759\u6001\u8d44\u6e90\u8def\u5f84**<\/h3>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6, \u8d4b\u503c\u7ed9base<\/p>\n<p><span class=\"token keyword\">const<\/span> env <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">loadEnv<\/span><span class=\"token punctuation\">(<\/span>mode<span class=\"token punctuation\">,<\/span> process<span class=\"token punctuation\">.<\/span><span class=\"token function\">cwd<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163318-696a684ed26c0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>2. \u540c\u65f6\u5728\u521b\u5efarouter\u65f6\u4f20\u5165\u8fd9\u4e2a\u53c2\u6570<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163319-696a684fa40f7.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u200b web-history\u6a21\u5f0f\u624d\u9700\u8981,\u4f46\u662f\u53ef\u4ee5\u90fd\u4f20 (hash\u8def\u7531\u4e0d\u8d70\u8bf7\u6c42)<\/p>\n<h3>3. \u6253\u5305<\/h3>\n<p>pnpm build<\/p>\n<h3>4. \u6253\u5305\u7ed3\u679c\u5206\u6790\u548cnginx\u914d\u7f6e<\/h3>\n<p>vite\u5728\u6253\u5305\u7ed3\u679c\u4e2d,\u628aindex.html\u4e2d\u5f15\u7528\u7684\u672c\u5730\u9759\u6001\u8d44\u6e90\u52a0\u4e0a\u4e86vite.config\u914d\u7f6e\u7684base\u524d\u7f00<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163319-696a684ff2002.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u8fd9\u6bb5 <\/p>\n<h4>4.1 \u4ec0\u4e48\u662f\u6839\u8def\u5f84(\/) ?<\/h4>\n<p>\/abc\/prefix\/xxx.js \u91cc\u6700\u524d\u9762\u7684\u90a3\u4e2a \/ \u5230\u5e95\u662f\u8c01\u7684\u201c\u6839\u201d&#xff1f;\u5b83\u4e0d\u662f\u78c1\u76d8\u6839&#xff0c;\u4e5f\u4e0d\u662f Nginx \u7684 root\/alias \u6839&#xff0c;\u800c\u662f URL \u7684\u7ad9\u70b9\u6839&#xff08;web root&#xff09;\u3002<\/p>\n<p>\/ \u662f\u6d4f\u89c8\u5668\u773c\u4e2d\u7684\u201c\u7ad9\u70b9\u6839\u8def\u5f84\u201d<\/p>\n<p>\u5b83\u6307\u7684\u662f&#xff1a;<\/p>\n<p>http(s):\/\/\u4f60\u7684\u57df\u540d:\u7aef\u53e3\/<\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>http:\/\/localhost:89\/<\/p>\n<p>\u6240\u4ee5&#xff1a;<\/p>\n<p>\/abc\/prefix\/xxx.js<\/p>\n<p>\u8868\u793a\u6d4f\u89c8\u5668\u4f1a\u8bf7\u6c42&#xff1a;<\/p>\n<p>http:\/\/localhost:89\/abc\/prefix\/xxx.js<\/p>\n<h4>4.2 \u90a3 Nginx \u7684 root\/alias \u8d77\u4ec0\u4e48\u4f5c\u7528&#xff1f;<\/h4>\n<ul>\n<li>root \u2014\u2014 \u628a URL \u62fc\u63a5\u5230 root \u540e\u9762<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u5199&#xff1a;<\/p>\n<p>location \/abc\/prefix\/ {<br \/>\n    root \/var\/www;<br \/>\n}<\/p>\n<p>\u8bf7\u6c42 \/abc\/prefix\/xxx.js \u4f1a\u6620\u5c04\u5230\u78c1\u76d8&#xff1a;<\/p>\n<p>\/var\/www\/abc\/prefix\/xxx.js<\/p>\n<ul>\n<li>alias \u2014\u2014 \u7528 alias \u66ff\u6362\u6389 location \u524d\u7f00<\/li>\n<\/ul>\n<p>location \/abc\/prefix\/ {<br \/>\n    alias \/var\/www\/dist\/;<br \/>\n}<\/p>\n<p>\u8bf7\u6c42 \/abc\/prefix\/xxx.js \u4f1a\u6620\u5c04\u5230\u78c1\u76d8&#xff1a;<\/p>\n<p>\/var\/www\/dist\/xxx.js<\/p>\n<p>\u26a0\ufe0f  root \u540e\u9762\u7684\u8def\u5f84\u53ef\u4ee5\u4e0d\u52a0\u2019\/\u2018&#xff0c; alias \u540e\u9762\u5fc5\u987b\u52a0\u2019\/\u2019<\/p>\n<h4>\u5173\u952e\u70b9\u603b\u7ed3<\/h4>\n<ul>\n<li>\/ \u662f URL \u6839&#xff0c;\u4e0d\u662f\u78c1\u76d8\u6839<\/li>\n<\/ul>\n<p>\u5b83\u6c38\u8fdc\u6307\u5411&#xff1a;<\/p>\n<p>http:\/\/\u57df\u540d:\u7aef\u53e3\/<\/p>\n<ul>\n<li>root\/alias \u51b3\u5b9a\u7684\u662f\u201cURL \u2192 \u78c1\u76d8\u8def\u5f84\u201d\u7684\u6620\u5c04<\/li>\n<\/ul>\n<p>\u5b83\u4eec\u4e0d\u4f1a\u6539\u53d8 URL \u7684\u542b\u4e49&#xff0c;\u53ea\u51b3\u5b9a Nginx \u53bb\u54ea\u91cc\u627e\u6587\u4ef6\u3002<\/p>\n<ul>\n<li>Vite \u7684 base \u51b3\u5b9a index.html  \u91cc\u751f\u6210\u7684 URL<\/li>\n<\/ul>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>base: &#039;\/abc\/prefix\/&#039;<\/p>\n<p>\u5c31\u4f1a\u751f\u6210&#xff1a;<\/p>\n<p>&lt;script src&#061;&#034;\/abc\/prefix\/assets\/index.js&#034;&gt;&lt;\/script&gt;<\/p>\n<p>\u6d4f\u89c8\u5668\u5c31\u4f1a\u8bf7\u6c42&#xff1a;<\/p>\n<p>http:\/\/host:port\/abc\/prefix\/assets\/index.js<\/p>\n<p>\u7136\u540e Nginx \u518d\u6839\u636e root\/alias \u53bb\u78c1\u76d8\u627e\u6587\u4ef6\u3002<\/p>\n<h4>4.3 index index.html\u4ec0\u4e48\u610f\u601d<\/h4>\n<p>&#x1f31f; \u542b\u4e49&#xff1a;\u5f53\u8bf7\u6c42\u7684\u662f\u4e00\u4e2a\u201c\u76ee\u5f55\u201d\u65f6&#xff0c;\u81ea\u52a8\u8fd4\u56de\u8be5\u76ee\u5f55\u4e0b\u7684 index.html  \u6587\u4ef6<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff1a;<\/p>\n<ul>\n<li>\u7528\u6237\u8bbf\u95ee \/abc\/prefix\/<\/li>\n<li>Nginx \u4f1a\u81ea\u52a8\u5c1d\u8bd5\u8bfb\u53d6&#xff1a;<\/li>\n<\/ul>\n<p>\/abc\/prefix\/index.html<\/p>\n<p>\u5982\u679c\u4f60\u5199&#xff1a;<\/p>\n<p>index index.html index.htm;<\/p>\n<p>\u90a3\u8868\u793a&#xff1a;<\/p>\n<ul>\n<li>\u5148\u627e index.html<\/li>\n<li>\u627e\u4e0d\u5230\u518d\u627e index.htm<\/li>\n<\/ul>\n<h5>4.3.1   \u4e3a\u4ec0\u4e48 SPA&#xff08;Vue3 &#043; Vite&#xff09;\u5fc5\u987b\u5199 index&#xff1f;<\/h5>\n<p>\u56e0\u4e3a SPA \u7684\u5165\u53e3\u6587\u4ef6\u5c31\u662f index.html&#xff0c;\u7528\u6237\u8bbf\u95ee&#xff1a;<\/p>\n<p>\/abc\/prefix\/<\/p>\n<p>\u4f60\u5e0c\u671b Nginx \u81ea\u52a8\u8fd4\u56de&#xff1a;<\/p>\n<p>dist\/index.html<\/p>\n<p>\u5426\u5219\u7528\u6237\u8bbf\u95ee\u76ee\u5f55\u65f6\u4f1a\u770b\u5230 403 \u6216\u4e0b\u8f7d\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<h5>4.3.2  index \u53ea\u5bf9\u201c\u76ee\u5f55\u8bf7\u6c42\u201d\u751f\u6548&#xff0c;\u4e0d\u5bf9\u201c\u6587\u4ef6\u8bf7\u6c42\u201d\u751f\u6548<\/h5>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>\/abc\/prefix\/ \u2192 \u4f1a\u89e6\u53d1 index \u673a\u5236<\/li>\n<li>\/abc\/prefix\/index.html \u2192 \u76f4\u63a5\u8fd4\u56de\u6587\u4ef6<\/li>\n<li>\/abc\/prefix\/app.js \u2192 \u4e0d\u4f1a\u89e6\u53d1 index<\/li>\n<\/ul>\n<h5>4.3.3  index \u4e0e try_files \u7684\u5173\u7cfb<\/h5>\n<p>\u5178\u578b SPA \u914d\u7f6e&#xff1a;<\/p>\n<p>location \/abc\/prefix\/ {<br \/>\n    alias \/var\/www\/dist\/;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/abc\/prefix\/index.html;<br \/>\n}<\/p>\n<ul>\n<li>index index.html&#xff1a;\u5904\u7406\u76ee\u5f55\u8bbf\u95ee<\/li>\n<li>try_files&#xff1a;\u5904\u7406\u524d\u7aef\u8def\u7531\u5237\u65b0&#xff08;history \u6a21\u5f0f&#xff09;<\/li>\n<\/ul>\n<p>\u4e24\u8005\u914d\u5408\u624d\u80fd\u8ba9 Vue3 \u5e94\u7528\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<h4>4.4 \u4ec0\u4e48\u60c5\u51b5\u5fc5\u987b\u5199 try_files<\/h4>\n<p>\u524d\u7aef\u4f7f\u7528 history \u8def\u7531&#xff08;\u5982 Vue Router \u7684 createWebHistory()&#xff09;<\/p>\n<ul>\n<li>\u6d4f\u89c8\u5668\u5237\u65b0 \/abc\/prefix\/dashboard \u4f1a\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u8fd9\u4e2a\u8def\u5f84\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709 try_files&#xff0c;Nginx \u4f1a\u5c1d\u8bd5\u627e \/abc\/prefix\/dashboard \u8fd9\u4e2a\u76ee\u5f55\u6216\u6587\u4ef6&#xff0c;\u7ed3\u679c 404\u3002<\/li>\n<li>\u52a0\u4e0a try_files&#xff0c;\u53ef\u4ee5\u56de\u9000\u5230 index.html&#xff0c;\u7531\u524d\u7aef\u8def\u7531\u63a5\u7ba1\u3002<\/li>\n<\/ul>\n<p>\u270d\ufe0f \u600e\u4e48\u5199 try_files<\/p>\n<p>&#x1f539; \u6807\u51c6\u5199\u6cd5&#xff08;\u90e8\u7f72\u5728\u5b50\u8def\u5f84 \/abc\/prefix\/&#xff09;<\/p>\n<p>location \/abc\/prefix\/ {<br \/>\n    root \/var\/www\/html;  # \u6216 alias \/var\/www\/dist\/;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/abc\/prefix\/index.html;<br \/>\n}<\/p>\n<p>&#x1f539; \u6839\u8def\u5f84\u90e8\u7f72&#xff08;base \u4e3a &#039;\/&#039;&#xff09;<\/p>\n<p>location \/ {<br \/>\n    root \/var\/www\/html;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/index.html;<br \/>\n}<\/p>\n<p>&#x1f539; alias \u914d\u5408<\/p>\n<p>location \/abc\/prefix\/ {<br \/>\n    alias \/var\/www\/dist\/;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/abc\/prefix\/index.html;<br \/>\n}<\/p>\n<p>\u2705 \u603b\u7ed3\u53e3\u8bc0<\/p>\n<p>SPA &#043; history \u8def\u7531 \u2192 \u5fc5\u987b try_files \u56de\u9000\u5230 index.html&#xff1b;\u8def\u5f84\u8981\u4e0e location \u5bf9\u9f50\u3002<\/p>\n<h3>5. \u6848\u4f8b<\/h3>\n<p>server {<br \/>\n        listen 89;<br \/>\n        server_name localhost;<\/p>\n<p>        location \/abc\/prefix\/ {<br \/>\n            root www;<br \/>\n            index index.html index.htm;<br \/>\n            try_files $uri $uri\/ \/abc\/prefix\/index.html;<br \/>\n        }<br \/>\n    }<\/p>\n<p>nginx\u6839\u76ee\u5f55\u7ed3\u6784<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163320-696a685022201.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u6253\u5305\u6587\u4ef6\u5b58\u653e\u4f4d\u7f6e<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163320-696a68504eb14.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u6210\u679c\u9884\u89c8<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163320-696a68508f6c8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>6. \u901f\u67e5\u7248<\/h3>\n<li>\n<p>\u4e0d\u586bbase, \u6216\u8005base\u4e3a&#034;\/&#034;<\/p>\n<p>\u2705\ufe0froot \u7248<\/p>\n<p>\u90e8\u7f72\u8def\u5f84: \/var\/www\/demo<\/p>\n<p>location \/ {<br \/>\n    root \/var\/www\/demo;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/index.html;<br \/>\n}<\/p>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/localhost\/  \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/demo\/index.html<\/p>\n<p>(\u5982\u679c\u6709\u8def\u7531\u8def\u5f84\u4e5f\u4f1a\u8d70\u5230try_files\u6700\u540e\u4e5f\u662f\u8bf7\u6c42\u7684index.html)<\/p>\n<p>\u800cindex.html\u5f15\u7528\u7684\u672c\u5730\u9759\u6001\u8d44\u6e90\u8def\u5f84\u4e3a**\u2018\/assets\/xxx\u2019**, \u8fd9\u4e2a\u8def\u5f84\u4f1a\u57fa\u4e8eroot\u53bb\u627e<\/p>\n<p>\u6700\u7ec8\u4f60\u53ea\u9700\u8981\u4fdd\u8bc1\u5728 root \/var\/www\/demo\u6587\u4ef6\u5939\u4e0b,\u5305\u542bassets\u7b49\u6253\u5305\u6587\u4ef6\u5c31\u884c\u4e86<\/p>\n<p>\u4f2a\u4ee3\u7801\u8bf4\u660e!!!<\/p>\n<p>const requstURL &#061; &#039;\/assets\/test.js&#039; \/\/ \u6253\u5305\u540e\u7684\u8d44\u6e90\u8def\u5f84(\u6d4f\u89c8\u5668\u8bbf\u95eeindex.html\u65f6\u53d1\u8d77\u8bf7\u6c42)<br \/>\nconst locationPath &#061; &#039;\/&#039; \/\/ nginx \u7684 location \u8def\u5f84<br \/>\nconst rootPath &#061; &#039;\/var\/www\/demo&#039; \/\/ nginx \u7684 root \u8def\u5f84<br \/>\n\/\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/demo\/assets\/test.js<br \/>\nconst access &#061; &#096;${rootPath}${locationPath}${requstURL}&#096;<\/p>\n<p>\u274c\ufe0falias \u7248 (\u6781\u5ea6\u4e0d\u5efa\u8bae\u4f7f\u7528alias\u5339\u914d\u6839\u8def\u5f84\u2019\/&#039;)<\/p>\n<p>\u90e8\u7f72\u8def\u5f84: \/var\/www\/demo<\/p>\n<p>location \/ {<br \/>\n    alias \/var\/www\/demo\/;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/index.html;<br \/>\n}<\/p>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/localhost\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/demo\/index.html<\/p>\n<p>\u4f2a\u4ee3\u7801\u8bf4\u660e!!!<\/p>\n<p><span class=\"token keyword\">const<\/span> requstURL <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/assets\/test.js&#039;<\/span> <span class=\"token comment\">\/\/ \u6253\u5305\u540e\u7684\u8d44\u6e90\u8def\u5f84(\u6d4f\u89c8\u5668\u8bbf\u95eeindex.html\u65f6\u53d1\u8d77\u8bf7\u6c42)<\/span><br \/>\n<span class=\"token keyword\">let<\/span> locationPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 location \u8def\u5f84<\/span><br \/>\n<span class=\"token keyword\">const<\/span> aliasPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/var\/www\/demo\/&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 alias \u8def\u5f84, \u5fc5\u987b&#039;\/&#039;\u7ed3\u5c3e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/demo\/assets\/test.js<\/span><br \/>\n<span class=\"token keyword\">const<\/span> access <span class=\"token operator\">&#061;<\/span> requstURL<span class=\"token punctuation\">.<\/span><span class=\"token function\">replace<\/span><span class=\"token punctuation\">(<\/span>locationPath<span class=\"token punctuation\">,<\/span> aliasPath<span class=\"token punctuation\">)<\/span><\/p>\n<\/li>\n<li>\n<p>base\u4e3a&#034;\/prefix&#034;\u6216&#034;\/prefix\/&#034;<\/p>\n<p>\u2705\ufe0froot \u7248<\/p>\n<p>\u90e8\u7f72\u8def\u5f84: \/var\/www\/prefix (\u9700\u8981\u5efa\u548cbase\u4e00\u6837\u7684\u6587\u4ef6\u5939)<\/p>\n<p>location \/prefix\/ {<br \/>\n    root \/var\/www;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/prefix\/index.html;<br \/>\n}<\/p>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/localhost\/prefix\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/prefix\/index.html<\/p>\n<p>\u4f2a\u4ee3\u7801\u8bf4\u660e!!!<\/p>\n<p><span class=\"token keyword\">const<\/span> requstURL <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/assets\/test.js&#039;<\/span> <span class=\"token comment\">\/\/ \u6253\u5305\u540e\u7684\u8d44\u6e90\u8def\u5f84(\u6d4f\u89c8\u5668\u8bbf\u95eeindex.html\u65f6\u53d1\u8d77\u8bf7\u6c42)<\/span><br \/>\n<span class=\"token keyword\">const<\/span> locationPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/prefix\/&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 location \u8def\u5f84<\/span><br \/>\n<span class=\"token keyword\">const<\/span> rootPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/var\/www&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 root \u8def\u5f84<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/prefix\/assets\/test.js<\/span><br \/>\n<span class=\"token keyword\">const<\/span> access <span class=\"token operator\">&#061;<\/span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">&#096;<\/span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${<\/span>rootPath<span class=\"token interpolation-punctuation punctuation\">}<\/span><\/span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${<\/span>locationPath<span class=\"token interpolation-punctuation punctuation\">}<\/span><\/span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${<\/span>requstURL<span class=\"token interpolation-punctuation punctuation\">}<\/span><\/span><span class=\"token template-punctuation string\">&#096;<\/span><\/span><\/p>\n<p>\u2705\ufe0falias \u7248<\/p>\n<p>\u90e8\u7f72\u8def\u5f84: \/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939(\/a, \/a\/b, a\/b\/c, \u2026)<\/p>\n<p>location \/prefix\/ {<br \/>\n    alias \/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939;<br \/>\n    index index.html;<br \/>\n    try_files $uri $uri\/ \/prefix\/index.html;<br \/>\n}<\/p>\n<p>\u4f2a\u4ee3\u7801\u8bf4\u660e!!!<\/p>\n<p><span class=\"token keyword\">const<\/span> requstURL <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/assets\/test.js&#039;<\/span> <span class=\"token comment\">\/\/ \u6253\u5305\u540e\u7684\u8d44\u6e90\u8def\u5f84(\u6d4f\u89c8\u5668\u8bbf\u95eeindex.html\u65f6\u53d1\u8d77\u8bf7\u6c42)<\/span><br \/>\n<span class=\"token keyword\">let<\/span> locationPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/prefix\/&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 location \u8def\u5f84<\/span><br \/>\n<span class=\"token keyword\">const<\/span> aliasPath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939\/&#039;<\/span> <span class=\"token comment\">\/\/ nginx \u7684 alias \u8def\u5f84, \u5fc5\u987b&#039;\/&#039;\u7ed3\u5c3e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939\/assets\/test.js<\/span><br \/>\n<span class=\"token keyword\">const<\/span> access <span class=\"token operator\">&#061;<\/span> requstURL<span class=\"token punctuation\">.<\/span><span class=\"token function\">replace<\/span><span class=\"token punctuation\">(<\/span>locationPath<span class=\"token punctuation\">,<\/span> aliasPath<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6700\u540e\u26a0\ufe0f :<\/p>\n<ul>\n<li>try_files \u6700\u540e\u4e00\u4e2a\u56de\u9000\u5730\u5740\u9700\u4e0e Vite \u7684 base \u8def\u5f84\u5b8c\u5168\u4e00\u81f4&#xff08;\u5982 base: \u2018\/prefix\/\u2019&#xff0c;\u5219\u56de\u9000\u5730\u5740\u4e3a \/prefix\/index.html&#xff09;&#xff1b;<br \/>\ns\/test.js\u2019 \/\/ \u6253\u5305\u540e\u7684\u8d44\u6e90\u8def\u5f84(\u6d4f\u89c8\u5668\u8bbf\u95eeindex.html\u65f6\u53d1\u8d77\u8bf7\u6c42)<\/li>\n<\/ul>\n<\/li>\n<p>   let locationPath &#061; &#039;\/prefix\/&#039; \/\/ nginx \u7684 location \u8def\u5f84<br \/>\n   const aliasPath &#061; &#039;\/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939\/&#039; \/\/ nginx \u7684 alias \u8def\u5f84, \u5fc5\u987b&#039;\/&#039;\u7ed3\u5c3e<br \/>\n   \/\/ \u5b9e\u9645\u8bbf\u95ee  \/var\/www\/\u4efb\u610f\u5355\u7ea7\u6216\u591a\u7ea7\u6587\u4ef6\u5939\/assets\/test.js<br \/>\n   const access &#061; requstURL.replace(locationPath, aliasPath)<\/p>\n<p>\u6700\u540e\u26a0\ufe0f:<\/p>\n<ul>\n<li>try_files \u6700\u540e\u4e00\u4e2a\u56de\u9000\u5730\u5740\u9700\u4e0e Vite \u7684 base \u8def\u5f84\u5b8c\u5168\u4e00\u81f4&#xff08;\u5982 base: \u2018\/prefix\/\u2019&#xff0c;\u5219\u56de\u9000\u5730\u5740\u4e3a \/prefix\/index.html&#xff09;&#xff1b;<\/li>\n<li>\u4e0d\u5efa\u8bae\u7528\u2019.\/\u2019<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>nginx\u90e8\u7f72vue3vite\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357<br \/>\n\u9879\u76ee\u7ed3\u6784:<br \/>\n\u8fd9\u91cc\u6211\u7528\u73af\u5883\u53d8\u91cf\u6765\u5b58\u7cfb\u7edf\u914d\u7f6e\u4fe1\u606f1. \u5728vite.config\u4e2d\u83b7\u53d6\u4e4b\u524d\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u7684\u9759\u6001\u8d44\u6e90\u8def\u5f84**<br \/>\n\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6, \u8d4b\u503c\u7ed9base<br \/>\nconst env  loadEnv(mode, process.cwd()) \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf2. \u540c\u65f6\u5728\u521b\u5efarouter\u65f6\u4f20\u5165\u8fd9\u4e2a\u53c2\u6570\u200b web-history\u6a21\u5f0f\u624d\u9700\u8981,\u4f46\u662f\u53ef\u4ee5\u90fd\u4f20 (hash\u8def\u7531\u4e0d\u8d70\u8bf7\u6c42)<br \/>\n3. \u6253\u5305<br \/>\npnpm build4. \u6253\u5305\u7ed3\u679c\u5206\u6790\u548cnginx\u914d<\/p>\n","protected":false},"author":2,"featured_media":61099,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[117,87,44],"topic":[],"class_list":["post-61106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-nginx","tag-87","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/61106.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"nginx\u90e8\u7f72vue3vite\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 \u9879\u76ee\u7ed3\u6784: \u8fd9\u91cc\u6211\u7528\u73af\u5883\u53d8\u91cf\u6765\u5b58\u7cfb\u7edf\u914d\u7f6e\u4fe1\u606f1. \u5728vite.config\u4e2d\u83b7\u53d6\u4e4b\u524d\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u7684\u9759\u6001\u8d44\u6e90\u8def\u5f84** \u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6, \u8d4b\u503c\u7ed9base const env loadEnv(mode, process.cwd()) \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf2. \u540c\u65f6\u5728\u521b\u5efarouter\u65f6\u4f20\u5165\u8fd9\u4e2a\u53c2\u6570\u200b web-history\u6a21\u5f0f\u624d\u9700\u8981,\u4f46\u662f\u53ef\u4ee5\u90fd\u4f20 (hash\u8def\u7531\u4e0d\u8d70\u8bf7\u6c42) 3. \u6253\u5305 pnpm build4. \u6253\u5305\u7ed3\u679c\u5206\u6790\u548cnginx\u914d\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/61106.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-16T16:33:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163318-696a684e19958.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61106.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/61106.html\",\"name\":\"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-16T16:33:21+00:00\",\"dateModified\":\"2026-01-16T16:33:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/61106.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/61106.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61106.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/61106.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"nginx\u90e8\u7f72vue3vite\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 \u9879\u76ee\u7ed3\u6784: \u8fd9\u91cc\u6211\u7528\u73af\u5883\u53d8\u91cf\u6765\u5b58\u7cfb\u7edf\u914d\u7f6e\u4fe1\u606f1. \u5728vite.config\u4e2d\u83b7\u53d6\u4e4b\u524d\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u7684\u9759\u6001\u8d44\u6e90\u8def\u5f84** \u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6, \u8d4b\u503c\u7ed9base const env loadEnv(mode, process.cwd()) \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf2. \u540c\u65f6\u5728\u521b\u5efarouter\u65f6\u4f20\u5165\u8fd9\u4e2a\u53c2\u6570\u200b web-history\u6a21\u5f0f\u624d\u9700\u8981,\u4f46\u662f\u53ef\u4ee5\u90fd\u4f20 (hash\u8def\u7531\u4e0d\u8d70\u8bf7\u6c42) 3. \u6253\u5305 pnpm build4. \u6253\u5305\u7ed3\u679c\u5206\u6790\u548cnginx\u914d","og_url":"https:\/\/www.wsisp.com\/helps\/61106.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-16T16:33:21+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260116163318-696a684e19958.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/61106.html","url":"https:\/\/www.wsisp.com\/helps\/61106.html","name":"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-16T16:33:21+00:00","dateModified":"2026-01-16T16:33:21+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/61106.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/61106.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/61106.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Nginx \u5b9e\u6218\u7ecf\u9a8c\u3011nginx\u90e8\u7f72vue3+vite\u524d\u7aef\u5e94\u7528\u9632\u8e29\u5751\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=61106"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/61099"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=61106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=61106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=61106"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=61106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}