使用nginx部署React項目

兩種部署方式

一、部署在nginx根目錄下

二、部署在nginx非根目錄下

在package.json里添加 "homepage": ".",如下

  "homepage": ".",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
 },

然后在添加 basename

import {createBrowserHistory} from "history";

const history = createBrowserHistory({basename: "/app"});
export default history

完成這兩步后執(zhí)行 yarn build 打包,生成build目錄。

把build目錄上傳到服務(wù)器的某個目錄,比如 /usr/local/var/www 目錄下,
然后在nginx.conf里添加

location /app {
    alias /usr/local/var/www/build;
    index index.html;
    try_files $uri /app/index.html; # 始終返回index.html
} 

執(zhí)行 nginx -s reload 刷新,在頁面打開 http://153.100.71.119/app,即可訪問頁面。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容