当我一起运行docker-compose,php7和nginx时,php文件被下载



当我运行以下docker-compose文件时,所有服务都运行良好,但是当我打开以下链接时,浏览器下载了一个hello-world.php文件而不是显示其内容。

http://0.0.0.0:8080/hello-world.php

这是我的文件结构:

docker-compose.yml
site.conf
www     
   hello-world.php

这是我的码头工人作曲:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./www:/usr/share/nginx/html/
      - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
      - php
  php:
    image: php:7-fpm
    volumes:
       - ./www:/usr/share/nginx/html/

这是我的网站。

server {
    index index.php index.html;
    server_name php-docker.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /usr/share/nginx/html/;
    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;
    }
}

这是我的你好世界:

<?php
    echo phpinfo();
?>

我找到了一个具有更好nginx配置文件的解决方案:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /usr/share/nginx/html;
    index index.php;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    server_tokens off;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ .php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

最新更新