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

How I Troubleshot and Resolved a Sudden 504 Error on My Personal Blog Today

My personal blog suddenly returning a 504 response today

1. Troubleshooting and resolution process for my personal blog suddenly returning a 504 response today. See Figure 1.

My personal blog suddenly returning a 504 response today

2. A 504 Gateway Timeout means that Nginx (or another reverse proxy) timed out while waiting for a response from the backend (PHP-FPM). I investigated in the following order:

3. I ran the top command, and the results were as follows: the problem was clear—the server was severely overloaded. Key metrics: load average 19.77 (normally should be under 1-2), 21 running processes (far too many), CPU usage at 87.4%, and only 96MB of memory available out of 1968MB. This is a small machine with 2GB of RAM, and it had been overwhelmed. See Figure 2.

Ran the top command, and the results were as follows: the problem was clear—the server was severely overloaded. Key metrics: load average 19.77 (normally should be under 1-2), 21 running processes (far too many), CPU usage at 87.4%, and only 96MB of memory available out of 1968MB. This is a small machine with 2GB of RAM, and it had been overwhelmed.
Plaintext

top - 15:21:01 up 993 days,  3:25,  1 user,  load average: 18.68, 19.55, 19.55
Tasks: 139 total,  20 running, 119 sleeping,   0 stopped,   0 zombie
%Cpu(s): 88.0 us,  9.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  1.3 hi,  1.7 si,  0.0 st
MiB Mem :   1968.6 total,     76.8 free,   1481.2 used,    410.6 buff/cache
MiB Swap:   2048.0 total,   1816.6 free,    231.4 used.    262.7 avail Mem

3. I exited top and ran: ps aux –sort=-%cpu | head -20, to see which processes were consuming resources. The cause was found. Over 15 PHP-FPM child processes were running simultaneously, each consuming 110MB of memory. PHP-FPM alone was consuming nearly 1.6GB. With only 2GB of total memory on the server, it was completely maxed out.

Plaintext

[root@iZ23wv7v5ggZ ~]# ps aux --sort=-%cpu | head -20
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www      2097069  5.6  5.4 583184 109280 ?       S    14:41   2:29 php-fpm: pool www
www      2097086  5.6  5.3 583376 108720 ?       R    14:43   2:23 php-fpm: pool www
www      2097087  5.6  5.5 585100 111712 ?       R    14:43   2:22 php-fpm: pool www
www      2097088  5.6  5.4 583136 109108 ?       S    14:43   2:22 php-fpm: pool www
www      2097093  5.6  5.4 583132 109292 ?       R    14:44   2:18 php-fpm: pool www
www      2097094  5.6  5.4 583132 109344 ?       R    14:44   2:18 php-fpm: pool www
www      2097109  5.6  5.4 583372 109752 ?       R    14:45   2:16 php-fpm: pool www
www      2097125  5.6  5.4 583116 109200 ?       R    14:47   2:09 php-fpm: pool www
www      2097162  5.6  5.4 583324 109732 ?       R    14:49   2:01 php-fpm: pool www
www      2097187  5.6  5.4 583244 109848 ?       R    14:53   1:48 php-fpm: pool www
www      2097281  5.6  5.4 583124 109308 ?       R    15:03   1:17 php-fpm: pool www
www      2097299  5.6  5.4 583052 109328 ?       R    15:05   1:09 php-fpm: pool www
www      2097341  5.6  5.4 583116 109408 ?       S    15:10   0:52 php-fpm: pool www
www      2097671  5.6  5.5 583052 112304 ?       R    15:18   0:24 php-fpm: pool www
www      2097746  5.5  5.5 582836 111876 ?       S    15:24   0:02 php-fpm: pool www
www      2097747  5.4  5.5 582852 111988 ?       R    15:25   0:01 php-fpm: pool www
root     1007745  3.2  1.0 154220 20344 ?        Ssl   2025 7458:44 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDunMonitor
root     2097357  0.5  7.7 516416 156836 ?       RNs  15:11   0:04 /usr/libexec/platform-python /usr/bin/dnf makecache --timer
root     1007719  0.3  0.3  99308  6048 ?        Ssl   2025 756:25 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDun
[root@iZ23wv7v5ggZ ~]#

