0

0

php+nginx服务发生500、502错误如何排查

王林

王林

发布时间:2023-05-22 09:25:14

|

2139人浏览过

|

来源于亿速云

转载

概述

当线上的服务中访问中出现500或者502错误时,需要紧急处理,排查问题,该怎么做?可以通过分析一些错误日志或者跟踪php-fpm进程来进行问题定位。

nginx error_log

nginx的error_log在nginx的配置文件中定义的

server {
 listen  80;
 server_name localhost;
 root   /var/www;

 access_log /users/jiao/logs/default.access.log;
 error_log /users/jiao/logs/default.error.log;
 location / {
  index index.html index.htm index.php;
  autoindex on;
 }
 location = /info {
  allow 127.0.0.1;
  deny all;
  rewrite (.*) /.info.php;
 }
 location ~ \.php$ {
  root /var/www;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param script_filename /var/www$fastcgi_script_name;
  include /usr/local/etc/nginx/fastcgi_params;
 }
}

查看error_log

➜  tail /users/jiao/logs/default.error.log
2019/07/17 11:08:18 [error] 77416#0: *76 kevent() reported about an closed connection (54: connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "get / http/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

发现出现了connection reset by peer,连接被重置了,此时可以再查看php-fpm的error_log进一步分析问题

php-fpm error_log

php-fpm的error_log在php-fpm.conf文件中配置中定义的

; error log file
; if it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; note: the default prefix is /usr/local/var
; default value: log/php-fpm.log
error_log = log/php-fpm.log

error_log里面的内容是这样的

Glimmer Ai
Glimmer Ai

基于GPT-3和DALL·E2的PPT制作工具

下载
➜ tail /usr/local/var/log/php-fpm.log
[17-jul-2019 10:49:54] notice: [pool www] child 81948 started
[17-jul-2019 11:08:18] warning: [pool www] child 77537, script '/var/www/index.php' (request: "get /index.php") execution timed out (3.801267 sec), terminating
[17-jul-2019 11:08:18] warning: [pool www] child 77537 exited on signal 15 (sigterm) after 1503.113967 seconds from start
[17-jul-2019 11:08:18] notice: [pool www] child 94339 started

可以看到是请求/var/www/index.php文件出现了超时

dtruss

dtruss是动态跟踪命令,可以根据pid,name跟踪进程

mac环境下使用dtruss,linux环境可以使用strace,pstack

➜ dtruss 
usage: dtruss [-acdefholls] [-t syscall] { -p pid | -n name | command | -w name }
  -p pid   # examine this pid
  -n name   # examine this process name
  -t syscall  # examine this syscall only
  -w name   # wait for a process matching this name
  -a    # print all details
  -c    # print syscall counts
  -d    # print relative times (us)
  -e    # print elapsed times (us)
  -f    # follow children
  -l    # force printing pid/lwpid
  -o    # print on cpu times
  -s    # print stack backtraces
  -l    # don't print pid/lwpid
  -b bufsize  # dynamic variable buf size

eg,

 dtruss df -h  # run and examine "df -h"
 dtruss -p 1871  # examine pid 1871
 dtruss -n tar  # examine all processes called "tar"
 dtruss -f test.sh # run test.sh and follow children

跟踪php-fpm:sudo dtruss -a -n php-fpm<br>

此时访问web页面,就可以看到跟踪内容

