json 通过 Ajax 请求:查询非常快,但返回响应非常慢

json requested via Ajax: queries are very fast but response is returned very slowly

注意这个

这是一个 ajax 请求。

如你所见,我在结果中写了duration,它是所有在api后端执行的查询的持续时间。

响应长度为11 KByte,所以不是响应权重问题。

但是您可以看到服务器正在为页面提供服务 5 seconds

我用的是nginx,在这台服务器上(是单项目开发VPS),没有流量,没有并发问题。

后端是在 laravel 8 中制作的,它只做这个:

    $start = microtime(true);
    $data = $this->articleRepository->getProducts($request->all());
    $duration = microtime(true) - $start;

    return response()->json([
        'status' => 'success',
        'data' => $data,
        'debug' => [
            'duration' => $duration
        ]
    ]);

我尝试用

替换laravel魔法
    $json = json_encode([
        'status' => 'success',
        'data' => $data,
        'debug' => [
            'duration' => $duration
        ]
    ]);

    return $json;

但它需要相同的时间。所以我认为这是服务器端的问题。

By the way, please note that dev VPS is a debian 11 machine in my local network. We already verified that up/down band is well over 350Mbits/secs, symmetric, and stable.

我无法诊断它,我有 VPS 的根访问权限,但我不知道是什么导致如此缓慢

有什么想法吗?

在这个非常具体的案例中,这是一个 DNS 问题。 “localhost”解析导致了一系列问题和延迟。我们将所有指向从 'localhost' 移动到 127.0.0.1 并解决了所有问题。

Note for future Googlers for Laragon and/or Xamp developers: we discovered accidentally that changing localhost to 127.0.0.1 when configuring redis in the laravel .env variabile fixes a huge amount on a windows machine when using both Laragon and Xamp