angular - 使用 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
有没有办法解决这个问题?
解决方案
我有同样的问题,但使用 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 的大师,所以也许有更好的解决方案。
推荐阅读
- bash - 如何检查命令在 bash 脚本中是否有效?
- c# - 通过使用第一维将所有二维数组值传递给另一个数组
- string - laravel 命名路由助手不会生成带有参数的 url。
- azure - azure search 的限制存储是 2.4 TB?
- reactjs - 缩放问题导致 react-native 应用程序的外观不一致
- java - 使用选项运行 Maven exec-maven-plugin,包括具有提供范围的依赖项,例如 Intellij
- r - 从 2 个长度不等的列表中获取共同元素
- mongodb - 无法在 Windows 32 位操作系统中安装 MongoDB
- c# - 每个孩子运行一个单独的任务
- wordpress - 将 .php 扩展名添加到 wordpress url