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;
  #}
}
饮歌长啸
浏览 2713回答 1
1回答

炎炎设计

历时两天,才找到解决方案,奉献各位初学者 两处坑:一、下载php的镜像要为 php:7.2-fpm二、php服务要加一行 container_name: php-fpm
打开App,查看更多内容
随时随地看视频慕课网APP