简体   繁体   English

Docker nginx + php 回复 403 禁止

[英]Docker nginx + php replied 403 Forbidden

I have this issue while i want to test my PHP project containerized.当我想测试我的 PHP 项目容器化时,我遇到了这个问题。 So I have installed docker on my laptop (the version included below) and then i create nginx container with php info file attach to /usr/share/nginx/html .所以我在我的笔记本电脑上安装了 docker(版本如下),然后我创建了 nginx 容器,其中 php 信息文件附加到/usr/share/nginx/html But when i checked the result, i got the 403 Forbidden.但是当我检查结果时,我得到了 403 Forbidden。 I've tried to convert the php info file into the HTML file, it works fine, But when i change it back to php info.我尝试将 php 信息文件转换为 HTML 文件,它工作正常,但是当我将它改回 php 信息时。 i got the 403 Forbidden.我得到了 403 禁止。 I've also tried to install php and php-fpm on the container itself.我还尝试在容器本身上安装 php 和 php-fpm。 But nothing change.但什么都没有改变。 Can you guys help me out.你们能帮帮我吗。

This is my docker version output这是我的docker version output

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

this is the php info file located in /home/taufik/Documents/praktikum/docker/30-mar-2021/sosial-media/html这是位于/home/taufik/Documents/praktikum/docker/30-mar-2021/sosial-media/html中的 php 信息文件

<?php
phpinfo();
?>

and this is my docker command to run the nginx container这是我的 docker 命令运行 nginx 容器

docker run -it --rm -dp 8080:80 --name=nginx -v $(pwd):/usr/share/nginx/html /home/taufik/html nginx

this is the log of the nginx container这是 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" "-"
,

Thanks in advance提前致谢

This error:这个错误:

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

sounds like your php file is not an index.php and you're trying to access the server at / , without the name of the file.听起来您的 php 文件不是index.php并且您正在尝试访问位于/的服务器,但没有文件名。 It then tries to show you the directory listing (all content in the directory you mounted at /usr/share/nginx/html/ but the default config doesn't allow that. Hence the 403 because the config forbids showing the directory listing.然后它会尝试向您显示目录列表(您安装在/usr/share/nginx/html/的目录中的所有内容,但默认配置不允许这样做。因此 403 因为配置禁止显示目录列表。

Try accessing the file with its name directly.尝试直接使用其名称访问该文件。 Something like localhost:8080/filename.php should work.localhost:8080/filename.php这样的东西应该可以工作。

However然而

If you're just using the nginx container, that alone won't let you execute php scripts as it's only nginx, no fpm whatsoever.如果您只是使用 nginx 容器,那么仅此一项就不会让您执行 php 脚本,因为它只是 nginx,没有任何 fpm。

There are two ways to fix this:有两种方法可以解决此问题:

  1. If you want something simple, just use the php:apache image and mount your php files to /var/www/html/ into the container.如果你想要一些简单的东西,只需使用php:apache图像并将 php 文件安装到/var/www/html/到容器中。 The command would look something like this: docker run -it --rm -dp 8080:80 --name=nginx -v $(pwd):/var/www/html php:apache该命令如下所示: docker run -it --rm -dp 8080:80 --name=nginx -v $(pwd):/var/www/html php:apache
  2. Use the php:fpm image in combination with an nginx container.php:fpm映像与 nginx 容器结合使用。 Slightly more complicated as you will need a custom config for the nginx container as well.稍微复杂一些,因为您还需要 nginx 容器的自定义配置。

Generally speaking, the docker way of doing things™ is to have one container for each service, hence an fpm and nginx container.一般来说,docker 的处理方式™ 是为每个服务拥有一个容器,因此是一个 fpm 和 nginx 容器。 You wouldn't install fpm into the nginx container.您不会将 fpm 安装到 nginx 容器中。

I'd reccommend you to look into how docker containers interact and can be made to work together.我建议您研究 docker 容器如何交互以及如何协同工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM