``try_files''不适用于Nginx Docker映像 [英] `try_files` not working with nginx docker image

查看:329
本文介绍了``try_files''不适用于Nginx Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Angular单页应用程序,正在尝试与1.17.1版的Nginx docker映像一起使用。

I have a simple Angular single-page application that I'm trying to serve with Nginx docker image, version 1.17.1.

在我的 Dockerfile ,我构建并打包Angular,然后将生成的文件复制到nginx映像。

In my Dockerfile, I build and package Angular, then copy the resulting files to the nginx image.

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/. /

最后一个 COPY 步骤会复制我的nginx配置到映像中。当前,这只是 /etc/conf.d/default.conf 。我仅更改了此文件中的1行,如下所示:

The final COPY step copies my nginx configuration into the image. Currently, this is just /etc/conf.d/default.conf. I've changed only 1 line in this file, as shown below:

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;
    }

}

页面 http:// localhost 上访问> index.html ,但添加任何其他url参数(例如, http :// localhost / dashboard ),使我进入Nginx 404错误页面。

The page index.html is accessible at http://localhost, but adding any additional url parameters (for example, http://localhost/dashboard), lands me at the Nginx 404 error page.

有关信息,以下是<$ c $的内容c> / usr / share / nginx / html 在Docker容器中:

For information, here's the contents of /usr/share/nginx/html within the Docker container:

# 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

我确保每次都要刷新页面,因为我知道浏览器缓存可能是一个问题有时。但是仍然没有运气。

I make sure to hard-refresh the page each time, because I know that browser caching can be an issue sometimes. But still no luck.

以下是来自nginx容器的一些示例日志:

Here are some example logs from the nginx container:

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" "-"

好像 try_files 行被完全忽略了。

推荐答案

好吧,我发现nginx 而忽略了 try_files 行完全...因为我将 default.conf 复制到错误位置的图像中n。

Well, I found that nginx was ignoring the try_files line completely... because I copied my default.conf into the image at the wrong location.

我使用的位置:

/etc/conf.d/default.conf

正确的位置:

/etc/nginx/conf.d/default.conf

这是一个愚蠢的解决方案的愚蠢问题,但我想我会把它保留下来,以防它对其他人有帮助。

It's a stupid problem with a stupid solution, but I guess I'll leave it up in case it helps someone else.

这篇关于``try_files''不适用于Nginx Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