2

我正在尝试使用 Docker 设置 Symfony 3.x 应用程序。我通过一个 docker-compose.yml 文件配置了 3 个 docker 容器:

  • Nginx
  • php-fpm
  • MySQL

当我导航到 my-project.dev:8080/ 时,我看到一个简单的 404-Not found-page。

  • 我无法加载 my-project.dev:8080/app_dev.php 或 my-project.dev:8080/config.php(我收到“找不到文件”错误)

我也没有在 /var/log/nginx/access.log 中看到任何条目。

码头工人-compose.yml:

web:
  image: nginx:latest
  ports:
    - "8080:80"
  volumes:
    - .:/var/www
    - ./docker/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
    - ./docker/nginx.conf:/etc/nginx/nginx.conf
  links:
    - php

php:
  image: php:5.6-fpm
  volumes:
    - .:/var/www
  links:
    - db

db:
  image: mysql:latest
  volumes:
    - /var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=my-password

nginx.conf 文件:

user  www-data;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

vhost.conf 文件:

server {
    listen *:80;

    server_name my-project.dev;
    root /var/www/web;

    location / {
       # try to serve file directly, fallback to app.php
       try_files $uri /app.php$is_args$args;
    }

    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
       fastcgi_pass php:9000;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;

       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ \.php$ {
       return 404;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}
4

3 回答 3

1

我必须通过创建这样的图像来覆盖 php-fpm 图像的 WORKDIR:

Dockerfile:

FROM php:5.6-fpm
MAINTAINER Firstname Lastname <firstname.lastname@domain.com>
WORKDIR /var/www

构建图像:

docker build -t companyx/php-5.6-fpm .

更新 docker-compose 文件:

web:
  image: nginx:latest
  ports:
    - "8080:80"
  volumes:
    - .:/var/www
    - ./docker/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
    - ./docker/nginx.conf:/etc/nginx/nginx.conf
  links:
    - php

php:
  image: companyx/php-5.6-fpm
  volumes:
    - .:/var/www
  links:
    - db

db:
  image: mysql:latest
  volumes:
    - /var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=my-password
于 2016-12-04T10:17:53.437 回答
0

我有同样的问题并尝试了不同的建议解决方案,但我的情况不同,我index.phppublic/文件夹中丢失了。

确保你在文件夹中有这个类public,你可以自己做,或者在你的根目录中创建一个 symfony 项目。

于 2020-03-22T11:43:12.853 回答
0

问题是你的定义是:

location ~ ^/(app_dev|config)\.php(/|$) {
   fastcgi_pass php:9000;
   fastcgi_split_path_info ^(.+\.php)(/.*)$;
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
   fastcgi_param DOCUMENT_ROOT $realpath_root;
}

location ~ \.php$ {
   return 404;
}

您正在使用返回 404 的 php 页面。

你应该改变 try_files $uri /app.php$is_args$args; 线路

try_files $uri /app_dev.php$is_args$args;

于 2016-12-04T17:00:41.530 回答