即使保持活动状态,Nginx也会关闭连接

So I have an issues which I'm not sure if it's a GO problem or issues with nginx configuration. Basically when trying to test a curl request to our servers, the connection unexpectedly gets closed, even though keep-alive is set to true. Below is the log I get back from nginx:

* About to connect() to sub.domain.com port 80 (#0)
* Trying W.X.Y.Z... connected
> POST /br HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: sub.domain.com
> Accept: */*
> Connection: keep-alive
> Content-Length: 1635
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Wed, 31 Aug 2016 15:31:21 GMT
< Server: nginx/1.6.1
< Content-Length: 3158
< Connection: keep-alive
<
{"data1": "some data", "data2": "some data2" ....
* Connection #0 to host sub.domain.com left intact
* Closing connection #0
... "dataX": "some dataX", "dataY": "some dataY}

Our nginx keep-alive configuration:

keepalive_timeout 150;
keepalive_requests 5000;

The nginx is sitting in front of a Go server, although it shouldn't impact it. Any tips on how to go about trying to figure out why the keep-alive is closing the connection?

Edit: complete nginx config:

user  nginx;
worker_processes  auto;
worker_rlimit_nofile 200000;

pid        /var/run/nginx.pid;

events {
    worker_connections 60000;
    multi_accept on;
}

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

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

    error_log   /dev/null;
    access_log   /dev/null;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout 150;
    keepalive_requests 5000;
    client_header_timeout 2;
    client_body_timeout 2;

    reset_timedout_connection on;
    send_timeout 2;
    gzip  on;

    include /etc/nginx/conf.d/*.conf;

    upstream go_app {
    server 127.0.0.1:3000 weight=1;
    keepalive 2000;
    }

    server {
        listen 80;
        server_name 1.domain.com 2.domain.com 3.domain.com 4.domain.com 5.domain.com;
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://go_app;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
    }


    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;

        location / {
        }

        error_page  404              /404.html;
        location = /40x.html {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
    }
}

And the go-code responsible for writing the response:

func Success(response http.ResponseWriter, value string) {
    response.Header().Set("Content-Type", "application/json; charset=utf-8")
    fmt.Fprint(response, value)
    response.Write(nil)
}
duancilan5124
duancilan5124 客户端交付POST正文需要多长时间?服务器(您的Gohttp处理程序或NGINX)需要多长时间才能将回复传递给客户端。您的NGINX配置中有一些超时,可能会影响keepalive。我想知道您是否尝试为“流”数据返回客户端保持长期连接。如果是这种情况,那么send_timeout设置对我来说很可疑。客户端和服务器之间的tcpdump至少应告诉您哪个端正在发起关闭。
接近 4 年之前 回复
doushi6947
doushi6947 不知道这里提供的信息是怎么回事。您可能需要记录nginx和后端之间发生的情况。另外,您确定是服务器关闭了连接吗?您是否使用http/1.1客户端进行了测试,并且使用相同的连接发出多个请求?
接近 4 年之前 回复
dongpi0658
dongpi0658 我添加了更多信息,其中包括负责在go中编写响应以及完整的nginx配置的函数。
接近 4 年之前 回复
dongye7231
dongye7231 可能是关闭连接的后端。代理配置是什么,Go服务器如何处理连接?
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