docker容器连接问题:nginx 和 php-fpm --link后并无法正常解析php程序,求解。

1、我是这样建立容器的

php-fpm 容器

docker run -itd --name php-fpm -v /Users/nibuw/Documents/devops/data/wwwlogs/php-fpm.log:/usr/local/php/var/log/php-fpm.log php:7.0.9

运行状态:

[root@4469e557bf9b src]# ps uax
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.9 161684 19312 ?        Ss+  10:33   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www          5  0.0  0.4 161684  8888 ?        S+   10:33   0:00 php-fpm: pool www
www          6  0.0  0.4 161684  8892 ?        S+   10:33   0:00 php-fpm: pool www
root         7  0.6  0.1  11784  2944 ?        Ss   10:49   0:00 /bin/bash
root        19  0.0  0.1  47432  3392 ?        R+   10:49   0:00 ps uax

nginx 容器

docker run -itd --name nginx_server -p 80:80 -p 443:443 --link php-fpm:php-fpm -v /Users/nibuw/Documents/devops/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf -v /Users/nibuw/Documents/devops/data/:/data nginx:1.11.3

运行状态:

[root@b23d2b95cf6e /]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.4  27236  8580 ?        Ss+  10:33   0:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
www          5  0.0  1.5  51812 31316 ?        S+   10:33   0:00 nginx: worker process
www          6  0.0  1.5  51812 31316 ?        S+   10:33   0:00 nginx: worker process
root         7  0.7  0.1  11784  2796 ?        Ss   10:48   0:00 /bin/bash
root        20  0.0  0.1  47176  3324 ?        R+   10:48   0:00 ps aux

然而:
直接访问转发后的本地IP

➜  ~ curl -I http://10.8.150.44
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 02 Aug 2016 10:52:02 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Tue, 02 Aug 2016 05:53:26 GMT
Connection: keep-alive
ETag: "57a03556-5"
Accept-Ranges: bytes

访问PHP就报错了

➜  ~ curl -I http://10.8.150.44/index.php
HTTP/1.1 502 Bad Gateway
Server: nginx
Date: Tue, 02 Aug 2016 10:52:26 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive

查看日志后错误代码

2016/08/02 10:42:43 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"
2016/08/02 10:42:43 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"
2016/08/02 10:42:45 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"
2016/08/02 10:42:45 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"
2016/08/02 10:42:45 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"
2016/08/02 10:42:45 [error] 5#0: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: _, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://10.37.129.2:9000", host: "10.211.55.2"

各位大神,求解到底哪里出错了,折磨了好几天啦。

阅读 12.1k
6 个回答

我也遇到了这个问题。通过curl 9000端口,发现访问不通,才发现listen配置的有问题。

修改php-fpm所在容器的php/etc/php-fpm.d/www.conf文件:
;listen = 127.0.0.1:9000
listen = 0.0.0.0:9000

看看nginx配置文件里fastcgi 指定的IP.

配置文件的问题

如果自定义端口,需要修改zz-docker.conf文件里面的listen=[::]:3001举个例子

刚好今天遇到类似的问题,容器互相访问是正常的,Nginx 访问静态页面也是正常,php-fpm 启动也是正常的,改什么 php-fpm 配置都没有用,感觉很是诡异。
最后发现是挂载的 php-fpm 配置文件跟容器内使用的不是同一个,但奇怪为什么能正常启动且没有报错,改成同一个之后就正常了,希望能帮到你

刚好今天遇到一模一样的问题,容器互相访问是正常的,Nginx 访问静态页面也是正常,php 启动也是正常的,改什么 php 配置都没有用,docker真tnn的诡异。气氛,看来还是放弃docker

推荐问题
宣传栏