本文介绍了React在Traefik中不适用于Path和Prefix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个React App,它是使用以下Dockerfile构建的I have a React App, that I build with the following Dockerfile# base imageFROM node:latest as builder# set working directoryRUN mkdir /usr/src/appWORKDIR /usr/src/app# add `/usr/src/app/node_modules/.bin` to $PATHENV PATH /usr/src/app/node_modules/.bin:$PATH# install and cache app dependenciesCOPY app/package.json /usr/src/app/package.jsonRUN npm installRUN npm install [email protected] -gCOPY ./app/usr/src/app# start appCMD ["npm", "start"]# production environmentFROM nginx:alpineRUN rm -rf /etc/nginx/conf.dCOPY conf /etc/nginxCOPY --from=builder /usr/src/app/build /etc/nginx/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"]然后我使用以下Docker Compose运行该Then I run this with the following Docker Composebuild: .labels: - "traefik.frontend.rule=Host:www.example.com;PathPrefix:/path" - "traefik.protocol=http" - "traefik.frontend.entryPoints=https" - "traefik.port=80" - "traefik.enable=true"restart: always调用 example.com/path 时,我收到很多404错误,因为React App是不是在寻找路径,而是在 example.com 的根目录中。 在没有PathPrefix的情况下运行时,该应用程序处于唤醒状态,并直接调用 example.com 。When calling example.com/path I get a lot of 404 Errors, as the React App is not looking for path, but in the root of example.com.The App is woking when run without PathPrefix and calling example.com directly.推荐答案您的应用不知道traefik正在添加前缀。Your app doesn't know that traefik is adding a prefix.您需要指定主页 package.json 文件中的$ c>属性,以修改将在您的应用中使用的每个相对URL。使用 npm运行脚本构建构建应用后,应该没问题。You need to specify homepage property in package.json file to modify every relative URLs that will be used in your app. After building your app using npm run-script build it should be fine.{ "homepage": "/path"} 反应文档 这篇关于React在Traefik中不适用于Path和Prefix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 14:55