You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 6.2升级至6.3后Varnish缓存出现503首字节超时问题

Symfony 6.2升级6.3后预发布环境Varnish 503超时问题

将Symfony应用从6.2版本升级至6.3版本后,本地运行一切正常,但在预发布(staging)环境出现异常:通过API登录或获取部分资源时,收到Varnish缓存服务器返回的503 First Byte Timeout错误。不过查看Kubernetes日志显示,应用自身返回的响应状态码为204,头部信息也正常。

为消除升级后的弃用提示,我修改了部分Normalizer类,以UserNormalizer为例:

升级前版本

<?php

declare(strict_types=1);

namespace App\Api\User\Serializer;

use App\Entity\User;
use App\Repository\CategoryRepository;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class UserNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
{
    public function __construct(
        private readonly NormalizerInterface $itemNormalizer,
        private readonly CategoryRepository $categoryRepository,
    ) {
    }

    /** @param User $object */
    public function normalize($object, $format = null, array $context = []): array|string
    {
        $object->categoryIds = $this->categoryRepository->findIdsWithJobsByUser($object);

        return $this->itemNormalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, $format = null, array $context = []): bool
    {
        return $data instanceof User;
    }

    public function hasCacheableSupportsMethod(): bool
    {
        return true;
    }
}

升级后版本

<?php

declare(strict_types=1);

namespace App\Api\User\Serializer;

use App\Entity\User;
use App\Repository\CategoryRepository;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final readonly class UserNormalizer implements NormalizerInterface
{
    public function __construct(
        private NormalizerInterface $itemNormalizer,
        private CategoryRepository $categoryRepository,
    ) {
    }

    /** @param User $object */
    public function normalize($object, $format = null, array $context = []): array|string
    {
        $object->categoryIds = $this->categoryRepository->findIdsWithJobsByUser($object);

        return $this->itemNormalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, $format = null, array $context = []): bool
    {
        return $data instanceof User;
    }

    public function getSupportedTypes(?string $format): array
    {
        return [
            '*' => false,
            User::class => true,
        ];
    }
}

我是按照Symfony官方文档及内置Normalizer示例进行修改的:移除了CacheableSupportsMethodInterface(及对应的hasCacheableSupportsMethod()方法),替换为getSupportedTypes()方法。

应用运行在Google Kubernetes Engine(GCP),通过Fastly交付。补充:部分资源(如静态页面,同样使用缓存且有类似getSupportedTypes()实现)可正常访问。

有没有人遇到过类似问题?


内容的提问来源于stack exchange,提问作者ppoz21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:30:26