reactjs - React Application 在使用 NGINX 时只显示 public/index.html
问题描述
我正在尝试将带有 Docker 的 React 应用程序部署到 Nginx 映像。
目前,一旦构建完成并运行 Docker,我可以通过 访问该页面localhost:xxxx
,但仅public/index.html
显示我的 React 应用程序中的内容(即 favicon、站点标题),而没有其他内容显示。
注意,运行npm start
显示页面没有任何问题。
以下是我的 React 应用程序的 、 和一般结构供参考Dockerfile
:nginx.conf
Dockerfile
FROM node:14.7-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
RUN npm install --silent
RUN npm install react-scripts@3.4.1 -g --silent
COPY . ./
RUN npm run build
FROM nginx:1.16.0-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
server {
listen: 80
server_name test-frontend;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
React 应用结构
test-frontend/
├── Dockerfile
├── .dockerignore
├── jsconfig.json
├── nginx.conf
├── npm-debug.log
├── package.json
├── package-lock.json
├── nodemodules/
├── package/
│ ├── ...
│ ├── ...
│ ├── ...
└── .../
├── public/
├── favicon.ico
├── icon.png
├── index.html <--- this loads fine.
├── manifest.json
├── src/
├── assets/
├── components/
├── views/
├── index.js <--- this does not populate.
├── routes.js <--- routes are ignored.
任何帮助将不胜感激。
解决方案
推荐阅读
- oracle - 错误数量或类型的参数错误 oracle 表单
- ios - iOS NSCoreDataCoreSpotlightDelegate,如何重新索引所有项目
- javascript - Node.js - 如何使用 MySQL 查询从 for 循环中返回带有数组的回调?
- codenameone - 使用代号一组件检查器树中的验证按钮
- android - Android-manifest 中的编码类型会导致以下问题:查找默认活动和编译过程
- javascript - 带有 jquery 验证的动态添加 name=name[] 的 Html 标记
- angular - _co.openSignup 不是函数
- android - Room - 只读取一次数据时不需要 LiveData?
- python - python asn1结构无法设置字段值
- c# - 此代码中反射的输出类型是什么