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

时间:2019-04-23 18:37:54

标签: php nginx docker-compose

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

Nginx似乎无法将php文件传递给php-fpm,这导致下载php文件而不是执行。

它适用于html文件。(localhost:8080 / readme.html)。

当我进入本地主机(http://localhost/)的根目录时,总是出现 403 Forbidden 错误。

请帮助。

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

1 个答案:

答案 0 :(得分:1)

问题是php-fpm(/var/www/html)和nginx(/usr/share/nginx/html)的根文件夹不同,但是您在此行中将根文件夹的名称从nginx传递给php-fpm :fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

因此,php-fpm在错误的文件夹中显示并且无法执行PHP文件。

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