简体   繁体   English

如何在docker中使用unix套接字将nginx连接到php-fpm

[英]how to connect nginx to php-fpm using unix socket in docker

There is my docker-compose.yml有我的 docker-compose.yml

version: '2'

services:
    web:
        image: nginx:latest
        ports:
            - "8018:80"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf
            - /private/var/log/nginx:/var/log/nginx
            - /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./code:/code
            - ./php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ./www.conf:/usr/local/etc/php-fpm.d/www.conf
            - /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

And in the site.conf I write like this fastcgi_pass unix:/var/run/php7-fpm.sock;在 site.conf 中,我这样写fastcgi_pass unix:/var/run/php7-fpm.sock; I also change the listen address to listen = /var/run/php7-fpm.sock in www.conf.我也在 www.conf 中将监听地址更改为listen = /var/run/php7-fpm.sock And in my MAC, there is a file named php7-fpm.sock in folder /private/var/run with mode 666在我的 MAC 中,文件夹 /private/var/run 中有一个名为 php7-fpm.sock 的文件,模式为 666

After I ran docker-compose up -d , the containers was running success.But when I visited http://localhost:8018 , it returned 502. After I checked the nginx error log, I found out this运行docker-compose up -d ,容器运行成功。但是当我访问http://localhost:8018 时,它返回 502。在我检查 nginx 错误日志后,我发现了这一点

2017/11/01 13:08:39 [error] 6#6: *1 connect() to unix:/var/run/php7-fpm.sock failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php7-fpm.sock:", host: "localhost:8018" 2017/11/01 13:08:39 [error] 6#6: *1 connect() to unix:/var/run/php7-fpm.sock 连接到上游时失败(111:连接被拒绝),客户端:172.18 .0.1,服务器:localhost,请求:“GET/HTTP/1.1”,上游:“fastcgi://unix:/var/run/php7-fpm.sock:”,主机:“localhost:8018”

Btw, before I tried using unix socket mode.顺便说一句,在我尝试使用 unix socket 模式之前。 I succeed in visiting http://localhost:8018 with tcp/ip mode.我用tcp/ip模式成功访问了http://localhost:8018

After I check the How to set up Apache2 and PHP-FPM via unix socket? 在我检查了如何通过unix socket设置Apache2和PHP-FPM之后? , I changed my docker-compose.yml to ,我将docker-compose.yml改为

version: '2'

services:
    web:
        image: nginx:latest
        ports:
            - "8018:80"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf
            - /private/var/log/nginx:/var/log/nginx
            - "phpsocket:/var/run"
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./code:/code
            - ./php-fpm.conf:/usr/local/etc/php-fpm.conf
            - ./www.conf:/usr/local/etc/php-fpm.d/www.conf
            - ./zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
            - "phpsocket:/var/run"
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

volumes:
    phpsocket:

And override zz-docker.conf to 并覆盖zz-docker.conf

[global]
daemonize = no

[www]
listen = /var/run/php7-fpm.sock
listen.mode = 0666

Finally When I visited http://localhost:8018 , phpinfo page showed up! 最后当我访问http:// localhost:8018时 ,phpinfo页面出现了!

I think the problem is that you're trying to share volumes that contain your php7-fpm.sock 我认为问题是你正在尝试共享包含php7-fpm.sock

Remove all the shared volumes for /private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock 删除/private/var/run/php7-fpm.sock:/var/run/php7-fpm.sock所有共享卷

Instead reference the service hostname from your nginx host configuration file. 而是从nginx主机配置文件中引用服务主机名。

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass php:9000;
}

In particular, notice php:9000 refers to port 9000 on the dynamic hostname given to your php service which will be resolvable to every other container in the network. 特别是,注意php:9000是指给你的php服务的动态主机名上的端口9000,它将可以解析为网络中的每个其他容器。

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

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