4. I performed an emergency recovery first, then investigated the root cause. I checked the total number of PHP-FPM processes. There were 17 in total.

Plaintext

[root@iZ23wv7v5ggZ ~]# ps aux | grep php-fpm | grep -v grep | wc -l
17

5. I edited the PHP-FPM configuration. Since the server only has 2GB of memory, I changed it to run 5 child processes by editing the file /usr/local/php/etc/php-fpm.conf. See Figure 3.

Edited the PHP-FPM configuration. The server only has 2GB of memory, so changed it to run 5 child processes by editing the file /usr/local/php/etc/php-fpm.conf

Before modification:

Plaintext

pm = dynamic
pm.max_children = 16
pm.start_servers = 10
pm.min_spare_servers = 8
pm.max_spare_servers = 16
pm.max_requests = 2048

After modification:

Plaintext

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

6. After saving, I restarted it by running the following command: See Figure 4.

Restarted after saving: executed the command:
Plaintext

[root@iZ23wv7v5ggZ ~]# service php-fpm restart
[root@iZ23wv7v5ggZ ~]# sudo systemctl restart php-fpm

7. I ran the top command, and the results were as follows. Memory recovered, increasing from 96MB free to 1214MB free, so the PHP-FPM adjustment took effect. The load was also dropping (19.77 → 7.97). However, CPU usage was still at 85.4% with 8 running processes, which was abnormal.

Plaintext

top - 15:55:11 up 993 days,  3:59,  1 user,  load average: 7.97, 11.01, 15.32
Tasks: 124 total,   8 running, 116 sleeping,   0 stopped,   0 zombie
%Cpu(s): 85.4 us,  9.9 sy,  0.0 ni,  1.0 id,  0.0 wa,  1.0 hi,  2.6 si,  0.0 st
MiB Mem :   1968.6 total,   1214.1 free,    302.7 used,    451.8 buff/cache
MiB Swap:   2048.0 total,   1878.8 free,    169.2 used.   1469.1 avail Mem

8. I checked what was consuming the CPU by running the command: ps aux –sort=-%cpu | head -10. All 5 PHP-FPM processes were maxed out, indicating a continuous heavy stream of requests hitting my server.

Plaintext

[root@iZ23wv7v5ggZ ~]# ps aux --sort=-%cpu | head -10
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www      2098124 17.0  3.1 452724 63280 ?        R    15:57   0:06 php-fpm: pool www
www      2098123 16.8  2.9 452404 59716 ?        R    15:57   0:06 php-fpm: pool www
www      2098122 16.7  3.0 452868 61800 ?        R    15:57   0:06 php-fpm: pool www
www      2098125 16.7  2.9 452404 59712 ?        R    15:57   0:06 php-fpm: pool www
www      2098126 16.6  3.0 452732 60772 ?        R    15:57   0:06 php-fpm: pool www
root     1007745  3.2  1.0 154220 20160 ?        Ssl   2025 7460:16 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDunMonitor
root     1007719  0.3  0.2  99308  5772 ?        Ssl   2025 756:31 /usr/local/aegis/aegis_client/aegis_12_81/AliYunDun
www      2040873  0.2  1.4 186640 29872 ?        R    Feb17  24:56 nginx: worker process
root         754  0.1  0.2 555732  4480 ?        Ssl   2023 2189:58 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
[root@iZ23wv7v5ggZ ~]#

9. I checked the access logs for my blog’s virtual host. The access volume from individual IPs to the blog was low, but the IP ranges were highly concentrated (222.167.251.x, 143.20.219.x, 66.92.14.x), resembling a distributed crawler. However, with only 2 requests per IP, it was unlikely they were the ones crashing the server.

