Background
My site www.shuijingwanwq.com is a WordPress-based content site with about 20,000 pages, and its daily traffic has been gradually increasing. The server uses Alibaba Cloud ECS (1 core, 2GB), and the web environment uses OneinStack (Nginx + PHP-FPM + Redis + Alibaba Cloud RDS MySQL). The site had been running smoothly until one night when widespread 504 Gateway Time-out errors suddenly occurred. The backend was particularly slow and almost impossible to operate. I had actually encountered the same issue a while ago and performed some initial optimization: Troubleshooting process for my personal blog suddenly responding with 504 today
Symptoms
- Frontend pages intermittently failed to load, occasionally returning a 504 error on refresh;
- The backend
/wp-admin/admin.phploaded extremely slowly and frequently timed out; - The Alibaba Cloud console showed ECS CPU usage consistently at 100%;
- A large number of
Aborted connectionandGot an error reading communication packetsappeared in the RDS error logs.
Troubleshooting Process
1. Server Basic Status Check
After logging into the server, I ran top and found the load was as high as 7.09 (on a 1-core machine), with %us exceeding 80%. Multiple php-fpm processes were consuming 13% to 20% CPU each, redis-server was also using about 13%, and %id was 0. About 150MB of memory remained, and Swap usage was minimal. iostat showed disk %util was very low, ruling out a disk I/O bottleneck. See Figure 1.

2. PHP-FPM Configuration Analysis
Checking /usr/local/php/etc/php-fpm.conf, I found several critical issues:
- Slow log was defined twice and ultimately overridden to 0:
request_slowlog_timeoutappeared twice. The first time it was3s, and later it was set to0, causing the slow log to never take effect.
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning
emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
[www]
listen = /dev/shm/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = ondemand
pm.max_children = 5
request_slowlog_timeout = 3s
slowlog = /usr/local/php/var/log/slow.log
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0
pm.status_path = /php-fpm_status
slowlog = var/log/slow.log
rlimit_files = 51200
rlimit_core = 0
catch_workers_output = yes
;env[HOSTNAME] = iZ23wv7v5ggZ
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp- Inconsistent timeout settings: PHP-FPM’s
request_terminate_timeout = 120swas set, but Nginx did not havefastcgi_read_timeoutconfigured (default 60s). As a result, when PHP execution exceeded 60 seconds, Nginx would return a 504 first, while PHP-FPM was still executing, wasting resources.
3. Missing Nginx Configuration
The PHP handling section in the site configuration file /usr/local/nginx/conf/vhost/www.shuijingwanwq.com.conf was missing fastcgi_read_timeout, defaulting to a 60-second timeout.
4. RDS Error Log Analysis
The RDS console error logs showed a large number of:
Aborted connection ... (Got an error reading communication packets)
Got packets out of orderThis indicates that the connection between PHP and MySQL was abnormally interrupted. This is usually because PHP execution took too long and exceeded MySQL’s wait_timeout, or the PHP process terminated prematurely. See Figure 3.

5. Empty Slow Logs
Neither the PHP slow log (/usr/local/php/var/log/slow.log) nor the RDS slow query log (console slow log details) had any data. The reason was that the PHP slow log was disabled, and the RDS long_query_time defaulted to 10 seconds. Current queries might take 2 to 5 seconds but were not being recorded.
Solutions
1. Fix PHP-FPM Configuration
I adopted the ondemand mode, which is suitable for 1-core machines with limited memory and releases processes when idle. The final configuration is as follows: See Figure 4.

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning
emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
[www]
listen = /dev/shm/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 500
request_terminate_timeout = 120s
request_slowlog_timeout = 3s
slowlog = /usr/local/php/var/log/slow.log
catch_workers_output = yes
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp- Removed
pm.start_serversandpm.min/max_spare_serversthat conflicted withondemand. - Ensured the slow log was unique and effective.
2. Increase Nginx Timeout
Added the following to the PHP handling location:
fastcgi_read_timeout 150s;This makes the Nginx timeout slightly longer than PHP-FPM’s 120 seconds to prevent Nginx from terminating prematurely. See Figure 5.

3. Adjust RDS Slow Query Threshold
Logged into the RDS console → Parameter Settings → changed long_query_time from 100 to 1 second to capture more slow SQL queries. See Figure 7.

4. Restart Services
systemctl restart php-fpm
systemctl restart nginxResults
After the adjustments, website access returned to normal, CPU usage dropped back to normal levels, and backend responsiveness improved significantly. Connection interruptions in the RDS error logs were drastically reduced. Although the RDS slow log still hasn’t found any exceptionally slow SQL queries, the overall bottleneck has been essentially resolved.
Future Optimization Suggestions
Although the site has recovered, considering the data volume has reached 20,000 pages and traffic continues to grow, the 1-core 2GB configuration is still struggling. The following are optimization measures that can be taken next:
✅ 1. Hardware Upgrade (Fundamental Solution)
- Upgrade ECS configuration: At least 2 cores and 4GB, preferably 4 cores and 8GB. After the upgrade, the PHP-FPM mode can be changed to
dynamic, andpm.max_childrencan be adjusted to 10~15, significantly improving concurrent processing capabilities. - Upgrade RDS specifications: If slow queries increase or connections are frequently maxed out, consider upgrading RDS memory or enabling read-only instances.
✅ 2. Use CDN to Accelerate Static Resources
Alibaba Cloud CDN + OSS combination:
- Upload static files such as site images, CSS, and JS to OSS, and enable CDN acceleration.
- Configure a WordPress plugin (like AliOSS) to automatically sync the media library.
- Effect: Reduces ECS bandwidth pressure and speeds up access nationwide.
✅ 3. Deep Optimization of Page Caching
W3 Total Cache is already installed. It is recommended to check:
- Page cache using Disk Enhanced or Redis.
- Object cache ensuring the Redis driver is used.
- Database cache also using Redis to avoid disk I/O.
- Enable Opcode cache (like Zend OPcache).
✅ 4. Dedicated Database Optimization
- Regularly run
OPTIMIZE TABLEto optimize large tables likewp_postsandwp_postmeta. - Add indexes for commonly queried fields, such as
post_dateandmeta_key. - Use the Index WP MySQL For Speed plugin to automatically analyze and add missing indexes.
✅ 5. Monitoring and Alerting
- Install Alibaba Cloud Monitor to set threshold alerts for CPU, memory, and RDS connections.
- Write a daily scheduled script to check the PHP slow log and RDS slow log, sending an email if new slow requests are found.
✅ 6. Regular Cleanup and Maintenance
- Clean up expired drafts, trashed posts, and useless postmeta data.
- Disable or delete infrequently used plugins to reduce backend load.
- Configure WP-Cron to use the system crontab instead, avoiding checking the task queue on every visit.
Summary
The root cause of this 504 outage was a mismatch between server configuration and business scale, combined with incorrect PHP-FPM parameter settings, which led to CPU exhaustion and connection interruptions. The issue was resolved by fixing the configuration, increasing timeouts, and enabling the slow log. In the long run, upgrading hardware and introducing a CDN are necessary steps.
I hope this record provides a reference for friends encountering similar issues. If you also have experience optimizing WordPress under high load, feel free to share!

