简体   繁体   English

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

[英]PHP files are DOWNLOADING instead of EXECUTING on Nginx

I have a simple docker-compose config with php-fpm and nginx. 我有一个简单的docker-compose配置,带有php-fpm和nginx。

It looks like Nginx can't pass the php file to php-fpm which results in download of php files instead of execution. 看来Nginx无法将php文件传递给php-fpm,这导致下载php文件而不是执行文件。

It works with html files.(localhost:8080/readme.html). 它可以处理html文件。(localhost:8080 / readme.html)。

I always get a 403 Forbidden error when I go to root of localhost( http://localhost/ ). 当我转到localhost( http:// localhost / )的根目录时,总是收到403 Forbidden错误。

Please help. 请帮忙。

docker-compose.yml 泊坞窗,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 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;
        }
    }
}

The problem is that the root folder for php-fpm ( /var/www/html ) and nginx ( /usr/share/nginx/html ) are different but you pass the name of the root folder from nginx to php-fpm in this line: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 问题是php-fpm( /var/www/html )和nginx( /usr/share/nginx/html )的根文件夹不同,但是您在此将nginx的根文件夹名称传递给php-fpm行: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Because of that, php-fpm looks in the wrong folder and can't execute the PHP file. 因此,php-fpm查找错误的文件夹中并且无法执行PHP文件。

Try using /var/www/html as the root for nginx (change it in the nginx config and the docker-comnpose file) and php-fpm should be able to find and execute the PHP files. 尝试使用/var/www/html作为nginx的根目录(在nginx配置和docker-comnpose文件中对其进行更改),并且php-fpm应该能够找到并执行PHP文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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