使用 react-app-rewired nginx 进行 React 生产

React production using react-app-rewired nginx

我尝试在 heroku 上部署我的应用程序,它运行正常。但是当我使用 react-app-rewired 脚本构建我的应用程序并使用 nginx 将它部署到我的服务器时。访问我的应用程序时,它仍然可以正常工作,但如果我使用 contextPath: http://35.240.229.243/products it throws 404 error. You can access my app in http://35.240.229.243 重新加载页面进行测试。我正在使用具有历史记录的反应路由器。帮帮我谢谢

首先你需要找到默认的nginx conf文件并禁用它,默认情况下它会在/etc/nginx/sites-enabled/default

sudo rm -rf /etc/nginx/sites-enabled/default

并在 /etc/nginx/sites-available/

下创建新文件
sudo touch /etc/nginx/sites-enabled/default/redbox

并使用 vim 或 nano 将此文本放入新的 conf 文件 redbox,

server {
        listen 80;

        root /usr/share/nginx/html;
        index index.html;

        location / {
            try_files $uri $uri/ /index.html;
        }
}

并启用它

sudo ln -s /etc/nginx/sites-available/redbox /etc/nginx/sites-enabled/redbox

接下来,确保Nginx文件没有语法错误,

sudo nginx -t

如果没有发现问题,重启Nginx。

sudo systemctl restart nginx