【问题标题】:Ngnix upstream timed out errorNginx 上游超时错误
【发布时间】:2017-03-25 09:14:09
【问题描述】:

我已经在亚马逊 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;
        ...
    }

【问题讨论】:

标签: nginx amazon-ec2 yii2 php-7


【解决方案1】:

fastcgi_pass 127.0.0.1:9000;

你在 9000 端口有监听吗?尝试运行 网络统计-atpn | grep 9000

【讨论】:

  • 现在出现另一个错误。我已经授予资产文件夹 ugin root 权限 0777 权限。异常(无效配置)'yii\base\InvalidConfigException',带有消息'Web 进程不可写入目录:/usr/share/nginx/html/xxxx/backend/web/assets'
【解决方案2】:

那些奇怪的错误是正常的。对于 yii2,更喜欢 unix 套接字(/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/。在/etc/nginx/fastcgi_params 上运行cat 以检查它有什么。如果添加/path/to/root/$fastcgi_script_name,包括fastcgi_index,请确保它是fastcgi_params;

显然虔诚地检查php-fpm.conf 的 TCP 或 unix 套接字配置。

以下内容在逻辑上不正确。 600年代是巨大的时间。但有时它会起作用。在nginx.conf 上小心它:

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

【讨论】:

  • 现在出现另一个错误。我已经授予资产文件夹 ugin root 权限 0777 权限。异常(无效配置)'yii\base\InvalidConfigException' 带有消息'Web 进程不可写目录:/usr/share/nginx/html/xxxx/backend/web/assets'
  • 禁用 SELinux。运行$ chgrp <user> /usr/share/nginx/html/xxxx/backend/web/assets $ chmod g+w /usr/share/nginx/html/xxxx/backend/web/assets<user> 对于 CentOS 是 nginx,对于 Ubuntu 是 www-data。检查usergroupps
【解决方案3】:

您的上游应用程序是否在监听并可用?我通常使用像“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

【讨论】:

  • 现在出现另一个错误。我已经授予资产文件夹 ugin root 权限 0777 权限。异常(无效配置)'yii\base\InvalidConfigException' 带有消息'Web 进程不可写目录:/usr/share/nginx/html/xxxx/backend/web/assets'
【解决方案4】:

您的上游很可能有错误。确保您可以连接到并且防火墙没有阻止任何内容。

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

更多信息在这里:Nginx upstream timed out (why and how to fix)

【讨论】:

    【解决方案5】:

    首先检查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
    

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

    request_terminate_timeout=30s
    

    并确保在php.ini 上启用max_execution_time ,如下所示:

    max_execution_time = 30
    

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 2018-02-07
      • 1970-01-01
      • 2019-03-10
      • 2018-08-17
      • 2017-02-09
      • 2014-03-01
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多