Docker nginx+;php回复403禁止

Docker nginx+;php回复403禁止,php,docker,nginx,Php,Docker,Nginx,我有这个问题,而我想测试我的PHP项目容器化。所以我在我的笔记本电脑上安装了docker(包括下面的版本),然后我用php信息文件创建了nginx容器,附加到/usr/share/nginx/html。但是当我检查结果时,我得到了403。我试着将php信息文件转换成HTML文件,效果很好!但是当我把它改回php信息时,我得到了403。我还尝试在容器本身上安装php和php fpm。但一切都没有改变。你们能帮帮我吗 这是我的docker版本输出 versionClient: Docker Engi

我有这个问题,而我想测试我的PHP项目容器化。所以我在我的笔记本电脑上安装了docker(包括下面的版本),然后我用php信息文件创建了nginx容器,附加到
/usr/share/nginx/html
。但是当我检查结果时,我得到了403。我试着将php信息文件转换成HTML文件,效果很好!但是当我把它改回php信息时,我得到了403。我还尝试在容器本身上安装php和php fpm。但一切都没有改变。你们能帮帮我吗

这是我的
docker版本
输出

versionClient: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:18:20 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:16:15 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
这是php信息文件,位于
/home/taufik/Documents/praktikum/docker/30-mar-2021/sosial-media/html

<?php
phpinfo();
?>
这是nginx容器的日志

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
,/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
,/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
,10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
,10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
,/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
,/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
,/docker-entrypoint.sh: Configuration complete; ready for start up
,2021/03/30 09:33:02 [error] 32#32: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "0.0.0.0:8080", referrer: "http://0.0.0.0:9000/"
,172.17.0.1 - - [30/Mar/2021:09:33:02 +0000] "GET / HTTP/1.1" 403 555 "http://0.0.0.0:9000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
,
提前感谢此错误:

2021/03/30 09:33:02 [error] 32#32: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "0.0.0.0:8080", referrer: "http://0.0.0.0:9000/"
听起来您的php文件不是
索引.php
,您试图访问
/
上的服务器,但没有文件名。然后,它会尝试向您显示目录列表(您在
/usr/share/nginx/html/
上挂载的目录中的所有内容),但默认配置不允许这样做。因此,403是错误的,因为配置禁止显示目录列表

尝试直接使用文件名访问该文件。类似于
localhost:8080/filename.php
的方法应该可以工作

然而 如果您只是使用nginx容器,那么仅此一点就不允许您执行php脚本,因为它只是nginx,没有fpm

有两种方法可以解决此问题:

  • 如果您想要一些简单的东西,只需使用
    php:apache
    图像并将php文件装载到容器中
  • php:fpm
    图像与nginx容器结合使用。稍微复杂一些,因为您还需要nginx容器的自定义配置
  • 一般来说,码头工人做事的方式™ 每个服务都有一个容器,因此有一个fpm和nginx容器。您不会将fpm安装到nginx容器中


    我建议您研究docker容器如何相互作用以及如何使其协同工作。

    谢谢kolaente,您的解决方案似乎有效。案例已结案