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



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

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

这是我的配置:

网站会议:

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到不存在的路径,因此失败了。

我的问题是怎么可能实现这一目标?

是的,我对所有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 容器中的卷。

我认为配置文件无效; 尝试此代码来修复 [ 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

网站

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

最新更新