Docker - index.html Nginx 文件共享无效

Docker - invalid sharing of index.html Nginx file

我的 docker-compose.yml 文件是这样的:

web:
  image: nginx:latest
  volumes:
    - /c/Users/marcin/docker/nginx-www/nginx/html/:/usr/share/nginx/html/ 
  ports:
    - "80:80"

/c/Users/marcin/docker/nginx-www/nginx/html/ 中,我创建了 index.html 包含以下内容的文件:

<html>
<head>
</head>
<body>
hello index
</body>
</html>

但是当我查看我的域时,我看到的是空白页面,但是查看页面源代码时,我看到的是这样的内容:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

似乎 Docker 正在使用默认的 Nginx 文件并且只使用它的一部分(可能是它在我的 index.html 文件中的确切长度。与其他文件例如 abc.html 没有这样的问题(可能是因为它在 Nginx 图像中默认不存在)。我怎样才能解决这个问题以显示 index.html 文件的正确内容?

由于您的卷以 /c/... 开头,我假设您正在使用 Windows 上的 Docker 工具箱和随附的 docker-machine 工具。您遇到的是 Nginx 的 known issue(或者 Nginx 使用的 sendfile Linux 系统调用)与 Virtualbox 共享文件夹的结合。

Vagrant documentation(也使用 VirtualBox)中也提到了这个问题,它还附带了一个可能适合您的建议解决方案:

There is a VirtualBox bug related to sendfile which can result in corrupted or non-updating files. You should deactivate sendfile in any web servers you may be running.

In Nginx:

sendfile off;

In Apache:

EnableSendfile Off

根据 bug report in the vendor bug tracker 中的最新评论,如果您正在使用 open_file_cache 指令(默认情况下禁用),您也需要将其关闭:

open_file_cache off;