惡意腳本利用 PHP 函數(如 fsockopen、curl、file_get_contents)發起大量外部請求,佔滿服務器帶寬/連接數,導致正常業務不可用。
; 1. 禁用高危函數(根據業務精簡)
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,fsockopen,pfsockopen,stream_socket_client,stream_socket_server,pcntl_exec,ld,dl,set_time_limit,ignore_user_abort
; 2. 禁止 URL 形式 fopen(阻斷 file_get_contents/fopen 遠程讀取)
allow_url_fopen = Off
allow_url_include = Off
; 3. 限制腳本最大執行時間與內存
max_execution_time = 30
max_input_time = 30
memory_limit = 128M
; 4. 限制 POST 大小與文件上傳
post_max_size = 8M
upload_max_filesed = 2M
max_file_uploads = 20
; 5. 啟用輸出緩衝控制
output_buffering = 4096
zlib.output_compression = On
; 6. 會話安全
session.cookie_httponly = 1
session.cookie_secure = 1
session.use_strict_mode = 1
session.gc_maxlifetime = 1440
disable_functions 限制 curl_exec,改用封裝好的 HTTP Client(內部白名單域名)。# 限制單 IP 連接數與請求頻率
limit_conn_zone $binary_remote_addr zone=conn:10m;
limit_req_zone $binary_remote_addr zone=req:10m rate=30r/s;
server {
limit_conn conn 20;
limit_req zone=req burst=50 nodelay;
# 靜態資源緩存,減輕 PHP 壓力
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2?)$ {
expires 30d;
add_header Cache-Control "public";
}
# 隱藏版本號
server_tokens off;
}
單靠 php.ini 只能阻擋最基礎的腳本濫用。**完整防禦體系 = 代碼規範 + PHP 加固 + Web Server 限流 + WAF 清洗 + 雲端 DDoS 防護 + 監控告警**。定期演練壓測、滲透測試,建立應急預案。