# Apm agent cannot connect to remote apm server

**URL:** <https://discuss.elastic.co/t/apm-agent-cannot-connect-to-remote-apm-server/209117>\
**Category:** APM\
**Created:** [November 22, 2019, 7:37pm UTC](https://discuss.elastic.co/t/apm-agent-cannot-connect-to-remote-apm-server/209117 "2019-11-22T19:37:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![he11oworld](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/he11oworld/32/58196_2.png) [@he11oworld](https://discuss.elastic.co/u/he11oworld)\
**Post date:** [November 22, 2019, 7:37pm UTC](https://discuss.elastic.co/t/apm-agent-cannot-connect-to-remote-apm-server/209117/1 "2019-11-22T19:37:54Z")

</div>

**Kibana version** :  
7.4.0  
**Elasticsearch version** :  
7.4.0  
**APM Server version** :  
7.4.0  
**APM Agent language and version** :  
Java, 1.11.0

**Is there anything special in your setup?** For example, are you using the Logstash or Kafka outputs? Are you using a load balancer in front of the APM Servers? Have you changed index pattern, generated custom templates, changed agent configuration etc.

my apm-server is on Kubernetes, and has nginx-proxy in front.

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

The elastic-apm-agent and the java application reside on my local system, I am able to send logs when I run `kubectl port forward apm-server 8200` but I get 502 status when I specify nginx-proxy loadbalancer url in `elasticapm.properties` file.

**Provide logs and/or server output (if relevant)**:

my **elasticapm.properties** file looks like this:

> service\_name=demo  
> application\_packages=ca.com.elastic  
> server\_urls=[https://172.20.11.188](https://172.20.11.188)  
> verify\_server\_cert=false  
> log\_level=debug

**apm-server.yaml file:**

> apm-server.yml: |  
> host: "0.0.0.0:8200"  
> setup.template.settings:  
> index:  
> number\_of\_shards: 1  
> codec: best\_compression  
> setup.kibana:  
> host: "\< kibana endpoint \>"  
> output.elasticsearch:  
> hosts: ["elasticsearch:9200"]

**logs from elastic-apm-agent:**

> 2019-11-22 11:25:03.188 [apm-reporter] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Starting new request to [https://172.20.11.188/intake/v2/events](https://172.20.11.188/intake/v2/events)  
> 2019-11-22 11:25:03.224 [apm-reporter] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Scheduling request timeout in 10s  
> 2019-11-22 11:25:13.225 [apm-request-timeout-timer] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Request flush because the request timeout occurred  
> 2019-11-22 11:25:13.235 [apm-reporter] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Receiving FLUSH event (sequence 1)  
> 2019-11-22 11:25:13.238 [apm-reporter] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Flushing 1386 uncompressed 601 compressed bytes  
> 2019-11-22 11:25:13.240 [apm-reporter] INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 0 seconds (+/-10%)  
> 2019-11-22 11:25:13.240 [apm-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Error sending data to APM server: Server returned HTTP response code: 502 for URL: [https://172.20.11.188/intake/v2/events](https://172.20.11.188/intake/v2/events), response code is 502  
> 2019-11-22 11:25:13.240 [apm-reporter] DEBUG co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Sending payload to APM server failed  
> java.io.IOException: Server returned HTTP response code: 502 for URL: [https://172.20.11.188/intake/v2/events](https://172.20.11.188/intake/v2/events)  
> at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)  
> at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)  
> at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)  
> at co.elastic.apm.agent.report.IntakeV2ReportingEventHandler.flush(IntakeV2ReportingEventHandler.java:224)  
> at co.elastic.apm.agent.report.IntakeV2ReportingEventHandler.handleEvent(IntakeV2ReportingEventHandler.java:128)  
> at co.elastic.apm.agent.report.IntakeV2ReportingEventHandler.onEvent(IntakeV2ReportingEventHandler.java:116)  
> at co.elastic.apm.agent.report.IntakeV2ReportingEventHandler.onEvent(IntakeV2ReportingEventHandler.java:50)  
> at co.elastic.apm.agent.shaded.lmax.disruptor.BatchEventProcessor.processEvents(BatchEventProcessor.java:168)  
> at co.elastic.apm.agent.shaded.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:125)  
> at java.lang.Thread.run(Thread.java:748)  
> 2019-11-22 11:25:13.243 [apm-reporter] WARN co.elastic.apm.agent.report.IntakeV2ReportingEventHandler -
> 
> 502 Bad Gateway
> # 502 Bad Gateway
> 
> * * *
> nginx/1.13.12

**Logs from nginx proxy pod:**

> 2019/11/22 19:24:35 [error] 10#10: \*309 connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /config/v1/agents HTTP/1.1", upstream: "[http://10.19.72.106:8200/config/v1/agents](http://10.19.72.106:8200/config/v1/agents)", host: "172.20.11.188"  
> 10.20.6.1 - - [22/Nov/2019:19:24:35 +0000] "POST /config/v1/agents HTTP/1.1" 502 174 "-" "elasticapm-java/1.11.0" "-"  
> 2019/11/22 19:24:44 [info] 10#10: \*309 client 10.20.6.1 closed keepalive connection  
> 2019/11/22 19:25:43 [error] 10#10: \*311 connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /intake/v2/events HTTP/1.1", upstream: "[http://10.19.72.106:8200/intake/v2/events](http://10.19.72.106:8200/intake/v2/events)", host: "172.20.11.188"  
> 10.20.6.1 - - [22/Nov/2019:19:25:43 +0000] "POST /intake/v2/events HTTP/1.1" 502 174 "-" "elasticapm-java/1.11.0" "-"  
> 2019/11/22 19:25:49 [info] 10#10: \*311 client 10.20.6.1 closed keepalive connection  
> 2019/11/22 19:26:13 [error] 10#10: \*313 connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /intake/v2/events HTTP/1.1", upstream: "[http://10.19.72.106:8200/intake/v2/events](http://10.19.72.106:8200/intake/v2/events)", host: "172.20.11.188"  
> 10.20.6.1 - - [22/Nov/2019:19:26:13 +0000] "POST /intake/v2/events HTTP/1.1" 502 174 "-" "elasticapm-java/1.11.0" "-"  
> 2019/11/22 19:26:26 [info] 10#10: \*313 client 10.20.6.1 closed keepalive connection  
> 10.20.6.1 - - [22/Nov/2019:19:27:13 +0000] "POST /intake/v2/events HTTP/1.1" 502 174 "-" "elasticapm-java/1.11.0" "-"  
> 2019/11/22 19:27:13 [error] 10#10: \*315 connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /intake/v2/events HTTP/1.1", upstream: "[http://10.19.72.106:8200/intake/v2/events](http://10.19.72.106:8200/intake/v2/events)", host: "172.20.11.188"  
> 2019/11/22 19:27:33 [info] 10#10: \*315 client 10.20.6.1 closed keepalive connection  
> 2019/11/22 19:27:43 [error] 10#10: \*317 connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /intake/v2/events HTTP/1.1", upstream: "[http://10.19.72.106:8200/intake/v2/events](http://10.19.72.106:8200/intake/v2/events)", host: "172.20.11.188"

Any ideas?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [November 24, 2019, 7:49am UTC](https://discuss.elastic.co/t/apm-agent-cannot-connect-to-remote-apm-server/209117/2 "2019-11-24T07:49:21Z")

</div>

> [@he11oworld](#):
>
> connect() failed (111: Connection refused) while connecting to upstream, client: 10.20.6.1, server: , request: "POST /intake/v2/events HTTP/1.1", upstream: "[http://10.19.72.106:8200/intake/v2/events](http://10.19.72.106:8200/intake/v2/events)", host: "172.20.11.188"

Please start by verifying your NginX configurations and see that the APM server is properly defined and reachable.  
Is `10.19.72.106` indeed the correct APM server IP? Is it reachable to your NginX server?  
I don't know a lot about NginX configuration, but in the error above it seems as if the server is not fully defined (`client: 10.20.6.1, server: , request: ...`), not sure if that is an indication for a problem.

---

<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:** [December 15, 2019, 3:49am UTC](https://discuss.elastic.co/t/apm-agent-cannot-connect-to-remote-apm-server/209117/3 "2019-12-15T03:49:24Z")

</div>

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