即使保持活着,Nginx也会关闭连接

时间:2016-08-31 15:49:17

标签: nginx go

所以我遇到一些问题,我不确定它是GO问题还是nginx配置问题。基本上,当尝试测试对我们的服务器的curl请求时,即使keep-alive设置为true,连接也会意外关闭。 下面是我从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}

我们的nginx keep-alive配置:

keepalive_timeout 150;
keepalive_requests 5000;

nginx坐在Go服务器前面,虽然它不应该影响它。关于如何试图弄清楚为什么保持活着正在关闭连接的任何提示?

编辑: 完成nginx配置:

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 {
        }
    }
}

负责编写回复的go代码:

func Success(response http.ResponseWriter, value string) {
    response.Header().Set("Content-Type", "application/json; charset=utf-8")
    fmt.Fprint(response, value)
    response.Write(nil)
}

0 个答案:

没有答案