# APM server behind nginx closes connection prematurely

**URL:** <https://discuss.elastic.co/t/apm-server-behind-nginx-closes-connection-prematurely/135624>\
**Category:** APM\
**Created:** [June 13, 2018, 4:45am UTC](https://discuss.elastic.co/t/apm-server-behind-nginx-closes-connection-prematurely/135624 "2018-06-13T04:45:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorgen\_Ader](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jorgen_ader/32/32193_2.png) [@Jorgen\_Ader](https://discuss.elastic.co/u/Jorgen_Ader)\
**Post date:** [June 13, 2018, 4:45am UTC](https://discuss.elastic.co/t/apm-server-behind-nginx-closes-connection-prematurely/135624/1 "2018-06-13T04:45:36Z")

</div>

The problem: Apm agent starts getting 502 after payload seems to go over ~120kb, have not figured exact value yet.

Http basic auth is used for elastic/kibana/apm (apm agents are ip whitelisted and use token)

Nginx logs

```auto
2018/06/13 04:30:12 [error] 4825#4825: *5334 upstream prematurely closed connection while reading response header from upstream, client: *.*.*.*, server: ***, request: "POST /v1/transactions HTTP/1.1"

```

Apm logs

```auto
2018-06-13T04:30:12.941Z	INFO	[request]	beater/handlers.go:150	handled request	{"request_id": "***", "response_code": 202, "method": "POST", "URL": "/v1/transactions", "content_length": 138635, "remote_address": "*.*.*.*", "user_agent": "elasticapm-python/2.1.1"}

```

relevant Nginx config:

```auto
// default nginx config
keepalive_timeout 65;

// upstream server config
upstream apm-server {
    server apm_server:8200;

    keepalive 15;
}

// location block
location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Connection "Keep-Alive";
        proxy_set_header Proxy-Connection "Keep-Alive";
        proxy_redirect off;
        proxy_pass http://apm-server;
    }

```

Apm config

```auto
apm-server:
  host: "0.0.0.0:8200"
  secret_token: "the token"
  max_unzipped_size: 104857600
  max_header_size: 10485760
  concurrent_requests: 50

setup.dashboards.enabled: true

setup.kibana.host: "kibana host"
setup.kibana.username: "http user"
setup.kibana.password: "http pw"

output.elasticsearch:
  hosts: ['elastic-host']
  headers:
    Authorization: "Basic x:x"

queue:
  mem:
    events: 16384
    flush.min_events: 512

```

Versions used:  
APM Server: 6.2.4  
APM python agent: 2.1.1  
Nginx: 1.12.2

---

<div class="post-metadata">

**Author:** ![gil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gil/32/41911_2.png) [@gil](https://discuss.elastic.co/u/gil)\
**Post date:** [June 13, 2018, 1:53pm UTC](https://discuss.elastic.co/t/apm-server-behind-nginx-closes-connection-prematurely/135624/2 "2018-06-13T13:53:05Z")

</div>

Thanks for the report. This can happen when the apm-server doesn't start writing a response back to the client within the write timeout. In 6.2, you can bump `apm-server.write_timeout` up to address this - the default in the upcoming release is `30s`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 4, 2018, 9:53am UTC](https://discuss.elastic.co/t/apm-server-behind-nginx-closes-connection-prematurely/135624/3 "2018-07-04T09:53:09Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
