我正在使用 nginx 運行一個簡單的基於 PHP 的網站。最近更新了一些系統組件後,網站停止運作。當我嘗試訪問該網站時,我看到一個空白頁面,其中顯示文字「找不到文件」。伺服器日誌告訴我
FastCGI 在 stderr 中發送:從上游讀取回應標頭時“主腳本未知”
PHP 日誌包含
- - 25/Jan/2020:17:18:50 +0100 "GET /index.php" 404 - 0.151 2048 0.00%
nginx 設定如下。
# configuration file /etc/nginx/nginx.conf:
user http http;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
types_hash_max_size 2048;
types_hash_bucket_size 128;
sendfile on;
keepalive_timeout 65;
# some server blocks elided
include /home/myuser/www/com.mydomain.conf;
}
# configuration file /etc/nginx/mime.types:
types {
application/A2L a2l;
# lots of types elided so as not to exceed post size limit
}
# configuration file /etc/nginx/fastcgi.conf:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
# configuration file /home/myuser/www/com.mydomain.conf:
server {
listen 80;
server_name mydomain.com;
# enforce https
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.com;
client_max_body_size 16m;
root /home/myuser/www/com.mydomain;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri $fastcgi_script_name =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
}
}
nginx 和 php-fpm 以使用者身分執行http
,並且index.php
該使用者可以存取該檔案:
-rwxrwxr-x 1 http http 1,7K 8. Nov 10:40 /home/myuser/www/com.mydomain/index.php
我已經通過測試確定了這一點$document_root
並且$fastcgi_script_name
是正確的SCRIPT_NAME
。
我究竟做錯了什麼?
編輯:這確實看起來像是權限問題,但我還是不明白。如果我將網站的內容移至/usr/share/webapps/
進行測試,它就會起作用。不幸的是,這不是生產用途的選擇。將文件放在預期的(原始)位置後,我可以運行類似的操作sudo -u http php /home/myuser/www/com.mydomain/test.php
並獲得預期的結果。什麼可以阻止 php-fpm (或套接字)存取這些檔案?open_basedir
未設定。
答案1
除了 nginx 和 php-fpm 之間的互動中可能出現的所有問題(在本網站的多個問題中討論過)之外,systemd 還存在另一個陷阱,而這正是本例中的罪魁禍首。
php-fpm.service 單元檔案包含該ProtectHome=true
指令。我能夠透過運行systemctl edit php-fpm.service
和指定來解決這個問題
[Service]
ProtectHome=false