• 搜索
  • 夜间模式
    ©2016-2025  周伯通的小站 Theme by OneBlog
    搜索
    标签
    # linux # python # android # 学习 # 折腾 # 教程 # 记录 # 软件 # vps # 新闻
  • 首页>
  • 分享>
  • 正文
  • nginx错误502,503,504分析

    2017年11月26日 19 阅读 0 评论 3592 字

    开发过程中经常会遇到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出现错误
      1
      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分别报错
      1
      2
      3
      4
      5
      6
      // 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):

    1
    2
    // 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 upst

    本文著作权归作者 [ 老周 ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    linuxnginx折腾
    取消回复

    发表留言
    回复

    Copyright©2016-2025  All Rights Reserved.  Load:0.016 s
    Theme by OneBlog V3.6.4
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。