Plaintext

[root@iZ23wv7v5ggZ ~]# tail -200 /data/wwwlogs/www.shuijingwanwq.com_nginx.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
2 66.92.14.197
2 222.167.251.94
2 222.167.251.83
2 222.167.251.69
2 222.167.251.248
2 222.167.251.161
2 222.167.251.155
2 222.167.251.100
2 143.20.219.38
2 143.20.219.227
2 143.20.219.208
2 143.20.219.199
2 136.0.94.36
2 13.53.45.144
1 66.92.14.92
1 66.92.14.90
1 66.92.14.85
1 66.92.14.8
1 66.92.14.77
1 66.92.14.75

10. The traffic might be coming from other virtual hosts. I checked which log file had been written to most frequently recently. The main traffic was concentrated on my blog (21MB log) and access_nginx.log (7MB).

Plaintext
<h1>Check which log file has been written to most frequently recently</h1>
ls -lt /data/wwwlogs/*.log | head -10

-rw-r--r-- 1 www root 21765969 Feb 25 16:01 /data/wwwlogs/www.shuijingwanwq.com_nginx.log
-rw-r--r-- 1 www root  7330312 Feb 25 16:01 /data/wwwlogs/access_nginx.log
-rw-r--r-- 1 www root     2189 Feb 25 15:47 /data/wwwlogs/error_nginx.log
-rw-r--r-- 1 www root    48532 Feb 25 15:40 /data/wwwlogs/learn-php-app-0605-prod.wangqiang.store_nginx.log
-rw-r--r-- 1 www root   213953 Feb 25 11:03 /data/wwwlogs/tym-jammerall.shuijingwanwq.com_nginx.log
-rw-r--r-- 1 www root        0 Jun 16  2023 /data/wwwlogs/learn-php-app-0605-prod.shuijingwanwq.com_nginx.log
-rw-r--r-- 1 www root        0 Jun  8  2023 /data/wwwlogs/fanxiapp-wangqiang-larabbs.shuijingwanwq.com_nginx.log


11. I checked the traffic source for access_nginx.log and found it! The IP 183.129.189.60 was aggressively hitting my server, accounting for 177 out of 200 log entries. I blocked this IP first:

Plaintext

[root@iZ23wv7v5ggZ ~]# tail -200 /data/wwwlogs/access_nginx.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
177 183.129.189.60
10 121.196.223.20
2 77.83.39.167
2 45.153.34.187
2 38.46.221.123
2 204.76.203.69
1 47.99.50.249
1 43.156.202.34
1 43.130.31.17
1 43.130.16.212
1 205.210.31.11

12. I added a rule in the security group on the Alibaba Cloud console: Log in to the Alibaba Cloud console → ECS → Security Groups → find the security group for my instance → Add Rule → set Direction to “Inbound”, Policy to “Deny”, Source to 183.129.189.60, and Port Range to -1/-1 (all ports). See Figure 5.

Added a rule in the security group on the Alibaba Cloud console: Log in to the Alibaba Cloud console → ECS → Security Groups → find the security group for my instance → Add Rule → set Direction to "Inbound", Policy to "Deny", Source to 183.129.189.60, and Port Range to -1/-1 (all ports)

13. I checked who was still making a large number of requests. 183.129.189.60 was still hitting the server! The security group rule might not have taken effect. I immediately blocked it using iptables. See Figure 6.

Checked who was still making a large number of requests. 183.129.189.60 was still hitting the server! The security group rule might not have taken effect. Immediately blocked it using iptables.
Plaintext

[root@iZ23wv7v5ggZ ~]# tail -500 /data/wwwlogs/access_nginx.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
473 183.129.189.60
10 121.196.223.20
3 81.29.142.6
2 77.83.39.167
2 45.153.34.187
2 38.46.221.123
2 204.76.203.69
1 47.99.50.249
1 43.156.202.34
1 43.130.31.17
[root@iZ23wv7v5ggZ ~]#

14. I ran the following command, which returned 0, indicating the block was successful.

Plaintext

[root@iZ23wv7v5ggZ ~]# sleep 30 && tail -100 /data/wwwlogs/access_nginx.log | grep "183.129.189.60" | wc -l
73

15. Since iptables was not working, I blocked it at the Nginx level. I blocked it directly in the virtual host configuration corresponding to access_nginx.log. First, I found which virtual host it was. Then I edited nginx.conf directly, saved it, and restarted Nginx. See Figure 7.

Since iptables was not working, blocked it at the Nginx level. Blocked it directly in the virtual host configuration corresponding to access_nginx.log. First, found which virtual host it was. Then edited nginx.conf directly, saved it, and restarted Nginx.
Plaintext

[root@iZ23wv7v5ggZ ~]# grep -rl "access_nginx.log" /usr/local/nginx/conf/
/usr/local/nginx/conf/nginx.conf
[root@iZ23wv7v5ggZ ~]# vi /usr/local/nginx/conf/nginx.conf
[root@iZ23wv7v5ggZ ~]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[root@iZ23wv7v5ggZ ~]#

16. I monitored it in real time, waiting 30 seconds to see if there were any new entries. If there was no output, it meant the block had taken effect. I confirmed it was effective. However, the load was still high, and the blog was still returning a 504 response.

Plaintext

[root@iZ23wv7v5ggZ ~]# timeout 30 tail -f /data/wwwlogs/access_nginx.log | grep "183.129.189.60"


17. The load stabilized at 7.6 and did not drop further. Something else was still consuming resources. I checked the current situation:

Plaintext
<h1>Current PHP-FPM process status</h1>
[root@iZ23wv7v5ggZ ~]# ps aux | grep "php-fpm" | grep -v grep | wc -l
6
<h1>Check for new requests from that IP in real time</h1>
[root@iZ23wv7v5ggZ ~]# tail -20 /data/wwwlogs/access_nginx.log | awk '{print $4, $1}'
[25/Feb/2026:15:26:57 121.196.223.20
[25/Feb/2026:15:26:57 121.196.223.20
[25/Feb/2026:15:26:57 121.196.223.20
[25/Feb/2026:15:26:57 121.196.223.20
[25/Feb/2026:15:27:07 121.196.223.20
[25/Feb/2026:15:27:07 121.196.223.20
[25/Feb/2026:15:27:17 121.196.223.20
[25/Feb/2026:15:27:17 121.196.223.20
[25/Feb/2026:15:28:33 47.99.50.249
[25/Feb/2026:15:34:01 45.153.34.187
[25/Feb/2026:15:49:35 43.156.202.34
[25/Feb/2026:15:56:04 38.46.221.123
[25/Feb/2026:16:01:12 38.46.221.123
[25/Feb/2026:16:12:08 81.29.142.6
[25/Feb/2026:16:12:12 81.29.142.6
[25/Feb/2026:16:12:16 81.29.142.6
[25/Feb/2026:16:20:52 185.242.226.113
[25/Feb/2026:16:45:12 34.158.168.101
[25/Feb/2026:16:45:13 34.158.168.101
[25/Feb/2026:16:45:14 34.158.168.101
<h1>Recent requests to the blog</h1>
[root@iZ23wv7v5ggZ ~]# tail -20 /data/wwwlogs/www.shuijingwanwq.com_nginx.log | awk '{print $4, $1, $7}' | tail -10
[25/Feb/2026:16:56:34 143.20.219.229 /robots.txt
[25/Feb/2026:16:56:34 143.20.219.183 /tag/develop
[25/Feb/2026:16:56:34 222.167.251.252 /robots.txt
[25/Feb/2026:16:56:34 66.92.14.190 /robots.txt
[25/Feb/2026:16:56:34 222.167.251.15 /robots.txt
[25/Feb/2026:16:56:34 66.92.14.195 /robots.txt
[25/Feb/2026:16:56:34 136.0.94.155 /robots.txt
[25/Feb/2026:16:56:34 66.92.14.151 /robots.txt
[25/Feb/2026:16:56:35 143.20.219.77 /robots.txt
[25/Feb/2026:16:56:35 136.0.94.158 /robots.txt
<h1>How many cores does my server have</h1>
[root@iZ23wv7v5ggZ ~]# nproc
1
[root@iZ23wv7v5ggZ ~]#

18. I found the root cause: a single-core CPU combined with a massive volume of distributed crawler traffic. My blog was being hit simultaneously by over a dozen different IPs within the same second. These IP ranges (143.20.219.x, 222.167.251.x, 66.92.14.x, 136.0.94.x) belong to distributed crawlers. A single-core machine simply could not handle it. I blocked these crawler IP ranges in bulk in Nginx:

Plaintext

deny 183.129.189.60;
deny 143.20.219.0/24;
deny 222.167.251.0/24;
deny 66.92.14.0/24;
deny 136.0.94.0/24;

19. Finally, I restarted the ECS instance and observed the situation over the next few days to see if the 504 would reappear. The issue was resolved. Here is a summary of what I did:

Reduced PHP-FPM max_children from 16 to 5 (to fit a single-core 2GB setup)
Blocked 5 malicious IP ranges in Nginx
Restarted the ECS instance to clear the backlog of stuck processes

20. The summary of this incident is as follows:

Direct cause
The IP 183.129.189.60 launched a malicious security scan against the server, aggressively probing for sensitive files (settings.ini, secret.sql, database.json, etc.) under the /core/static/ path. At the same time, four distributed crawler IP ranges (143.20.219.x, 222.167.251.x, 66.92.14.x, 136.0.94.x) sent a massive amount of concurrent requests to my blog within the same second. All of these requests were processed by PHP-FPM.
Root cause
My server has a single-core CPU and 2GB of memory, but PHP-FPM was configured with max_children = 16 and start_servers = 10. Each PHP-FPM child process consumes about 110MB of memory; 16 processes = 1.76GB, which nearly exhausted the entire 2GB of memory. This configuration had barely sustained normal traffic over the past 993 days, but it was always in a “critical state” with no margin to handle traffic spikes.
Avalanche process
Malicious scanning + crawler traffic flooding in → all 16 PHP-FPM processes occupied → memory exhausted, system starts using swap → swap is on disk, I/O slows down → request processing time increases → processes cannot be released in time → new requests queue up → Nginx times out waiting for a PHP-FPM response → returns 504 Gateway Timeout. The CPU load spiked from a normal 0.5 to 19.77, creating a vicious cycle.
Resolution
Reduced PHP-FPM max_children from 16 to 5 to match the single-core 2GB hardware configuration. Blocked the malicious scanning IP and four crawler IP ranges at the Nginx level; blocked requests now return a 403 directly without consuming PHP-FPM processes. Restarted the ECS instance to clear the backlog of stuck processes.
Lessons learned
The number of PHP-FPM processes must match the server’s hardware resources; a single-core 2GB machine should run a maximum of 5 processes. Going 993 days without a restart or configuration adjustment, leaving it in a chronically critical resource state, was the biggest hidden risk. The server should have basic protective measures in place, such as Nginx rate limiting (limit_req_zone) and automated blocking of malicious IPs, rather than running exposed and waiting for an incident to happen.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系

评论

一条对“How I Troubleshot and Resolved a Sudden 504 Error on My Personal Blog Today”的回复

  1. […] my site http://www.shuijingwanwq.com It is a WordPress-based content station, with about 20,000 pages, and the average daily traffic is 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 website has been running smoothly until a large area suddenly appears one night 504 Gateway time-out, the background is especially slow and almost impossible to operate. In fact, I have encountered the same problem some time ago, and I have also made a preliminary optimization:My personal blog, today suddenly responded to 504’s troubleshooting process […]