21416/0x3479b6:  1559  63  3 getrusage(0x0, 0x7ffee1ec0760, 0x0)   = 0 0
21416/0x3479b6:  1561  4  0 getrusage(0xffffffffffffffff, 0x7ffee1ec0760, 0x0)   = 0 0
21416/0x3479b6:  1627  77  17 poll(0x7ffee1ec08c0, 0x1, 0x1388)   = 1 0
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
21416/0x3479b6:  1872  29  24 lstat64("/var/www/index.php\0", 0x7ffee1ecff38, 0x0)   = 0 0
21416/0x3479b6:  1884  9  6 lstat64("/var/www\0", 0x7ffee1ecfdf8, 0x0)   = 0 0
21416/0x3479b6:  1889  6  3 lstat64("/var\0", 0x7ffee1ecfcb8, 0x0)   = 0 0
21416/0x3479b6:  1899  12  8 readlink("/var\0", 0x7ffee1ed0090, 0x400)   = 11 0
21416/0x3479b6:  1905  6  4 lstat64("/private/var\0", 0x7ffee1ecfb78, 0x0)   = 0 0
21416/0x3479b6:  1917  6  3 lstat64("/private\0", 0x7ffee1ecfa38, 0x0)   = 0 0
21416/0x3479b6:  2178  18  14 stat64("/var/www/.user.ini\0", 0x7ffee1ed0240, 0x0)   = -1 err#2
21416/0x3479b6:  2217  5  1 setitimer(0x2, 0x7ffee1ed07e0, 0x0)   = 0 0
21416/0x3479b6:  2225  4  0 sigaction(0x1b, 0x7ffee1ed0788, 0x7ffee1ed07b0)   = 0 0
21416/0x3479b6:  2237  5  1 sigprocmask(0x2, 0x7ffee1ed0804, 0x0)   = 0x0 0
21416/0x3479b6:  3643  48  40 open_nocancel(".\0", 0x0, 0x1)   = 5 0
21416/0x3479b6:  3648  7  3 fstat64(0x5, 0x7ffee1ed0110, 0x0)   = 0 0
21416/0x3479b6:  3653  7  2 fcntl_nocancel(0x5, 0x32, 0x10f252158)   = 0 0
21416/0x3479b6:  3661  12  7 close_nocancel(0x5)   = 0 0
21416/0x3479b6:  3670  10  7 stat64("/usr/local/var\0", 0x7ffee1ed0080, 0x0)   = 0 0
21416/0x3479b6:  3681  11  8 chdir("/var/www\0", 0x0, 0x0)   = 0 0
21416/0x3479b6:  3698  4  0 setitimer(0x2, 0x7ffee1ed02d0, 0x0)   = 0 0
21416/0x3479b6:  3710  6  3 fcntl(0x3, 0x8, 0x10f3fd858)   = 0 0
21416/0x3479b6:  3733  9  6 stat64("/private/var/www/index.php\0", 0x7ffee1ecff10, 0x0)   = 0 0
74904/0x332630: 723125 1073381  19 kevent(0x9, 0x0, 0x0)   = 0 0
74902/0x332629: 770666 1073387  17 kevent(0x8, 0x0, 0x0)   = 0 0
74904/0x332630: 723165 1061954  20 kevent(0x9, 0x0, 0x0)   = 0 0
74902/0x332629: 770709 1061954  20 kevent(0x8, 0x0, 0x0)   = 0 0
74904/0x332630: 723201 1074786  16 kevent(0x9, 0x0, 0x0)   = 0 0
74902/0x332629: 770747 1074783  16 kevent(0x8, 0x0, 0x0)   = 0 0
74904/0x332630: 723229 1069141  13 kevent(0x9, 0x0, 0x0)   = 0 0
74902/0x332629: 770777 1069145  11 kevent(0x8, 0x0, 0x0)   = 0 0
21416/0x3479b6:  3942 3902233  7 __semwait_signal(0x703, 0x0, 0x1)   = -1 err#4
74902/0x332629: 770814  103  25 kill(21416, 15)   = 0 0
dtrace: error on enabled probe id 2172 (id 161: syscall::write:return): invalid kernel access in action #13 at dif offset 68
dtrace: error on enabled probe id 2172 (id 161: syscall::write:return): invalid kernel access in action #13 at dif offset 68
74902/0x332629: 771325  7  2 sigreturn(0x7ffee1ecfc40, 0x1e, 0xc1a4b78e0404663a)   = 0 err#-2
74902/0x332629: 771336  7  3 kevent(0x8, 0x0, 0x0)   = 1 0
dtrace: error on enabled probe id 2174 (id 159: syscall::read:return): invalid kernel access in action #13 at dif offset 68
74902/0x332629: 771352  11  7 wait4(0xffffffffffffffff, 0x7ffee1ed0748, 0x3)   = 21416 0
dtrace: error on enabled probe id 2172 (id 161: syscall::write:return): invalid kernel access in action #13 at dif offset 68
74902/0x332629: 773511 1957 1899 fork()   = 28060 0
28060/0x3754c5:  125:  0:  0 fork()   = 0 0
28060/0x3754c5:  128  9  2 bsdthread_register(0x7fff6774c418, 0x7fff6774c408, 0x2000)   = -1 err#22
dtrace: error on enabled probe id 2172 (id 161: syscall::write:return): invalid kernel access in action #13 at dif offset 68
74902/0x332629: 773737  4  1 wait4(0xffffffffffffffff, 0x7ffee1ed0748, 0x3)   = 0 0
74902/0x332629: 773742  6  3 read(0x5, "\0", 0x1)   = -1 err#35
28060/0x3754c5:  320  4  0 getpid(0x0, 0x0, 0x0)   = 28060 0
28060/0x3754c5:  328  7  2 __mac_syscall(0x7fff67758a17, 0x4, 0x7ffee1ed0208)   = -1 err#45
28060/0x3754c5:  332  5  2 csops(0x6d9c, 0xb, 0x7ffee1ed0248)   = -1 err#22
28060/0x3754c5:  755  14  11 dup2(0x1, 0x2, 0x0)   = 2 0
28060/0x3754c5:  797  89  22 close(0x4)   = 0 0
28060/0x3754c5:  806  11  6 dup2(0x7, 0x0, 0x0)   = 0 0
28060/0x3754c5:  817  4  0 geteuid(0x0, 0x0, 0x0)   = 501 0
28060/0x3754c5:  820  3  0 close(0x5)   = 0 0
28060/0x3754c5:  821  3  0 close(0x6)   = 0 0
28060/0x3754c5:  824  5  1 sigaction(0xf, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  825  3  0 sigaction(0x2, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  827  3  0 sigaction(0x1e, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  828  3  0 sigaction(0x1f, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  829  3  0 sigaction(0x14, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  830  3  0 sigaction(0x3, 0x7ffee1ed0688, 0x0)   = 0 0
28060/0x3754c5:  1043  3  0 close(0x7)   = 0 0

可以看到系统底层执行的函数,如lstat64获取文件内容信息,kill(21416, 15)kill掉php-fpm进程,fork()出新的php-fpm进程

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

Go高并发任务调度与Goroutine池化实践
Go高并发任务调度与Goroutine池化实践

本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

38

2026.03.10

Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

83

2026.03.09

JavaScript浏览器渲染机制与前端性能优化实践
JavaScript浏览器渲染机制与前端性能优化实践

本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

97

2026.03.06

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

223

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

458

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

169

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

246

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

34

2026.03.03

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 13.4万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.3万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号