我正在摆弄Docker,尝试设置一个Docker组合,在单独的Alpine容器上运行Nginx和PHP-FPM。我的设置可以在 GitHub 上找到 https://github.com/sparkbuzz/lemp_docker,我的 docker-compose.yml 如下所示:
version: '3'
services:
alpine_nginx:
build: ./nginx
container_name: alpine_nginx
links:
- alpine_php
ports:
- "80:80"
alpine_php:
build: ./php
container_name: alpine_php
ports:
- "9000:9000"
我能够成功构建图像,当我在浏览器中访问localhost时,我可以看到索引.html由Nginx提供。但是,当尝试访问phpinfo.php时,我收到502 - 错误的网关错误
我可以docker exec -it ... /bin/ash
到两个正在运行的实例中,似乎服务运行良好,但是,很明显端口 9000 上的 PHP-FPM 甚至从未被击中。
以下是来自我的控制台的一些反馈:
Recreating alpine_php ...
Recreating alpine_php ... done
Recreating alpine_nginx ...
Recreating alpine_nginx ... done
Attaching to alpine_php, alpine_nginx
alpine_php | [06-Nov-2017 21:46:39] NOTICE: fpm is running, pid 1
alpine_php | [06-Nov-2017 21:46:39] NOTICE: ready to handle connections
alpine_nginx | 2017/11/06 21:46:46 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.20.0.2:9000", host: "localhost"
alpine_nginx | 172.20.0.1 - - [06/Nov/2017:21:46:46 +0000] "GET /index.php HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"```
我很接近,但不确定为什么Nginx对PHP上游不满意。Nginx配置如下:
server {
listen 80;
server_name localhost;
location / {
root /var/www/localhost/htdocs/;
index index.html;
}
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass alpine_php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
如何让 Nginx 与 PHP FPM 对话?
你在php-fpm配置中127.0.0.1
听。
将此添加到php/Dockerfile
RUN sed -i 's/127.0.0.1:9000/0.0.0.0:9000/g' /etc/php7/php-fpm.d/www.conf