首頁 運維 Nginx php+nginx服務發生500、502錯誤如何檢查

php+nginx服務發生500、502錯誤如何檢查

May 22, 2023 am 09:25 AM
php nginx

概述

当线上的服务中访问中出现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里面的内容是这样的

➜ 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+nginx服務發生500、502錯誤如何檢查的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1425
52
Laravel 教程
1323
25
PHP教程
1272
29
C# 教程
1251
24
PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和Python:深入了解他們的歷史 PHP和Python:深入了解他們的歷史 Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

為什麼要使用PHP?解釋的優點和好處 為什麼要使用PHP?解釋的優點和好處 Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

docker怎麼啟動容器 docker怎麼啟動容器 Apr 15, 2025 pm 12:27 PM

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

PHP與Python:用例和應用程序 PHP與Python:用例和應用程序 Apr 17, 2025 am 12:23 AM

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

See all articles