FastCGI:無法在 NGinx 和 PHP-FPM 上開啟主腳本

FastCGI:無法在 NGinx 和 PHP-FPM 上開啟主腳本

顯然我不是唯一一個遇到這個問題的人,但是在嘗試了針對情侶的每一個建議解決方案之後,我發現我不知道該怎麼辦。

我正在運行 Ubuntu 16.04、NGinx 和 PHP-FPM。

nginx.conf 是預設的,我只設定了一種資訊更豐富的日誌格式。

使用者權限似乎沒問題:NGinx 和 PHP_FPM 在使用者 www-data 下運作。文檔根所有者是 www-data。 FPM-Pool 所有者是 www-data。套接字檔案存在並且可寫入。

我已經嘗試將資料夾和檔案權限設為最弱(chmod 777):沒有結果。

這是我的 server.conf,正如您從註釋行中看到的那樣,我嘗試了很多技巧 - 但沒有效果:

server {
    listen         8080;
#    listen         8080 default_server;
 #   listen         [::]:8080 default_server;
    server_name    example.com www.example.com
    root           /var/www/nginx/;
    index          index.php;
        access_log /var/log/nginx/scripts.log scripts;


    gzip             on;
    gzip_comp_level  3;
    gzip_types       text/plain text/css application/javascript image/*;

    location ~ \.php$ {
        if ($uri !~ "^/uploads/") {
            fastcgi_pass unix:/run/php/php-fpm-www.sock;

        }

    include fastcgi.conf;

#    include         snippets/fastcgi-php.conf;
    include         fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;

  }

    # Block access to .htaccess
    location ~ \.htaccess {
        deny all;
    }

}    

這是 fpm-pool 配置:

[www]
listen = /run/php/php-fpm-$pool.sock
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
php_admin_flag[allow_url_fopen] = off
php_admin_flag[allow_url_include] = off
php_admin_value[memory_limit] = 128M
pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
chroot = /var/www/nginx/
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
catch_workers_output = yes

這是 nginx 的 access.log 的輸出:

/var/www/nginx/index.php > GET /index.php HTTP/1.1

這就是實際的 error.log:

2018/08/29 17:34:27 [error] 24020#24020: *47 FastCGI sent in stderr: "Unable to open primary script: /var/www/nginx/index.php (No such file or directory)" while reading response header from upstream, client: 213.61.37.18, server: example.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm-www.sock:", host: "example.com:8080"

答案1

在 @michael 的提示後,我自己發現了錯誤 - 感謝您的啟發。

我正在使用 chroot,因為目標是「監禁」每個網站環境/ 資料夾。所以我設定了這個特定的根環境到真實的檔案系統位置/var/www/nginx

在 NGinx 的伺服器設定中,我傳遞了帶有前導 $document_root 的 fastcgi 參數 SCRIPT_FILENAME。

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

在 NGinx 中 $document_root 引用根指令。 $document_root 當然是/var/www/nginx

但是 PHP-環境具有“更改的根”(/ 表示 /var/www/nginx)。這意味著 PHP 現在正在資料夾 /var/www/nginx 中尋找 index.php。但由於根資料夾只是「虛擬」的,PHP 的 /var/www/nginx 實際上指向真實檔案系統上的這個位置:/var/www/nginx/var/www/nginx。

因此將參數更改為此將修復錯誤。

fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

相關內容