没有不值得去解决的问题,也没有不值得去学习的技术!

A Record of Troubleshooting and Optimizing a 504 Error on a WordPress Site

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.

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.php loaded extremely slowly and frequently timed out;
  • The Alibaba Cloud console showed ECS CPU usage consistently at 100%;
  • A large number of Aborted connection and Got an error reading communication packets appeared 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.

Logged into the server, 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_timeout appeared twice. The first time it was 3s, and later it was set to 0, causing the slow log to never take effect.
Plaintext
;;;;;;;;;;;;;;;;;;;;;
; 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 = 120s was set, but Nginx did not have fastcgi_read_timeout configured (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:

Plaintext
Aborted connection ... (Got an error reading communication packets)
Got packets out of order

This 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.

This 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.

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.
Plaintext
;;;;;;;;;;;;;;;;;;;;;
; 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_servers and pm.min/max_spare_servers that conflicted with ondemand.
  • Ensured the slow log was unique and effective.

2. Increase Nginx Timeout

Added the following to the PHP handling location:

Plaintext
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.

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.

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

Bash
systemctl restart php-fpm
systemctl restart nginx

Results

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, and pm.max_children can 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 TABLE to optimize large tables like wp_posts and wp_postmeta.
  • Add indexes for commonly queried fields, such as post_date and meta_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!

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系