nginx错误502,503,504分析

来自:互联网
时间:2019-08-07
阅读:

开发过程中我们经常会遇到Nginx 502,503,504错误,这些错误代表什么?什么情况下会出现这些错?下面一一说明(均以nignx+php-fpm举例):

wikipedia上这么解释:

502 Bad Gateway
The server was acting as a gateway or proxy and received an invalid response from the upstream server.

503 Service Unavailable
The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.

504 Gateway Time-out
The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

502 Bad Gateway

fpm进程挂掉或者后端程序过长时间未返回。

编写一个简单的php脚本gateway.php进行测试,内容很简单 <?php sleep(10);echo ‘ok;’?> ,开始下面的测试:

1.启动nginx,不启动fpm,直接 curl http://localhost/gateway.php ,响应502 bad gateway错误且nginx的error log出现错误

2017/02/10 19:08:21 [error] 216#216: *84 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: website80.com, request: "GET /gateway.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

2.启动fpm,修改php-fpm.conf中request_terminate_timeout的值为5s。继续 curl http://localhost/gateway.php ,响应502 bad gateway。nginx和php-fpm分别报错

// nginx error log
2017/02/10 19:10:57 [error] 246#246: *88 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.17.0.1, server: website80.com, request: "GET /gateway.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

// php-fpm error log
[10-Feb-2017 19:10:57] WARNING: [pool www] child 242, script '/home/website/default/gateway.php' (request: "GET /gateway.php") execution timed out (6.205572 sec), terminating
[10-Feb-2017 19:10:57] WARNING: [pool www] child 242 exited on signal 15 (SIGTERM) after 36.692467 seconds from start

503 Service Unavailable

当遇到这个状态码的时候表示服务临时不可用,比如nginx配置了频率限制而client端又超过了配置的限制后就会收到503的响应。

504 Gateway Time-out

nginx的fastcgi模块有一个fastcgi_read_timeout配置,它表示从FastCGI server获取数据的超时时间。如果超过这个配置客户端就是收到504的响应。还以 gateway.php 举例(修改fastcgi_read_timeout的值为5s):

// nginx error log
2017/02/12 14:57:26 [error] 138#138: *1113 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: website80.com, request: "GET /gateway.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
返回顶部
顶部