首页 > 解决方案 > Angular routing in docker and nginx

问题描述

I have the following Dockerfile

FROM node:14.15.5 as build-stage
WORKDIR /webapp

COPY package*.json /webapp/
RUN npm ci

COPY ./ /webapp/
RUN npm run build:prod


# copy SPA and serve by nginx
FROM nginx:1.15
COPY --from=build-stage /webapp/dist/kubernetes-webapp/ /usr/share/nginx/html
EXPOSE 80

I have enabled routing in angular application, and when accessing it on localhost:4200/myroute it's working.

But when I run the application in a docker container and acess localhost:4200/myroute it returns 404 Not Found nginx/1.15.12

The nginx configuration is the default one coming from the nginx/1.15.12 image

How am I able to solve the problem and allow angular routing in the docker container with nginx.

标签: angulardockernginx

解决方案


据我所知,上述 docker 文件将在 ngnix 中创建一个构建和复制文件以运行您的应用程序(您构建 angular 并通过 nginx 服务器公开构建文件夹/静态文件)

  1. 您正在 ngnix 和端口 80 上运行应用程序。
  • 尝试打开 urllocalhost:80或只是 localhost 因为默认端口是 80

*当您运行 Angular 进行开发时,您使用命令 ng serve 并在端口 4200 上打开,并且在幕后的服务命令会为您提供一个服务器来运行/开发您的项目。

但是在您的这种情况下,您已经进行了构建,并且您选择了在端口 80 上运行的服务器。


推荐阅读