我创建了两个自定义图像,一个用于
Nginx
,另一个用于基于Php-fpm
的alpine
,创建了它们的容器,但由于某种原因,Nginx
在尝试提供php文件时给了我502 Bad Gateway
,并出现以下错误:
/var/log/nginx/error.log
:
2024/04/16 15:28:29 [error] 25#25: *16 connect() failed (111: Connection refused) while connecting to upstream, client: 172.30.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.30.0.3:9000", host: "localhost:8000"
但是当我使用
php:fpm-alpine
来自 Docker Hub 的镜像时,它工作得很好,你会在下面找到一个包含 Docker Hub 镜像和我的自定义镜像的 docker compose,然后在 Nginx default.conf
文件中替换 fastcgi_pass my-php-fpm:9000;
到 fastcgi_pass php-fpm:9000;
以测试两者。
修改配置文件后不要忘记运行
nginx -s reload
或重新启动nginx容器。
我不知道问题出在哪里,所有容器共享相同的卷和相同的网络。
Nginx Dockerfile :
FROM alpine:3.19
RUN apk upgrade && apk update
RUN apk add nginx && apk add openssl
RUN mkdir /var/www/html
COPY ./default.conf /etc/nginx/http.d/
CMD nginx -g "daemon off;"
EXPOSE 80
默认.conf:
server {
listen 80;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
try_files $uri =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
my-php-Fpm Dockerfile :
FROM alpine:3.19
RUN apk upgrade && apk update
RUN apk add curl php83 php83-phar php83-fpm
RUN mkdir -p /var/www/html/
CMD php-fpm83 -F
EXPOSE 9000
docker-compose.yml:
version: "3.8"
services:
nginx:
image: my-nginx
build:
context: ./nginx/
container_name: nginx
ports:
- 8000:80
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
php-fpm:
image: php:fpm-alpine
container_name: php-fpm
ports:
- 9000
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
my-php-fpm:
image: my-php-fpm
build:
context: ./my-php-fpm/
container_name: my-php-fpm
ports:
- 9000
networks:
- net
volumes:
- ./index.php:/var/www/html/index.php
networks:
net:
index.php:
<?php echo phpinfo();?>
目录结构:
test
├── docker-compose.yml
├── index.php
├── my-php-fpm
│ └── Dockerfile
└── nginx
├── Dockerfile
└── default.conf
感谢我发现的StackOverflow答案,我可以让我的自定义
php-fpm
容器按预期工作
因为我正在使用
php-fpm82
我在这个路径中找到了它的配置
/etc/php82/php-fpm.d/www.conf
我必须搜索listen = 127.0.0.1:9000
并将其更改为0.0.0.0:9000
重新启动容器,并且 php 页面
index.php
按预期显示,但我不明白的是为什么 DockerHub 图像 php:fpm-alpine
与我的 Nginx
容器配合得很好,即使设置了 listen = 127.0.0.1:9000
它的配置。
有什么想法吗?