欢迎来到资源库(www.zyku.net)

Nginx

当前位置:首页 > 服务器教程 > Nginx > 502

nginx错误502,503,504分析

时间:2018-12-09|栏目:Nginx|点击:|我要投稿

开发过程中我们经常会遇到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"

(资源库 www.zyku.net)

502 503 504

上一篇:Nginx搭建负载均衡实现动静分离的方法

栏    目:Nginx

下一篇:Nginx反向代理后获取不到客户端的真实ip地址的解决方法

本文标题:nginx错误502,503,504分析

本文地址:https://www.zyku.net/nginx/1753.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务