Nginx Php-fpm 7.3 无法从特定文件夹读取 PHP 文件

Nginx Php-fpm 7.3 Can't read PHP files from a particular folder

我们有一个 Magento 2 网站。出于某种原因,我们的 Nginx/PHP-FPM 无法从 MAGEROOT/pub/ 文件夹中读取除 index.php.

以外的文件

我们在 Nginx 日志 "Unable to open primary script: /home/goodprice/public_html/releases/current/pub/get.php (No such file or directory)" 中收到以下错误,浏览器显示 未指定输入文件。

这是部分 Nginx 配置文件。

# Run Magento (behind Varnish)
server {
    listen 8088;

    server_name {{website name}}.com.au www.{{website name}}.com.au m2.{{website name}}.com.au;

    set $MAGE_ROOT /home/goodprice/public_html/releases/current;

    index index.php;
    root $MAGE_ROOT/pub;
    set $code default;

    location /sitemap.xml {
        root $MAGE_ROOT/pub/media;
        autoindex off;
    }

    # Rewrites for edm
    include /etc/nginx/global/rewrites.conf;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Serve media under /pub/media/
    location /pub/ {
        location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
            deny all;
        }
        alias $MAGE_ROOT/pub/;
        add_header X-Frame-Options "SAMEORIGIN";
    }

    # Rewrite signed static files
    rewrite ^/static/(version\d*/)?(.*)$ /static/ last;

    # Static assets
    location ~ ^/static/(version\d*/)?(.*)$ {
        tcp_nodelay on;

        # Images, CSS, JS
        location ~* \.(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @static;
        }

        # Fonts
        location ~* \.(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @static;
        }

        # Catch all
        try_files $uri $uri/ @static;
    }

    # Media assets
    location /media/ {
        tcp_nodelay on;
        autoindex off;

        # Images, CSS, JS
        location ~* \.(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @media;
        }

        # Fonts
        location ~* \.(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @media;
        }

        # Catch all
        try_files $uri $uri/ @media;
    }

    # Password paths
    location /media/order_attachments {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    location /media/convert {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    # Below prescriptions dir does not contain actual prescriptions
    #location /media/prescriptions {
    #    auth_basic "Restricted";
    #    auth_basic_user_file /etc/nginx/htpasswd;
    #}
    location /media/webforms {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    location /media/raveinfosys/exporter {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }

    location @static { rewrite /static/(version\d*/)?(.*)$ /static.php?resource= last; }
    location @media { try_files $uri $uri/ /get.php$is_args$args; }

    # PHP entry point for setup application
    location ~* ^/setup($|/) {
        root $MAGE_ROOT;
        location ~ ^/setup/index.php {
            fastcgi_pass fastcgi_backend;

            fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
            fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=600";
            fastcgi_read_timeout 300s;
            fastcgi_connect_timeout 300s;

            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ ^/setup/(?!pub/). {
            deny all;
        }

        location ~ ^/setup/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }

    # PHP entry point for update application
    location ~* ^/update($|/) {
        root $MAGE_ROOT;

        location ~ ^/update/index.php {
            fastcgi_split_path_info ^(/update/index.php)(/.+)$;
            fastcgi_pass fastcgi_backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            include        fastcgi_params;
        }

        # Deny everything but index.php
        location ~ ^/update/(?!pub/). {
            deny all;
        }

        location ~ ^/update/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }

    # Main PHP
    location ~ (index|get|static|report|404|503|health_check|deploy_clear_opcache)\.php$ {
        try_files $uri =404;

        fastcgi_pass fastcgi_backend;

        fastcgi_param  PHP_FLAG      "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_read_timeout         300s;
        fastcgi_connect_timeout      300s;

        # fastcgi_param  MAGE_MODE     $MAGE_MODE;
        fastcgi_param  MAGE_RUN_CODE $code;
        fastcgi_param  MAGE_RUN_TYPE store;

        # Increase fastcgi buffer size to stop nginx errors on large posts
        fastcgi_buffers 32 256k;
        fastcgi_buffer_size 512k;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;

        fastcgi_hide_header  'X-Powered-By';
    }

    # Return 503 if the maintenance flag is found
#    if (-f $MAGE_ROOT/var/.maintenance.flag) {
#        return 503;
#    }
#
#    # Custom 503 error page
#    error_page 503 @maintenance;
#
#    location @maintenance {
#        root /home/goodprice/public_html/maintenance;
#        rewrite ^(.*)$ /503.html break;
#    }

    # Use Magento 403 404 page
    error_page 403 404 /errors/404.php;

   # Banned locations (only reached if the earlier PHP entry point regexes don't match)
    location ~* (\.php$|\.htaccess$|\.git) {
    deny all;
    }
}

这会导致一些问题。一是 Magento 2 无法提供占位符图像,因为它需要执行 get.php。这不是权限问题,因为正在执行 index.php。有人可以帮助解决上述 Nginx 配置中的问题吗?任何帮助将不胜感激。

ls -la 来自下面的发布目录

drwxr-xr-x  6 goodprice goodprice 4096 Nov 24 16:16 .
drwxr-xr-x 16 goodprice goodprice 4096 Nov 30 12:11 ..
-rw-rw-r--  1 goodprice goodprice 1038 Nov 11 01:12 cron.php
-rwxrwxr-x  1 goodprice goodprice  102 Nov 10 23:04 deploy_clear_opcache.php
drwxrwxr-x  3 goodprice goodprice 4096 Nov 11 01:12 errors
-rw-rw-r--  1 goodprice goodprice 2775 Nov 24 16:16 get.php
-rw-rw-r--  1 goodprice goodprice 3329 Nov 11 01:12 health_check.php
-rw-rw-r--  1 goodprice goodprice 6206 Nov 11 01:12 .htaccess
-rw-r--r--  1 goodprice goodprice 1360 Nov 12 11:49 index.php
-rw-rw-r--  1 goodprice goodprice  169 Jan 10  2021 info.php
drwxrwxr-x 67 goodprice goodprice 4096 Nov 29 00:01 media
drwxrwxr-x  3 goodprice goodprice 4096 Nov 11 01:12 opt
drwxr-xr-x  4 goodprice goodprice 4096 Nov 30 13:12 static
-rw-rw-r--  1 goodprice goodprice  445 Nov 11 01:12 static.php
-rw-rw-r--  1 goodprice goodprice  101 Nov 11 01:12 .user.ini

Php Fpm conf.d 文件提取用户和组。

group = "goodprice"
listen.group = "nobody"
listen.mode = 0660
listen.owner = "goodprice"
user = "goodprice"

nginx.conf如下

include /etc/nginx/conf.d/modules/*.conf;

user nobody;

worker_processes  1;
worker_rlimit_nofile 16384;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/conf.d-custom/*.conf;
}

将文件权限更改为

-rw-r-xr-x  1 goodprice goodprice 2775 Nov 24 16:16 get.php

chmod 755

顺便说一句,如果这是一个停放域,那么您的问题将有不同的转折点 因为 NginX 在多个域中的表现不如 Apache 流畅 .

这里的问题是 php-fpm 配置。我问这个问题的错误是我应该用 nginx 配置发布整个 php-fpm 配置。

在我们的服务器上,php-fpm 设置由每个站点的 cpanel 控制。问题是 php-fpm 已将 php_value[doc_root] 设置为 pub 文件夹上方的文件夹。这是因为服务器和 cpanel 配置为在 /home/goodprice/public_html/ 中包含代码。我修改为 php_value[doc_root] = "/home/goodprice/public_html/releases/current/",认为那是 Magento root,所以 php 应该在那里阅读。但实际上应该是php_value[doc_root] = "/home/goodprice/public_html/releases/current/pub/"。所以在 运行 时间 php 正在寻找一个文件,但是根(文件夹)在它正在寻找的地方是错误的。这个问题令人困惑,因为 php 错误地指出了文件的路径,而不是它实际尝试定位文件的位置。我无法解释为什么它会给出正确的路径然后只在它的根文件夹中查找文件。

所以总而言之,如果 nginx 配置中有 try_files,请确保 nginx 根目录或最终路径与 php-fpm conf 中的文件夹 php_value[doc_root] 相同。或者最好在你的 php-fpm 中根本没有 php_value[doc_root]