【发布时间】:2021-07-18 19:20:38
【问题描述】:
亲爱的堆栈溢出,
我正在尝试配置一个 Nginx 反向代理,以基于同一域下每个的基本 URL 为两个 react 应用程序提供服务,您将在下面找到项目中使用的当前配置。
当前的情况是,当浏览到 ADMIN URL (/admin/) 时,会为浏览器完成的每个请求加载 index.html 文件,因此所有资产都像 @987654328 一样加载@,所以我的假设是缺少的配置在 nginx.conf 文件之一中?
项目结构
- NGINX(代理)
- NGINX - React APP (/)
- NGINX - React APP 管理员 (/admin)
配置
Docker 编写
version: "3.7"
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- 8000:80
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
frontend:
container_name: frontend
build:
context: ../frontend
dockerfile: Dockerfile.prod
frontend-admin:
container_name: frontend-admin
build:
context: ../frontend-admin
dockerfile: Dockerfile.prod
nginx.conf
http {
server {
listen 80;
location /admin/ {
proxy_pass http://frontend-admin:80;
}
location / {
proxy_pass http://frontend:80;
}
}
}
REACT 应用程序
两个项目都使用了以下文件,我尝试将管理项目中的“location /”更改为“location /admin/”,但没有任何成功。
Dockerfile
# build environment
FROM node:14.16.1-alpine as build
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
COPY . ./
RUN yarn build
# production environment
FROM nginx:stable-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY --from=build /usr/src/app/nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
反应应用
- 最终 URL 应该是:http://myapp.com/
- 基本路径:
vite.config.js
// ...
export default defineConfig(({ mode }) => ({
base: '/',
// ...
}));
React APP 管理员
- 最终的 URL 应该是:http://myapp.com/admin/
- 基本路径:
vite.config.js
// ...
export default defineConfig(({ mode }) => ({
base: '/admin/',
// ...
}));
【问题讨论】: