Nginx 入口:向 url 添加参数时出现 502

发布于 2025-01-11 09:57:53 字数 3228 浏览 0 评论 0原文

我在 k8s 中部署了一个 Nodejs Rest 服务,通过 nginx 入口公开。它响应基本的获取,但当我传递 URL 参数时,我得到 502。

import express from "express";
const app = express();
app.get("/service-invoice", async (req, res) => {
  res.send(allInvoices);
}
app.listen(80);

其中 allInvoices 只是从 MongoDB 加载的发票对象的集合。

我使用以下入口配置将其部署到 k8s:

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: service-invoice-read
  namespace: ctx-service-invoice
  annotations:
    kubernetes.io/ingress.class: addon-http-application-routing
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /service-invoice-read(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: service-invoice-read
            port:
              number: 80

用curl 调用它:

curl localhost:30000/service-invoice-read/service-invoice

我得到一个有效的 json 响应。到目前为止,一切都很好。

但是,我也想通过 Id 访问这些对象。为此,我有以下代码:

app.get("/service-invoice/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const invoice = // code to load invoice by id from mongo
    res.send(invoice);
  } catch (e) {
    res.status(sc.NOT_FOUND).send(error);
  }
});

用curl调用此:

curl localhost:30000/service-invoice-read/service-invoice/e98e03b8-b590-4ca4-978d-270986b7d26e

导致 502 - Bad Gateway 错误。

我在 pod 日志中看不到任何错误,所以我很确定这是来自 nginx。

我真的不明白这是从哪里来的。我尝试过不使用 try/catch 在日志中查看它是否爆炸,但仍然没有乐趣。

这是我的入口日志,按照评论中的要求:

2022/03/03 18:45:21 [error] 847#847: *4524 upstream prematurely closed connection while reading response header from upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
2022/03/03 18:45:21 [error] 847#847: *4524 connect() failed (111: Connection refused) while connecting to upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
2022/03/03 18:45:21 [error] 847#847: *4524 connect() failed (111: Connection refused) while connecting to upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
10.42.1.1 - - [03/Mar/2022:18:45:21 +0000] "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1" 502 150 "-" "curl/7.68.0" 140 0.006 [ctx-service-invoice-service-invoice-read-80] [] 10.42.1.100:80, 10.42.1.100:80, 10.42.1.100:80 0, 0, 0 0.004, 0.004, 0.000 502, 502, 502 b78e6879fabe2d5947525a2b694b4b9f
W0303 18:45:21.529749       7 controller.go:1076] Service "ctx-service-invoice/service-invoice-read" does not have any active Endpoint.

有人知道我在这里做错了什么吗?

I have a nodejs rest service deployed in k8s exposed with nginx ingress. It responds to a basic get, but when I pass a URL parameter I get a 502.

import express from "express";
const app = express();
app.get("/service-invoice", async (req, res) => {
  res.send(allInvoices);
}
app.listen(80);

Where allInvoices is just a collection of invoice objects loaded from MongoDB.

I deploy this to k8s with the following ingress config:

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: service-invoice-read
  namespace: ctx-service-invoice
  annotations:
    kubernetes.io/ingress.class: addon-http-application-routing
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /service-invoice-read(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: service-invoice-read
            port:
              number: 80

Calling this with curl:

curl localhost:30000/service-invoice-read/service-invoice

I get back a valid json response. So far, so good.

But, I also want to access these objects by Id. To do that I have the following code:

app.get("/service-invoice/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const invoice = // code to load invoice by id from mongo
    res.send(invoice);
  } catch (e) {
    res.status(sc.NOT_FOUND).send(error);
  }
});

Calling this with curl:

curl localhost:30000/service-invoice-read/service-invoice/e98e03b8-b590-4ca4-978d-270986b7d26e

Results in a 502 - Bad Gateway error.

I can't see any errors in my pod's logs, so I'm pretty sure this is coming from nginx.

I don't really understand where this is coming from. I've tried without the try/catch to see in the logs if it blows up and still no joy.

Here's my ingress logs, as requested in the comments:

2022/03/03 18:45:21 [error] 847#847: *4524 upstream prematurely closed connection while reading response header from upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
2022/03/03 18:45:21 [error] 847#847: *4524 connect() failed (111: Connection refused) while connecting to upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
2022/03/03 18:45:21 [error] 847#847: *4524 connect() failed (111: Connection refused) while connecting to upstream, client: 10.42.1.1, server: _, request: "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1", upstream: "http://10.42.1.100:80/service-invoice/6220d042a95986f58c46356f", host: "localhost:30000"
10.42.1.1 - - [03/Mar/2022:18:45:21 +0000] "GET /service-invoice-read/service-invoice/6220d042a95986f58c46356f HTTP/1.1" 502 150 "-" "curl/7.68.0" 140 0.006 [ctx-service-invoice-service-invoice-read-80] [] 10.42.1.100:80, 10.42.1.100:80, 10.42.1.100:80 0, 0, 0 0.004, 0.004, 0.000 502, 502, 502 b78e6879fabe2d5947525a2b694b4b9f
W0303 18:45:21.529749       7 controller.go:1076] Service "ctx-service-invoice/service-invoice-read" does not have any active Endpoint.

Does anyone know what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-01-18 09:57:54

问题并不像看上去的那样。在这种情况下,配置工作正常。真正的问题是代码中存在一个错误,该错误被全局异常处理程序抑制而没有被记录。由于某种原因,这导致了 502——尽管我仍然不明白为什么我得到了这个确切的响应,但我并不是特别感兴趣。

全局异常处理程序的目的是在服务可能终止时保持服务运行。鉴于 k8s 中的服务终止是完全可以接受的,我删除了该处理程序并允许 pod 终止,这为我提供了有关正在发生的情况的更多信息。

The problem wasn't what it seemed. In this case, the configuration is working fine. The real problem is that there was an error in the code that was being suppressed by a global exception handler without being logged. For some reason, this resulted in a 502 -- though I still don't understand why I got that exact response but I'm not specifically interested.

The aim of the global exception handler is to keep the service running when it would otherwise die. Given that a service dying in k8s is perfectly acceptable I've removed this handler and allowed the pod to die, which gives me a lot more information about what is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文