Nginx错误日志分析

来源: 海外服务器
导读 Nginx错误日志是洞察服务器运行状况的宝贵窗口,它忠实记录了服务运行中遇到的各种问题。通过分析这些日志条目,我们可以快速定位性能瓶颈、安全威胁和配置缺陷。无论是连接超时、权限拒绝,还是资源耗尽和恶意扫描,每一个错误代码都在讲述系统运行的真实故事。掌握日志分析技巧,能帮助运维人员从被动的故障处理转向主动的预防优化。学习使用grep筛选关键信息、借助awk进行数据统计,或是通过可视化工具发现异常模式,都将极大提升问题诊断效率。

# more nginx/error.log2010/08/18 14:42:19 [error] 18256#0: *264534 readv() failed (104:Connection reset by peer) while reading upstream, client:6.217.236.22, server: wo.china.com, request: "GET /list/index/rcreated/HTTP/1.0", upstream: "fastcgi://127.0.0.1:10080", host:"www.hichina.com"2010/08/18 14:46:55 [error] 18254#0: *272781 upstream prematurelyclosed connection while reading response header from upstream,client: 6.217.236.22, server: wo.china.com, request: "GET /top/HTTP/1.0", upstream: "fastcgi://127.0.0.1:10080", host:"www.hichina.com"2010/08/18 14:46:55 [error] 18254#0: *272781 open()"/var/www/nginx-default/50x.html" failed (2: No such file ordirectory), client:6.217.236.22, server: wo.china.com, request: "GET /top/HTTP/1.0", upstream: "fastcgi://127.0.0.1:10080", host:"www.hichina.com"

# more kern.logAug 18 14:42:18 w23 kernel: [2524799.982979] php-cgi[30124]:segfault at 45 ip 082da5bf sp bfd89c50 error 4 inphp5-cgi[8048000+4cc000]

出现这种问题的原因:# ab -c 1000 -n 10000http://www.hichina.com/list/index/rcreated/

在用ab测试之前:[email protected]:/var/log# netstat -anpActive Internet connections (servers and established)Proto Recv-Q Send-Q LocalAddressForeign AddressState PID/Program nametcp0 0127.0.0.1:100800.0.0.0:*LISTEN4145/php-cgi在用ab测试之后:[email protected]:/var/log# netstat -anp|moreActive Internet connections (servers and established)Proto Recv-Q Send-Q LocalAddressForeign AddressState PID/Program nametcp0 0127.0.0.1:100800.0.0.0:*LISTEN4145/php-cgi说明:在用ab测试后cgi的pid相同,说明不是网上说的“由于php-cgi重启或者cgi进程死掉造成的”

$ ps aux|grep php-cgi|wc 514 5655 42144

# ulimit -acore filesize (blocks,-c) 0data segsize(kbytes, -d) unlimitedschedulingpriority(-e) 0filesize(blocks, -f) unlimitedpendingsignals(-i) 16382max locked memory (kbytes, -l)64max memory size(kbytes, -m) unlimitedopenfiles(-n) 1024pipesize(512 bytes, -p) 8POSIX message queues (bytes, -q) 819200real-timepriority(-r) 0stacksize(kbytes, -s) 8192cputime(seconds, -t) unlimitedmax userprocesses(-u) unlimitedvirtualmemory(kbytes, -v) unlimitedfilelocks(-x) unlimited

ab压力测试,问题及原因:# ab -c 515 -n 1000 http://192.168.206.121/2010/08/26 10:35:03 [alert] 13342#0: *8353 socket() failed (24: Toomany open files) while connecting to upstream, client:192.168.206.120,

server: localhost, request: "GET / HTTP/1.0", upstream:"fastcgi://127.0.0.1:10080", host:"192.168.206.121"(超过了cgi进程的个数)

2010/08/26 10:53:16 [alert] 15016#0: *4644 1024worker_connections are not enough while connecting to upstream,client: 192.168.206.120, server:

localhost, request: "GET / HTTP/1.0", upstream:"fastcgi://127.0.0.1:10080", host:"192.168.206.121"(增加worker_connections的数量)

2010/08/26 17:26:21 [error] 12936#0: *685388 connect() failed(111: Connection refused) while connecting to upstream, client:192.168.206.121,

server: localhost, request: "GET /info.php HTTP/1.0", upstream:"fastcgi://127.0.0.1:10080", host: "192.168.206.121"