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

Symfony集成Stripe Webhook遇connection reset by peer问题求助

Symfony + Docker 集成Stripe Webhook连接重置问题解决

核心问题原因

你把Stripe CLI的转发目标指向了phpfpm:9000,但php-fpm是FastCGI服务,并非HTTP/HTTPS服务器,无法直接处理Stripe发送的HTTP POST请求,导致连接被重置。正确的做法是将请求转发到负责处理HTTP请求的Nginx服务。

具体修复步骤

1. 修改Stripe CLI的转发目标

更新docker-compose.yml中stripe-cli服务的command配置,把转发地址改成Nginx服务的HTTP/HTTPS端口:

stripe-cli:
    image: stripe/stripe-cli
    # 改为http://nginx:80(HTTP)或者https://nginx:443(HTTPS,配合--skip-verify忽略自签证书)
    command: ["listen", "--forward-to", "http://nginx:80/webhook/stripe", "--api-key", "secret", "--skip-verify"]
    volumes:
        - ./:/var/www/app:cached
    depends_on:
        - phpfpm
        - nginx # 新增依赖,确保Nginx先启动

2. 确认Nginx配置正确转发请求

检查.docker/nginx/default.conf,确保所有请求(包括/webhook/stripe)都能转发到php-fpm。典型的Symfony Nginx配置应包含:

server {
    root /var/www/app/public;
    # ... 其他SSL、端口配置

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass phpfpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }
}

3. 重启服务并测试

  • 重启Stripe CLI服务:
    docker-compose restart stripe-cli
    
  • 发送测试事件验证:
    docker-compose exec stripe-cli trigger checkout.session.completed
    
  • 查看Symfony日志(var/logs/dev.log)确认事件是否被正确接收,同时检查数据库中对应订单的状态是否更新。

额外优化建议

  • 避免直接使用getenv(),改用Symfony的参数组件更可靠:
    // 替换getenv('STRIPE_SECRET')为
    $this->getParameter('stripe.secret');
    // 需在config/services.yaml中配置参数:
    // parameters:
    //     stripe.secret: '%env(STRIPE_SECRET)%'
    //     stripe.webhook_secret: '%env(STRIPE_WH_SECRET)%'
    
  • Webhook处理中exit()可能导致Symfony无法正确返回响应,建议改为返回Response对象:
    catch(UnexpectedValueException $e) {
        $log->warning('Invalid payload: ' . $e->getMessage());
        return new JsonResponse(['error' => 'Invalid payload'], Response::HTTP_BAD_REQUEST);
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 01:25:56