代码之家  ›  专栏  ›  技术社区  ›  Shruthi Bhaskar

My docker container kong的响应为“从上游服务器接收到无效响应”

  •  0
  • Shruthi Bhaskar  · 技术社区  · 7 年前

    我在8000和8001端口的本地linux机器上运行kong,在5000和5001端口的docker容器中运行kong。

    我的应用程序正在tomcat服务器上的另一个容器上运行,端口8811(端口fwd从8811到8080)

    我已使用以下API配置配置了我的kong容器,

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

    我的linux机器kong具有以下API配置,

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

    当我使用curl访问示例API时,我看到来自Kong容器的以下错误响应,

    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
    

    如果我尝试在linux框中调用相同的API,我会看到以下成功响应。

    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>
    

    这里有什么问题?为什么我的香港集装箱没有给出正确的回应?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Shruthi Bhaskar    7 年前

    经过一些试验和测试,我能够找出它失败的原因。

    由于这里的场景是Kong在一个容器中运行,而Tomcat服务器上的示例应用程序在另一个容器中运行,因此必须使用Tomcat容器的ip和端口配置Kong容器中的上游url配置。 (即“upstream\u url= http://172.17.0.4:8080/sample/hello “其中172.17.0.4是tomcat容器的IP地址)

    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'
    

    这解决了问题,但我不太确定这是否是最佳的。

        2
  •  0
  •   SoftSkiller    2 年前

    我也有同样/类似的问题。

    基本上,您要做的是从容器连接到主机上的服务。因此,必须使用确切的IP地址和端口号配置上游url,而不是使用localhost。虽然公认的答案是设置确切的IP地址,这肯定有效,但由于主机的IP地址正在更改,使用解析为主机使用的内部IP地址的特殊DNS名称将比使用IP地址更容易。

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