docker-compose中nginx无法连接到 php提示not found in upstream php-fpm ?

环境:centos7+docker17.12+docker-compose1.8.0
yml文件

mysql5.7+php7.2+nginx1.13 均是官方下载的镜像,版本为latest

version: "3"
services:
    php-fpm:
      image: php:latest
      restart: always
      links:
        - mysqldb:mysqldb
      volumes:
        - "./src:/var/www/html"
      expose:
        - 9000

    nginx:
      image: nginx:latest
      restart: always
      depends_on:
        - php-fpm
        - mysqldb
      links:
        - php-fpm
      volumes:
        - "./src:/usr/share/nginx/html"
        - "./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
      ports:
          - "8888:80"

    mysqldb:
      image: mysql:latest
      restart: always
      volumes:
        - "./data:/var/lib/mysql"
      ports:
        - "3306:3306"
      environment:
        MYSQL_USER: root
        MYSQL_PASSWORD: 123456
        MYSQL_ROOT_PASSWORD: 123456

nginx配置如下default.conf

server {
  listen    80;
  server_name localhost;
  
  #charset koi8-r;
  
  #access_log logs/host.access.log main;
  
  location / {
    root  html;
    index index.html index.php;
  }
  
  #error_page 404       /404.html;
  
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  html;
  }
  
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  
  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    fastcgi_pass php-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
  }
  
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
阅读 12.1k
2 个回答

历时两天,才找到解决方案,奉献各位初学者

两处坑:
一、下载php的镜像要为 php:7.2-fpm
二、php服务要加一行 container_name: php-fpm

关键点有2个:
一个是docker-compose.yml的container_name,比如这里叫php-fpm设定。
如果直接使用下面这个,会报错。

fastcgi_pass php-fpm:9000; #如果使用这个,会报错:nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/conf.d/default.conf:20

另一个是nginx.conf中upsteam的设定。

需要改成

upstream phpupstream {
   server php-fpm:9000 fail_timeout=5s max_fails=5;
}

......
server{
    ....
    fastcgi_pass phpupstream;
    ....
}

网上有很多提到使用upstream,但是没有用server 打头,所以依然会提示找不到php-fpm。


下面我提供一份完整的docker-compose.yml,如果你是想用于部署laravel项目,那就正好,还有debug的command呢。用不到就删掉。这份原本是基于dockerfile的,我已经注释掉build了,改用image,方便大家测试。

version: "3"
services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./logs/nginx:/var/log/nginx
    depends_on:
      - php
    networks:
      - laravel
    links:
      - php

  php:
    # build: ./Dockerfile
    image: php:7.2-fpm
    container_name: php-fpm
    volumes:
        - ./src:/var/www
        - ./php:/usr/local/etc/php
    working_dir: /var/www
    # command: "php artisan serve --host=0.0.0.0 --port=8000"
    ports:
      # - "8000:8000"
      - "9000:9000"
    networks:
      - laravel
    expose:
      - 9000
networks:
    laravel:
      external: true

对应的nginx配置文件:

upstream phpupstream {
   server php-fpm:9000 fail_timeout=5s max_fails=5;
}

server{
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # fastcgi_pass php-fpm:9000; #如果使用这个,会报错:nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/conf.d/default.conf:20
        fastcgi_pass phpupstream;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题