【问题标题】:Dockerize angular with base pathDockerize 角度与基本路径
【发布时间】:2021-11-05 22:10:50
【问题描述】:

我正在尝试使用 base-href 对我的 Angular 项目进行 dockerize,但是在它构建并复制到 Nginx 之后它无法正常工作。有人能给我一些关于我能做什么的提示吗?我可以设置 nginx.conf 的基本路径吗?

# base image
FROM node:14.15.4 as build
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli@10.0.4

# add app
COPY . .

# generate build
RUN ng build --build-optimizer --aot --base-href=/dev/

# base image
FROM nginx:1.18.0-alpine

# overwrite default nginx website
RUN rm -rf /usr/share/nginx/html/*

# copy artifact build from the 'build environment'
COPY --from=build /app/dist /usr/share/nginx/html

# expose port 80
EXPOSE 80

# run nginx
CMD ["nginx", "-g", "daemon off;"]

【问题讨论】:

    标签: node.js angular docker nginx


    【解决方案1】:

    是的,你必须更新你的 nginx.conf

    像这样更新你的 nginx 配置文件,

    location /dev/ {
       alias /usr/share/nginx/html;
       try_files $uri $uri/ /dev/index.html; 
    }
    

    【讨论】:

    • 当我构建我的角度时,我还需要定义 base-href 吗?
    • @rosepalette 是的,你需要。
    • 在 sh 进入容器并更改 nginx.conf 之后。它仍然显示:2021/09/09 09:03:57 [error] 22#22: *1 "/usr/share /nginx/html/dev/index.html”未找到(2:没有这样的文件或目录),客户端:172.18.0.1,服务器:localhost,请求:“GET /dev/ HTTP/1.1”,主机:“localhost :4201"
    • @rosepalette 更改 conf 文件后是否重新加载了 nginx
    • 我只是重新启动容器.. 这也可以吗?
    猜你喜欢
    • 2021-08-26
    • 2022-10-17
    • 2017-09-03
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    相关资源
    最近更新 更多