我想将我的项目部署到生产中,不幸的是我有一个搅拌机问题react-router
嵌套路由器方法
我已经尝试解决这个问题 3 小时了,但我能弄清楚。
我不完全擅长 docker 和 nginx 配置
当我在开发中运行该项目时,它工作正常,甚至在正常构建下也是如此。毕竟我注意到了调试方法,我让知道问题不仅仅出现在反应路由器中容器配置.
Note:上次生产构建使用相同的路由方法运行良好,我没有更改任何内容。
默认配置文件
server {
listen 3000;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
expires -1; # Set it to different value depending on your standard requirements
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Dockerfile
FROM node:14-alpine as react-build
# => Run container
FROM nginx:1.15.2-alpine
ENV NODE_ENV production
COPY package.json /home/node/app
RUN npm install
COPY . /home/node/app
RUN npm run build
# Nginx config
RUN rm -rf /etc/nginx/conf.d
COPY conf /etc/nginx
# Static build
COPY --from=react-build /home/node/app/build /usr/share/nginx/html
# Default port exposure
EXPOSE 3000
# Copy .env file and shell script to container
WORKDIR /usr/share/nginx/html
COPY ./env.sh .
COPY .env .
COPY .env.local .
# Add bash
RUN apk add --no-cache bash
# Make our shell script executable
RUN chmod +x env.sh
#Stream Editor CR LF
RUN sed -i -e 's/\r$//' /usr/share/nginx/html/env.sh
# Start Nginx server
CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
这是路由文件树
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import store from './Redux/Store/Store';
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
document.getElementById('root')
app.js
return (
<>
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/inventory/*" element={<Inventory />} />
<Route path="/process/*" element={<Process />} />
</Routes>
</>
)
库存.js
return (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<RawItems />} />
<Route path=":Item/*" element={<Material />} />
</Route>
</Routes>
</>
)
Material.js
return (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Cupper />} />
<Route path="alumin" element={<Aluminium />} />
</Route>
</Routes>
</>
)
Issue:这是问题发生的地方
应用程序到库存 ===> 工作正常
inventory/:Item ====> 渲染 Material.js 组件
当 uri 触及这条路径时
inventory/:Item/
and inventory/:Item/alumin
===> 该路径未渲染Cupper.js
and Aluminium.js
这里的问题是什么,为什么这个嵌套路由不起作用(inventory/:Item/*)
如果它不起作用然后why previous nested route working(/inventory/*)
**
我完全困惑问题发生在哪里。是在 nginx docker 容器构建中还是react-router
实施问题?