兩種部署方式
一、部署在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,即可訪問頁面。