Ngnix上游超时错误

时间:2016-11-11 07:11:41

标签: nginx amazon-ec2 yii2 php-7

我已在amazon ec2实例上安装了php7和nginx,并在yii2中收到了以下错误。

2016/11/11 07:00:33 [error] 11220#0: *14 upstream timed out (110: Connection timed out) 
while reading response header from upstream, client: XXX.XX.XXX.111, server: 
example.com, request: "POST /backend/web/index.php?r=site%2Flogin HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "example.com",
referrer: "http://example.com/backend/web/index.php?r=site%2Flogin"

以下是我的nginx配置文件。 (/etc/nginx/conf.d/virtual.conf)

server {
    charset utf-8;
    client_max_body_size 128M;
    listen       80;
    server_name example.com;


    location / {
        root   /path/to/root;
        index index.php  index.html index.htm;
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php?$args;
    }

    error_page  404              /errors/404.html;
    location = /errors/404.html {
        root   /path/to/root;
    }

    error_page   500 502 503 504  /errors/50x.html;
    location = /errors/50x.html {
        root   /path/to/root;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /path/to/root;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass   /var/run/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
        try_files $uri =404;
    }
    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

我已经尝试做以下但没有运气:(

location / {
        ...
        proxy_read_timeout 300;
        ...
    }

5 个答案:

答案 0 :(得分:1)

那些奇怪的错误是正常的。对于yii2,更喜欢unix socket(/var/run/php7.0-fpm.sock;或其他)。在PHP-FPM配置文件(php-fpm.conf)中设置request_terminate_timeout=30s。在php.ini制作max_execution_time = 30

这几乎是一种防故​​障配置。针对每次更改调试nginx -t并检查前端。

    location ~ \.php$ {
            root            /usr/share/nginx/html;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include         fastcgi_params;
    }

如果仍然无效,请将client_max_body_size 128M;增加到1 GB。

可以编译一些发行版,将PHP内容保存在/etc/nginx/snippets/。在cat上运行/etc/nginx/fastcgi_params以检查其内容。如果您添加/path/to/root/$fastcgi_script_name,请添加fastcgi_index,请确保其为fastcgi_params;

显然虔诚地检查php-fpm.conf是否有TCP或unix socket config。

下面的东西在逻辑上是不正确的。 600年是一个巨大的时间。但有时候它有效。请注意nginx.conf

proxy_connect_timeout  600s;
proxy_send_timeout  600s;
proxy_read_timeout  600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;

答案 1 :(得分:1)

很可能是您的上游错误。确保你可以连接,防火墙没有阻止任何东西。

如果你使用fastcgi(PHP-FPM),那么你需要调整fastcgi_read_timeout而不是proxy_read_timeout。

此处有更多信息:Nginx upstream timed out (why and how to fix)

答案 2 :(得分:0)

fastcgi_pass 127.0.0.1:9000;

你有没有收听9000端口的东西?试试跑步 netstat -atpn | grep 9000

答案 3 :(得分:0)

您的上游应用是否可以收听?我通常会使用简单的东西,如telnet'测试上游正在倾听和回应:

[root@ip-10-0-1-1 opt]# telnet 127.0.0.1 8080
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]close

telnet> close
Connection closed.
[root@ip-10-0-1-1 opt]#

如果您的流程尚未投放(但可以访问),您会看到拒绝连接的情况。错误:

[root@ip-10-0-1-1 opt]# telnet localhost 8000
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

HTH

答案 4 :(得分:0)

首先检查netstat -atpn | grep 9000,通常有很多连接需要像这样关闭:

tcp 1257 0 localhost:9000 localhost:46650 CLOSE_WAIT
tcp 1257 0 localhost:9000 localhost:46650 CLOSE_WAIT
tcp 1257 0 localhost:9000 localhost:46650 CLOSE_WAIT

尝试在 request_terminate_timeout 上启用 /etc/php/7.x/fpm/pool.d/www.conf 并给出终止请求的持续时间。像这样:

request_terminate_timeout=30s

并确保像这样在 max_execution_time 上启用 php.ini

max_execution_time = 30