WordPress 站點頻繁出現 502 Bad Gateway,錯誤日誌常見:
upstream sent too big header while reading response header from upstream
/usr/local/php/etc/php-fpm.conf 或 /www/wdlinux/php/etc/php-fpm.conf)pm.max_children = 30 ; 原 10,視記憶體調整
request_terminate_timeout = 60s ; 原 0,防止進程長期掛起
nginx.conf http 區塊)fastcgi_buffers 8 128k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
若日誌仍報 too big header,可嘗試:
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
server {
listen 80;
server_name *.example.com;
location / {
proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# ...
}
}
php.ini)max_execution_time = 300
max_children,注意記憶體上限。/var/log/nginx/error.log,務必以此為依據定位具體原因。| 層面 | 關鍵參數 | 建議值 |
|---|---|---|
| PHP-FPM | pm.max_children | 30~100(視 RAM) |
| PHP-FPM | request_terminate_timeout | 60s |
| Nginx FastCGI | fastcgi_buffers / buffer_size | 8 128k / 128k |
| Nginx FastCGI | fastcgi_*_timeout | 300 |
| Proxy | proxy_buffers / buffer_size | 32 32k / 64k |
| PHP | max_execution_time | 300 |
依序調整並觀察日誌,多數 502 可徹底解決。