AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77488295
Accepted
mikyll98
mikyll98
Asked: 2023-11-15 21:54:36 +0800 CST2023-11-15 21:54:36 +0800 CST 2023-11-15 21:54:36 +0800 CST

Kong 502 Bad Gateway:“从上游服务器收到无效响应”

  • 772

长话短说

我无法通过 Kong 网关向本地 docker 容器(可通过 127.0.0.1:4002 访问)发送 HTTP 请求。

Kong网关组件:

  • 服务测试服务:url=http://127.0.0.1:4002;
  • 路线test_route : paths[]=/test, name=test_route;

向 Kong 代理请求:

$ curl -X GET http://localhost:8000/test
{
  "message":"An invalid response was received from the upstream server",
  "request_id":"9aba1e35ea54222d9fd27c4e2eeea850"
}

长版

我创建了一个简单的 Express 应用程序,它使用“Foo”和“Bar”响应 HTTP 请求,并将其进行了 Docker 化。

应用

索引.js:

const express = require('express');

const app = express();
var port = 4002;

app.use("/foo", (req, res) => {
    res.send("Foo");
});

app.use("/bar", (req, res) => {
  res.send("Bar");
});

app.use((req, res) => {
  res.send("Test unknown path");
});

app.listen(port, () => {
  console.log(`Foo-bar listening on port ${port}`)
})

Dockerfile:

FROM node:lts-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4002
CMD ["node", "index.js"]

构建并运行它后,我可以将 HTTP 请求发送到“localhost:4002”并正确获取响应。

例子:

$ docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED              STATUS                 PORTS                                             NAMES
fce1cc4e7b24   mikyll/test-service:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute      0.0.0.0:4002->4002/tcp                            nice_blackwell

$ curl -X GET http://localhost:4002
Test unknown path

$ curl -X GET http://localhost:4002/foo
Foo

$ curl -X GET http://localhost:4002/bar
Bar

Kong设置

我按照说明在 Docker 上设置 Kong Gateway并管理服务和路由:

  1. 我运行 Kong Gateway 容器:
curl -Ls https://get.konghq.com/quickstart | bash
  1. 测试连接:
$ curl --head localhost:8001
HTTP/1.1 200 OK
[...]
  1. 创建了一个服务:
$ curl -i -s -X POST http://localhost:8001/services --data name=test_service --data url='http://127.0.0.1:4002'
HTTP/1.1 201 Created
[...]

服务 JSON:

{
  "data":[
    {
      "retries":5,
      "tls_verify":null,
      "protocol":"http",
      "port":4002,
      "updated_at":1700054325,
      "created_at":1700054325,
      "connect_timeout":60000,
      "read_timeout":60000,
      "client_certificate":null,
      "host":"127.0.0.1",
      "path":null,
      "name":"test_service",
      "enabled":true,
      "tags":null,
      "write_timeout":60000,
      "ca_certificates":null,
      "id":"e79539ca-603b-4237-8abd-6ff07bd73160",
      "tls_verify_depth":null
    }
  ],
  "next":null
}
  1. 创建了一条路线:
$ curl -i -X POST http://localhost:8001/services/test_service/routes --data 'paths[]=/test' --data name=test_route

路由 JSON:

{
  "data":[
    {
      "https_redirect_status_code":426,
      "service":{
        "id":"e79539ca-603b-4237-8abd-6ff07bd73160"
      },
      "id":"4b04a262-6229-4361-9b2c-88f00b0fe6c2",
      "hosts":null,
      "path_handling":"v0",
      "updated_at":1700054404,
      "created_at":1700054404,
      "preserve_host":false,
      "headers":null,
      "methods":null,
      "sources":null,
      "destinations":null,
      "paths":[
        "/test"
      ],
      "snis":null,
      "name":"test_route",
      "tags":null,
      "request_buffering":true,
      "response_buffering":true,
      "protocols":[
        "http",
        "https"
      ],
      "regex_priority":0,
      "strip_path":true
    }
  ],
  "next":null
}

问题

当我尝试发送 HTTP 请求时,收到错误“从上游服务器收到无效响应。”:

$ curl -X GET http://localhost:8000/test
{
  "message":"An invalid response was received from the upstream server",
  "request_id":"9aba1e35ea54222d9fd27c4e2eeea850"
}

问题

我的配置(服务/路由)是否错误?我是否缺少一些设置步骤?

node.js
  • 2 2 个回答
  • 35 Views

2 个回答

  • Voted
  1. Best Answer
    Ponsakorn30214
    2023-11-15T22:28:35+08:002023-11-15T22:28:35+08:00

    问题主要在于如何将 Kong 与 Docker 上的服务连接起来。
    该服务http://127.0.0.1:4002意味着 Kong 将使用端口 4002 调用 127.0.0.1 (localhost),基本上调用 Kong 容器本身,而不是 Expressjs 应用程序。

    要解决此问题,请在同一用户定义的网络上运行 ExpressJS 应用程序和 Kong ,并使用 Docker 的服务名称(在本例中为 Nice_blackwell)调用该应用程序。
    也许创建一个撰写文件只是为了更容易管理。

    • 1
  2. mikyll98
    2023-11-15T22:41:21+08:002023-11-15T22:41:21+08:00

    相关 GitHub 线程

    正如Ponsakorn30214所说,请求失败是因为(显然)url=http://127.0.0.1:4002从 Kong docker 容器的角度来看意味着“localhost”。

    替代解决方案

    解决该问题的另一种可能性是使用两个容器都在其上运行的计算机的私有 IP 地址,但如果 IP 地址被重新分配,这种情况就会中断。

    修补

    使用以下命令修补 Kong 服务(假设主机 IP 地址为192.168.XXX.XXX):

    curl --request PATCH --url localhost:8001/services/test_service --data url=http://192.168.XXX.XXX:4002
    

    结果

    测试对网关的 HTTP 请求:

    $ curl -X GET http://localhost:8000/test/test
    Foo
    
    $ curl -X GET http://localhost:8000/test/foo
    Foo
    
    $ curl -X GET http://localhost:8000/test/bar
    Bar
    
    • 0

相关问题

  • 在 Docker 容器中找不到模块“build”

  • 单击 html canvas 元素后如何发送到 node.js 数据库?

  • 我的程序安装程序脚本在安装 Node.js 的 npm 后停止运行并且无法继续

  • Rest-Api动态图像路径和Express除非

  • bcrypt 模块无法异步工作

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve