简体   繁体   中英

Docker-compose Nginx php-fpm file not found

I have a simple docker-compose config with php-fpm and nginx, and I can't see any php file. When I go to localhost, it shows File Not Found.

I tried everything I could find on the net, but everything I have tried has failed. It works fine for html, but not for php files. Seems to be a path issue, or something like that.

I come across this error when I docker-compose logs :

project3-php_1     | 172.17.0.5 -  29/Mar/2016:13:29:12 +0000 "GET /index.php" 404
project3-front_1   | 172.17.0.1 - - [29/Mar/2016:13:29:12 +0000] "GET / HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"
project3-front_1   | 2016/03/29 13:29:12 [error] 8#8: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "localhost"

Here's my docker-compose:

project3-front:
    image: nginx
    ports:
      - "80:80"
    links:
      - "project3-php:project3-php"
    volumes:
      - ".:/home/docker"
      - "./nginxdir/default.conf:/etc/nginx/conf.d/default.conf"
      - "./html:/usr/share/nginx/html"

  project3-php:
      build: phpdir
      volumes:
        - ".:/home/docker:rw"
        - "./html:/var/www/html"
      ports:
        - "9000:9000"
      working_dir: "/home/docker"

Then my dockerfile for php:

FROM php:5.6-fpm
EXPOSE 9000

my default.conf for nginx:

server {
  listen 80;
  server_name localhost;
  index index.php index.html;

  error_log  /var/log/nginx/error.log  warn;
  access_log /var/log/nginx/access.log;
  root /usr/share/nginx/html;

  location ~ \.php$ {
      fastcgi_pass project3-php:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

pwd of the main folder is:

/media/revenge/share/PROJECTS/docker_images/php7-nginx

The file hierarchy is:

├── docker-compose.yml
├── html
│   ├── index.php
├── nginxdir
│   ├── default.conf
├── phpdir
│   ├── dockerfile
│   └── php.ini

The whole folder is chmod 777

Any idea would be greatly appreciated. I'm sure there is something I didn't get. Thanks in advance.

Here is how you can find the problem

  1. Switch on the nginx debug mode with custom command , eg:

docker-compose.yml

web:
    image: nginx
    volumes:
        - "~/www/project:/var/www"
        - "~/www/project/vhost.conf:/etc/nginx/conf.d/site.conf"
    # Add this row:
    command: [nginx-debug, '-g', 'daemon off;']
  1. Edit your " site.conf " file (in this example it is the ~/www/project/vhost.conf file) Switch on the debug mode by the error_log (add "debug" word at the end):
error_log "/var/log/nginx/error.log" debug;
  1. (Re)start the "web" container:
docker-compose stop web
docker-compose up -d web
  1. Test the containers, are all containers running?
docker-compose ps
  1. Test in browser
  2. "Connect" and view or download ( http://blog.dcycle.com/blog/ae67284c/docker-compose-cp ) and view the /var/log/nginx/error.log file.

The problem in the most of case

You haven't set yet or you are using different directory structure in web and php-fpm . If you want to use different structure than you have to set the "fpm structure" at the fastcgi_param SCRIPT_FILENAME place, like this:

If your docker-compose.yml contains like this:

phpfpm:
    image: php:fpm
    volumes:
        - "~/www/project:/var/www/html/user/project"
#                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ the path in the php-fpm container

You have to set this in "site.conf" in nginx container:

fastcgi_param SCRIPT_FILENAME /var/www/html/user/project$fastcgi_script_name;
#                             ^^^^^^^^^^^^^^^^^^^^^^^^^^

Finally found it:

I was missing this line in the volume of PHP section of docker-compose:

"./html:/usr/share/nginx/html"

here's what the docker-compose should look like:

project3-front:
    image: nginx
    ports:
        - "80:80"
    links:
        - "project3-php:project3-php"
    volumes:
        - ".:/home/docker"
        - "./nginxdir/default.conf:/etc/nginx/conf.d/default.conf"
        - "./html:/usr/share/nginx/html"
project3-php:
    build: phpdir
    volumes:
        - ".:/home/docker:rw"
        - "./html:/var/www/html"
    ports:
        - "9000:9000"
    working_dir: "/home/docker"

The absolute root (here "/usr/share/nginx/html") in the nginx default.conf file had to be set as well in the php part of docker-compose (was only under nginx before)

That's a relief ;)

In my case the volumes: of php and nginx were pointing a the correct (and hence the same) directory. But in my nginx config there was a NGINX_SERVER_ROOT: pointing the wrong way.

So make sure you double check all volumes and root directory settings. Some are easy to overlook.

While replacing $document_root$fastcgi_script_name; by /var/www/html/user/project$fastcgi_script_name; might fix the issue, the more elegant approach in my opinion would be just changing the nginx root directory in the default.conf:

server {
  listen 80;
  server_name localhost;
  index index.php index.html;

  error_log  /var/log/nginx/error.log  warn;
  access_log /var/log/nginx/access.log;
  root /var/www/html;

  location ~ \.php$ {
      fastcgi_pass project3-php:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

And in docker-compose.yml:

project3-front:
  image: nginx
  ports:
    - "80:80"
  links:
    - "project3-php:project3-php"
  volumes:
    - ".:/home/docker"
    - "./nginxdir/default.conf:/etc/nginx/conf.d/default.conf"
    - "./html:/var/www/html"

I had the same problem but this wasn't solved by any of this answer here:

Yesterday a Docker Update hits in - which has been installed. The problem depending on this update is, that the (vEthernet (DockerNAT)) Network has been changed. In that way my firewall (in my case Kaspersky) reset my network firewall to "public" instead of "trustable".

I figured this out by open " Docker -> Settings -> Shared Devices ". On the first look, everthing seems fine. There was a "tick" on each device I need to be shared. Next I tried to disable and enable my shared devices again. By enable my shared device again the default error "A firewall is blocking file Sharing between Windows and the containers. See documetation for more info." came up. -> PERFECT!! . The shared device is not able to access the files anymore but Docker persists in the state that its all fine.

So I could fix it by:

  • Set (vEthernet (DockerNAT)) Network as trusted again in my Kaspersky "Firewall - Network" settings.
  • Restarted my docker container by using docker-compose up and everthing runs like a charm. My files could be accessed again.

One of the case might be, Your running the docker container and did composer install/update

then run following to restart container

`docker-compose down` `docker-compose up -d`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM