在 nginx 中设置上下文路径

在 nginx 中设置上下文路径

我正在尝试从上下文路径提供我的 webpack 捆绑应用程序和文件。Nginx 配置文件如下:

nginx.conf

server {

  listen 80;
  root   /usr/share/nginx/html;
  index  index.html index.htm;

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

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

Dockerfile

# Build Environment
FROM node:14.2.0-alpine as build

# Working Directory
WORKDIR /app

#Copying node package files to working directory
COPY package.* .

#Installing app dependency
RUN npm install --silent

#Copy all the code
COPY . .

#RUN Production build
RUN npm run build

# production environment
FROM nginx:stable-alpine

# Deploy the built application to Nginx server
# /usr/share/nginx/html is the Nginx static directory used to serve the appliation
COPY --from=build /app/build /usr/share/nginx/html/mfeexample

# Remove the default Nginx configuration in Nginx Container
RUN rm /etc/nginx/conf.d/default.conf

# Apply the new configuration file
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf

# Expose the Application on PORT 80
EXPOSE 80

# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]

现在查看浏览器中的网络选项卡,我发现所有文件都在 http://localhost:3000/mfeexample/ 上提供服务,这正是我想要的。但是,当我尝试嵌套路由时,它会中断,嵌套路由是指 http://localhost:3000/mfeexample/customer/,再次查看网络选项卡,nginx 正在寻找 http://localhost:3000/mfeexample/customer/index.html 下的文件,但我希望 nginx 始终从 http://localhost:3000/mfeexample 找到文件。

PS:请记住,我是 nginx 的新手,并且我一直在尝试各种尝试和错误的方法来达到这一点。

提前致谢。

相关内容