如何在两个具有不同卷路径的单独容器上设置php-fpm和nginx

时间:2019-01-10 12:44:25

标签: php docker nginx

我正在尝试使用docker使用2个容器(nginxphp7-fpm)来设置开发环境。

我想发生的是,当用户访问包含/api的任何URL时,它都使用php-fpm,但其他所有内容都从/var/www/html加载。

这是我的配置:

site.conf:

server {
    index index.html;
    server_name impressive.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    location /api {
        index index.php;
        alias /var/www/api;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }
}

docker-compose.yml

web:
  image: nginx
  volumes:
    - ./frontend/public:/var/www/html
    - ./site.conf:/etc/nginx/conf.d/site.conf
  links: [ php ]
  ports:
    - "8080:80"
  environment:
    - NGINX_HOST=http://impressive.local
    - NGINX_PORT=80

php:
    image: php:7-fpm
    volumes:
    - ./api:/var/www/api

这无法正常工作,当我访问impressive.local/api时,我在日志中收到以下错误:

web_1  | 2019/01/10 12:23:47 [error] 6#6: *1 "/var/www/api/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: impressive.local, request: "GET /api/ HTTP/1.1", host: "impressive.local:8080"

我意识到php-fpm容器是包含/var/www/api目录而不是nginx的容器。使用我的配置,nginx尝试alias到不存在的路径,因此失败了。

我的问题是如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

是的,我在所有Laravel应用中都使用了此配置。

这是我的配置示例...

version: '2'
services:

  app:
    container_name: app
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=x.x.x.x"
  web:
    container_name: web
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    ports:
      - 8080:80

如您所见,您指定使用Web容器中的卷。

答案 1 :(得分:0)

我认为配置文件无效;尝试使用此代码修复[docker-compose.yml和site.conf]

  

docker-compose.yml

version: '2'

services:
  web:
    image: nginx
    volumes:
      - ./frontend/public:/var/www/html
      - ./site.conf:/etc/nginx/conf.d/site.conf
    ports:
      - "8080:80"
    environment:
      - NGINX_HOST=impressive.local
      - NGINX_PORT=80
    links:
      - php

  php:
      image: php:7-fpm
      volumes:
      - ./api:/var/www/api
      - ./frontend/public:/var/www/html
  

site.conf

server {
  index index.html index.php;
  server_name impressive.local;
  error_log  /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;
  root /var/www/html;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  location /api/ {
    alias /var/www/api;
  }

  location ~  ^/api/(.+\.php)$ {
    alias /var/www/api;
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

  # pass the PHP scripts to FastCGI server listening on php:9000
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  location ~ /\.ht {
    deny all;
  }
}

最后,运行docker-compose builddocker-compose up