PHP 未找到根据配置的 vHost 应找到的文件

PHP file not found that should be found according to configured vHost

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

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

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

docker-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;
}

我必须通过创建这样的图像来覆盖 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-编写文件:

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

问题是你的定义是这样的:

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;
}

您正在使用 php 页面 return 404。

你应该换行 try_files $uri /app.php$is_args$args;

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

我遇到了同样的问题并尝试了不同的建议解决方案,但我的情况不同,我在 public/ 文件夹中丢失了 index.php

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