在Nginx上PHP文件是DOWNLOADING而不是EXECUTING

问题描述 投票:0回答:1

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

看起来Nginx无法将php文件传递给php-fpm,导致下载php文件而不是执行。

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

当我去localhost(http://localhost/)的root时,我总是得到403 Forbidden错误。

请帮忙。

泊坞窗,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 nginx docker-compose
1个回答
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文件。

© www.soinside.com 2019 - 2024. All rights reserved.