上傳約 300KB 圖片時出現 HTTP 500,Apache 錯誤日誌顯示:
mod_fcgid: HTTP request length 132296 (so far) exceeds MaxRequestLen (131072)
原因:mod_fcgid 預設 MaxRequestLen = 131072 位元組(128KB),超過即拒絕請求。
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
FcgidConnectTimeout 20
# 調整為 15MB(15728640 位元組)
FcgidMaxRequestLen 15728640
</IfModule>
<VirtualHost *:80>
ServerName example.com
<IfModule mod_fcgid.c>
FcgidMaxRequestLen 15728640
</IfModule>
</VirtualHost>
<IfModule mod_fcgid.c>
FcgidMaxRequestLen 15728640
</IfModule>
除 FastCGI 限制外,PHP 本身上傳相關設定也需同步調整(php.ini):
; 檔案上傳最大值
upload_max_filesize = 20M
; POST 資料最大值(需 ≥ upload_max_filesize)
post_max_size = 25M
; 腳本最大執行時間(大檔上傳需較長)
max_execution_time = 300
; 腳本解析輸入資料最大時間
max_input_time = 300
; 記憶體限制
memory_limit = 256M
修改後重啟 Apache 生效。
# nginx.conf
http {
client_max_body_size 20M; # 客戶端請求體大小限制
# ...
}
# php-fpm.conf / www.conf
; 請求體緩衝區大小
request_body_buffer_size = 16k;
error_log、PHP error_log、Nginx error.log$_FILES['file']['error'] 為 UPLOAD_ERR_OK (0)php_flag engine off 禁止腳本執行。mime_content_type() + 檔案簽名(Magic Bytes)雙重檢查。FastCGI 模式大檔上傳 500 錯誤多因 FcgidMaxRequestLen 預設值過小。同步調整 PHP upload_max_filesize/post_max_size 及 Web Server 對應參數,並落實安全防護,即可穩定支援大檔上傳。