修复Nginx + PHP 5xx Error

之前时不时会收到 Google Search Console 发来的邮件告知在索引页面的时候遇到了5xx,一直都没有管。 直到上周我自己重现了一次才开始重视起来。
Search Console has identified that your site is affected by 1 Page indexing issue(s). The following issues were found on your site.

Top Issues

Server error (5xx)
We recommend that you fix these issues when possible to enable the best experience and coverage in Google Search.
检查日志发现如下内容:

2023/12/12 01:23:45 [error] 2175086#0: *57111 connect() to unix:/run/php-fpm/www.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 192.3.114.12, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/www.sock:", host: "example.com", referrer: "http://example.com/"

于是跑了一遍压力测试,发现将近一半的请求都遇到5xx错误,同时日志中产生如上记录。

wrk -t12 -c400 -d30s https://ioio.name

便因此做了一下研究,主要参考 11: Resource temporarily unavailable, while connecting to upstream + Bad Gateway (Nginx)

通过执行如下命令调整了 net.core.somaxconn 及 net.core.netdev_max_backlog

echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
echo "net.core.netdev_max_backlog = 65535" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

更新nginx配置文件,给 FastCGI service 添加

然后重启服务

再次运行压力测试,错误消失。

wrk -t12 -c400 -d30s https://ioio.name
Running 30s test @ https://ioio.name
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 180.51ms 88.60ms 1.91s 89.36%
Req/Sec 109.17 32.60 212.00 71.29%
39114 requests in 30.08s, 1.86GB read
Socket errors: connect 158, read 0, write 0, timeout 0
Requests/sec: 1300.31
Transfer/sec: 63.29MB

-EOF-