首页 > 解决方案 > 使用 Docker 在 nginx 中部署时,角度未找到资源

问题描述

我正在尝试使用 Docker 和 Nginx 来部署我的 Angular 应用程序,但是,我遇到了一个问题。

当我运行命令:npm run build 时,我的 index.html 有以下脚本引用:

<body>
  <script type="text/javascript" src="runtime.js"></script>
  <script type="text/javascript" src="polyfills.js"></script>
  <script type="text/javascript" src="styles.js"></script>
  <script type="text/javascript" src="vendor.js"></script>
  <script type="text/javascript" src="main.js"></script>
</body>

从我的感觉来看,这些是相对路径,因此当 nginx 启动时,我的 index.html 在 firstApp 中,然后在访问这些文件时,资源 URL 应创建为:

http://localhost/firstApp/runtime.js

但是当我在浏览器中运行应用程序时,我收到 404 错误,因为 Nginx 试图在以下位置查找资源:

http://localhost/runtime.js

有没有办法解决这个问题?

标签: angulardockernginx

解决方案


我有同样的问题,但使用 jsons。完整的解决方案如何在 docker 上部署 Angular。但是我仍然有服务 json 的问题。我像这样编辑 docker 映像和 nginx 配置:

码头工人:

# Stage 2
FROM nginx:1.15.8-alpine

COPY --from=node /usr/src/app/dist /usr/share/nginx/html

RUN rm /etc/nginx/nginx.conf /etc/nginx/mime.types
COPY nginx.conf /etc/nginx/nginx.conf
COPY mime.types /etc/nginx/mime.types

Nginx:

http {
    server {
        listen 80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
             autoindex  on;
        }
         location ~* \.(json)$ {
        }
    }
}

哑剧

我不是 nginx 的大师,所以也许有更好的解决方案。


推荐阅读