Search code examples
tomcatdockercurlcontainerskong

My docker container kong responds with "An invalid response was received from the upstream server"


I have kong running in my local linux machine on port 8000 and 8001 , Also i have kong which is running in one of my docker container on port 5000 and 5001.

my application is running on an another container on tomcat server on port 8811 (port fwd from 8811 to 8080)

i have configured my kong container with following API configs,

curl -i -X POST \
 --url http://localhost:5001/apis/ \
 --data 'name=sample' \
 --data 'uris=/samplewar' \
 --data 'upstream_url=http://localhost:8811/sample/hello'

and my linux machine kong with following API configs,

curl -i -X POST \
 --url http://localhost:8001/apis/ \
 --data 'name=sample' \
 --data 'uris=/samplewar' \
 --data 'upstream_url=http://localhost:8811/sample/hello' 

when i do curl to access my sample API i see the following error response from my Kong container,

curl -i -X GET --url http://localhost:5000/samplewar
HTTP/1.1 502 Bad Gateway
Date: Thu, 28 Dec 2017 08:25:26 GMT
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: kong/0.11.2

An invalid response was received from the upstream server

If i try and invoke the same API in my linux box, i see the following success response.

curl -i -X GET --url http://localhost:8000/samplewar

HTTP/1.1 200 OK
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 311
Connection: keep-alive
Server: Apache-Coyote/1.1
Date: Thu, 28 Dec 2017 08:25:33 GMT
X-Kong-Upstream-Latency: 2
X-Kong-Proxy-Latency: 0
Via: kong/0.11.2

<html>
<head>
<title>Sample Application Servlet Page</title>
</head>
<body bgcolor=white>
<table border="0">
<tr>
<td>
<img src="images/tomcat.gif">
</td>
<td>
<h1>Sample Application Servlet</h1>
This is the output of a servlet that is part of
the Hello, World application.
</td>
</tr>
</table>
</body>
</html>

What is the issue in here? why is my Kong container not giving a proper response?


Solution

  • After some trials and testing i was able to figure out the reason on why it was failing.

    Since the scenario in here where Kong is running in one container and sample application on Tomcat server in another container, upstream_url configuration in kong container has to be configured with the ip and port of the tomcat container. (i.e "upstream_url=http://172.17.0.4:8080/sample/hello" where 172.17.0.4 is the ipaddress of my tomcat container)

    curl -i -X POST \
    --url http://localhost:5001/apis/ \
    --data 'name=sample' \
    --data 'uris=/samplewar' \
    --data 'upstream_url=http://172.17.0.4:8080/sample/hello'
    

    This solved the issue, however i am not quite sure if this is optimal one.