• nginx出现504 Gateway Time-out的解决思路


    在安装完Nginx+PHP-fpm+Mysql后 (如何安装LNMP环境,请参考快速配置LNMP环境Nginx0.8.54+MYSQL5.5.16+PHP5.3.8+CentOS 6),跑PHP的应用会经常出现504 Gateway Time-out 或者502 Bad Gateway的情况。

    这个问题耽误了我差不多4个小时的时间,网上有很多前辈们的解决方法,在这里记录下解决这个问题的思路。首先这个问题主要是因为PHP的Script执行时间太长了,已经超过nginx能接受的底线。

    在nginx的日志中会看到这样的log

    2012/08/11 13:39:45 [error] 30788#0: *1 upstream timed out (110: Connection timed out) 
    while reading response header from upstream, client: 127.0.0.1, server: www.xbc.me, 
    request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.xbc.me"

    在php-fpm的日志中会发现这样的log

    2012/08/11 13:39:45 [error] 30788#0: *1 upstream timed out (110: Connection timed out) 
    while reading response header from upstream, client: 127.0.0.1, server: www.xbc.me, 
    request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.xbc.me"

    一般来说,出现这样的情况是因为Nginx会从php-fpm的9000端口读取fastcgi的执行结果,等来N久都不见回复,所以就报504了。解决办法很简单,修改php的最长执行时间

    ; Maximum execution time of each script, in seconds; http://php.net/max-execution-time; 
    Note: This directive is hardcoded to 0 for the CLI SAPI;
    fix by Matt 2012.8.11
    ;max_execution_time = 3000

    在这里我注释掉php.ini参数的时间限制。还有php-fpm里的

    ;request_terminate_timeout = 0

    这个参数会在php.ini中max_execution_time因为某些原因不能正常工作才会生效。

    之前设置的是300s,安装Magento的时候就一直就报504。后来索性修改到3000了。在Nginx的配置文件添加

    #add by Matt 2012.8.11
    fastcgi_read_timeout 3000;
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;

    Magento你到底安装多少sql文件啊?

    PS:有时候缓存过小也会引起504,具体情况还需要根据Nginx的日志内容来分析。修改fastcgi的缓存大小:

    fastcgi_buffers 2 256k;
    fastcgi_buffer_size 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    参考

    解决Nginx + PHP(FastCGI)遇到的502 Bad Gateway错误[原创]

    安装 LNMP 的 VPS Nginx 出现 504 Gateway Time-out

    nginx php fastcgi Connection reset by peer的原因及解决办法

  • 相关阅读:
    Window 命令
    HTTP 状态码
    Mysql基本用法-存储引擎-04
    Mysql基本用法-存储引擎-03
    Mysql基本用法-left join、right join、 inner join、子查询和join-02
    Mysql基本用法-01
    二进制编码-详细讲解
    JS操作文件
    PHP5接口技术入门
    PHP5中__get()、__set()方法
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8334740.html
Copyright © 2020-2023  润新知