简体   繁体   English

在Docker Compose中将PHP-FPM与Nginx连接

[英]Connecting PHP-FPM with Nginx in Docker Compose

I'm fiddling with Docker, trying to setup a Docker composition with Nginx and PHP-FPM running on separate Alpine containers. 我不喜欢Docker,尝试通过在单独的Alpine容器上运行的Nginx和PHP-FPM设置Docker组合。 My setup is available on GitHub at https://github.com/sparkbuzz/lemp_docker , my docker-compose.yml looks as follows: 我的设置可在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"

I am able to build the images successfully, and when I visit localhost in my browser, I can see the index.html served by Nginx. 我能够成功构建映像,并且当我在浏览器中访问localhost时,可以看到Nginx提供的index.html。 However, when trying to access phpinfo.php, I get a 502 - Bad Gateway error 但是,当尝试访问phpinfo.php时,出现502- 错误的网关错误

I can docker exec -it ... /bin/ash into both the running instances, and it seems the services are running happily, however, it's clear PHP-FPM on port 9000 is never even hit. 我可以在两个正在运行的实例中都使用docker exec -it ... /bin/ash ,看来服务运行得很顺利,但是很明显,端口9000上的PHP-FPM从未被使用过。

Here's some feedback from my console: 以下是来自我的控制台的一些反馈:

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

I'm so close, but not sure why Nginx isn't happy with the PHP upstream. 我很亲近,但不确定为什么Nginx对上游的PHP不满意。 Nginx config is as follows: 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;
  }
}

How do I get Nginx talking to PHP FPM? 我如何让Nginx与PHP FPM对话?

You are listen 127.0.0.1 in your php-fpm config. 您正在php-fpm配置中收听127.0.0.1

Add this to php/Dockerfile 将此添加到php/Dockerfile

RUN sed -i 's/127.0.0.1:9000/0.0.0.0:9000/g' /etc/php7/php-fpm.d/www.conf

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

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