PHP文件正在下载,而不是在Nginx上执行

PHP文件正在下载,而不是在Nginx上执行,php,nginx,docker-compose,Php,Nginx,Docker Compose,我有一个简单的docker compose配置,使用php fpm和nginx 看起来Nginx无法将php文件传递给phpfpm,这会导致下载php文件而不是执行 它可以处理html文件(localhost:8080/readme.html) 当我转到localhost()的根目录时,总是出现403禁止的错误 请帮忙 docker-compose.yml version: '3' services: nginx: image: nginx:alpine

我有一个简单的docker compose配置,使用php fpm和nginx

看起来Nginx无法将php文件传递给phpfpm,这会导致下载php文件而不是执行

它可以处理html文件(localhost:8080/readme.html)

当我转到localhost()的根目录时,总是出现403禁止的错误

请帮忙

docker-compose.yml

version: '3'

services:

    nginx:
        image: nginx:alpine
        container_name: nginx
        restart: always
        volumes:
            - './etc/nginx/nginx.conf:/etc/nginx/nginx.conf'
            - './var/log:/var/log'
            - './web:/usr/share/nginx/html'
        ports:
            - 8080:80
            - 443:443
        depends_on:
            - php

    php:
        image: php:fpm-alpine
        container_name: php
        restart: always
        volumes:
            - "./web:/var/www/html"
            - './var/log:/var/log'
nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        root         /usr/share/nginx/html;
        index        index.php index.html index.htm;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            if (-f $request_filename/index.html) {
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename/index.php){
                rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                rewrite (.*) /index.php;
            }
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include fastcgi_params;
        }
    }
}

问题是php fpm(
/var/www/html
)和nginx(
/usr/share/nginx/html
)的根文件夹不同,但您可以在这一行中将根文件夹的名称从nginx传递给php fpm:
fastcgi\u参数脚本\文件名$document\u root$fastcgi\u脚本\名称

因此,php fpm在错误的文件夹中查找,无法执行php文件

尝试使用
/var/www/html
作为nginx的根目录(在nginx配置和docker comnpose文件中更改它),php fpm应该能够找到并执行php文件。

可能重复的