【问题标题】:Upstream Node server closing connection to nginx上游节点服务器关闭到 nginx 的连接
【发布时间】:2017-05-09 07:09:17
【问题描述】:

我正在使用 nginx 作为节点服务器的代理,它是限速请求的。速率为每 30 秒一个请求;大多数请求都可以返回响应,但是如果请求长时间保持打开状态,我会得到以下信息:

upstream prematurely closed connection while reading response header from upstream

我不知道是什么原因造成的。下面是我的nginx配置:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /srv/www/main/htdocs;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;    

        location /vcheck {
            proxy_pass http://127.0.0.1:8080$is_args$query_string;
            # proxy_buffer_size 128k;
            # proxy_buffers 4 256k;
            # proxy_busy_buffers_size 256k;
            # proxy_http_version 1.1;
            # proxy_set_header Upgrade $http_upgrade;
            # proxy_set_header Connection 'upgrade';
            # proxy_set_header Host $host;
            # proxy_cache_bypass $http_upgrade;         
            # proxy_redirect off;

            proxy_read_timeout 600s;
        }

        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index routes.php$is_args$query_string;
        }

        location / {
            if (-f $request_filename) {
                expires max;
                break;
            }

            if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
                rewrite ^(.*) /routes.php last;
            }
        }       

    }
}

Node 提前关闭连接是否有原因?

编辑:我正在使用 Node 的内置 HTTP 服务器。

【问题讨论】:

  • 如果是 Node 正在关闭连接,那么显示 Node 代码而不是 nginx 配置会更有意义。

标签: node.js nginx


【解决方案1】:

似乎您必须延长 nodejs 应用程序的响应超时时间。

如果是expressjs 应用程序,我猜你试试这个:

安装:npm i --save connect-timeout

使用:

var timeout = require('connect-timeout');
app.use(timeout('60s'));



但我建议不要让连接等待并修复 nodejs 应用程序中的问题,找出它停止这么长时间的原因。

似乎 nodejs 应用程序存在无法响应的问题,并且请求丢失,让 nginx 等待。

【讨论】:

  • 我没有使用快递,但你提到了超时;节点的内置服务器有自己的超时,因此将其设置为零是解决方法。
  • @Raggeth 我的例子是为了表达,但是是的想法是告诉你它应该有超时属性。 Cuz express 本身是在扩展内置网络服务器的connect 框架上扩展的。
猜你喜欢
  • 2020-09-28
  • 2021-02-03
  • 2016-10-19
  • 2020-07-08
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
相关资源
最近更新 更多