Cam*_*son 5 nginx docker angular
我有一个简单的 Angular 单页应用程序,我正在尝试使用 Nginx docker 映像版本 1.17.1 来提供该应用程序。
在 my 中Dockerfile
,我构建并打包 Angular,然后将生成的文件复制到 nginx 映像。
FROM node:12.6.0 AS package-stage
WORKDIR /app
COPY ./angular/package.json ./angular/package-lock.json ./
RUN npm install
COPY ./angular/. ./
RUN npm run ng build -- --configuration=production
FROM nginx:1.17.1
COPY --from=package-stage /app/dist/* /usr/share/nginx/html/
COPY ./nginx/. /
Run Code Online (Sandbox Code Playgroud)
最后COPY
一步将我的 nginx 配置复制到映像中。目前,这只是/etc/conf.d/default.conf
。我只更改了该文件中的 1 行,如下所示:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; <-- Added by me
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Run Code Online (Sandbox Code Playgroud)
该页面index.html
可通过 访问http://localhost
,但添加任何其他 url 参数(例如,http://localhost/dashboard
),会将我带到 Nginx 404 错误页面。
/usr/share/nginx/html
有关信息,以下是Docker 容器内的内容:
# cd /usr/share/nginx/html
# ls -l
total 1616
-rw-r--r-- 1 root root 27338 Jul 28 17:13 3rdpartylicenses.txt
-rw-r--r-- 1 root root 494 Jun 25 12:19 50x.html
-rw-r--r-- 1 root root 15406 Jul 28 17:12 favicon.ico
-rw-r--r-- 1 root root 1027 Jul 28 17:13 index.html
-rw-r--r-- 1 root root 637237 Jul 28 17:12 main-es2015.22ca1a6907ac396057b0.js
-rw-r--r-- 1 root root 727972 Jul 28 17:13 main-es5.26d2792d89e453a5208c.js
-rw-r--r-- 1 root root 37298 Jul 28 17:12 polyfills-es2015.9a05c5eeb2c24cb2663d.js
-rw-r--r-- 1 root root 115451 Jul 28 17:13 polyfills-es5.fdab6b769da451ba6a00.js
-rw-r--r-- 1 root root 1440 Jul 28 17:12 runtime-es2015.d5623f03f1e64ac8e12f.js
-rw-r--r-- 1 root root 1440 Jul 28 17:13 runtime-es5.a8c9c2928baa49aa82ad.js
-rw-r--r-- 1 root root 64080 Jul 28 17:12 styles.56c73d0b9f8117f2238e.css
Run Code Online (Sandbox Code Playgroud)
我确保每次都硬刷新页面,因为我知道浏览器缓存有时可能是一个问题。但仍然没有运气。
以下是 nginx 容器中的一些示例日志:
Camerons-Air:My-Site cameronhudson$ docker run -it --rm -p 80:80 gcr.io/my-site/github.com/cameronhudson8/my-site/frontend:latest
2019/07/28 17:55:50 [error] 7#7: *1 open() "/usr/share/nginx/html/dashboard" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /dashboard HTTP/1.1", host: "localhost"
172.17.0.1 - - [28/Jul/2019:17:55:50 +0000] "GET /dashboard HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" "-"
Run Code Online (Sandbox Code Playgroud)
就好像这try_files
条线被完全忽略了。
好吧,我发现 nginx完全忽略了该行try_files
...因为我将其复制default.conf
到了错误的位置的图像中。
我使用的位置:
/etc/conf.d/default.conf
Run Code Online (Sandbox Code Playgroud)
正确位置:
/etc/nginx/conf.d/default.conf
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的问题,有一个愚蠢的解决方案,但我想我会保留它,以防它对其他人有帮助。
归档时间: |
|
查看次数: |
4262 次 |
最近记录: |