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

如何解决Laravel调用WooCommerce API时的504网关超时问题

解决WooCommerce API 504网关超时问题的可行措施

问题详情

调用WooCommerce API更新商品图片时触发504 Gateway Time-out错误,Postman测试复现相同问题。尝试调整max_execution_time参数后无改善,本地环境出现该问题,暂未验证生产环境。

调用代码

public function updateProduct($ecart, $product, $wooId){

    //set_time_limit(3600); 
    //ini_set('max_execution_time', 3600);
    //ini_set('memory_limit', '2048M');
    $response = Http::withHeaders([
        'Content-Type' => 'application/json',
        ])->withBasicAuth($ecart->client_key, $ecart->client_secret)->put(
        $ecart->url.'/wp-json/wc/v3/products/'.$wooId,
        $product);
        //dd($response);
        dump('res',$response);
        $body = json_decode($response->getBody(), true);
       dd($body);

    return $response;

}

响应信息

Illuminate\Http\Client\Response^ {#9529
  #response: GuzzleHttp\Psr7\Response^ {#9583
    -reasonPhrase: "Gateway Time-out"
    -statusCode: 504
    -headers: array:6 [
      "Server" => array:1 [
        0 => "nginx"
      ]
      "Date" => array:1 [
        0 => "Fri, 27 Jan 2023 08:08:48 GMT"
      ]
      "Content-Type" => array:1 [
        0 => "text/html"
      ]
      "Content-Length" => array:1 [
        0 => "160"
      ]
      "Connection" => array:1 [
        0 => "keep-alive"
      ]
      "Strict-Transport-Security" => array:1 [
        0 => "max-age=63072000"
      ]
    ]
    -headerNames: array:6 [
      "server" => "Server"
      "date" => "Date"
      "content-type" => "Content-Type"
      "content-length" => "Content-Length"
      "connection" => "Connection"
      "strict-transport-security" => "Strict-Transport-Security"
    ]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream^ {#9580
      -stream: stream resource {@1032
        wrapper_type: "PHP"
        stream_type: "TEMP"
        mode: "w+b"
        unread_bytes: 0
        seekable: true
        uri: "php://temp"
        options: []
      }
      -size: null
      -seekable: true
      -readable: true
      -writable: true
      -uri: "php://temp"
      -customMetadata: []
    }
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar^ {#9554
    -cookies: []
    -strictMode: false
  }
  +"transferStats": GuzzleHttp\TransferStats^ {#9584
    -request: GuzzleHttp\Psr7\Request^ {#9577
      -method: "PUT"
      -requestTarget: null
      -uri: GuzzleHttp\Psr7\Uri^ {#9553
        -scheme: "https"
        -userInfo: ""
        -host: "test.myraidbox.de"
        -port: null
        -path: "/wp-json/wc/v3/products/10612"
        -query: ""
        -fragment: ""
        -composedComponents: "https://test.myraidbox.de/wp-json/wc/v3/products/10612"
      }
      -headers: array:5 [
        "Content-Length" => array:1 [
          0 => "1902"
        ]
        "User-Agent" => array:1 [
          0 => "GuzzleHttp/7"
        ]
        "Authorization" => array:1 [
          0 => "Basic Y2tfYWY4MTFhZmU4OWM2OWNjMWRjY2NiMGRlYjVmYjc2Y2UyODQxYTUyNzpjc182ZThkODlhZDIzNTVlYjA1ZTJhOTY5YThkODE3ZmQ4NWQ0NjY3MDA2"
        ]
        "Host" => array:1 [
          0 => "test.myraidbox.de"
        ]
        "Content-Type" => array:1 [
          0 => "application/json"
        ]
      ]
      -headerNames: array:5 [
        "content-length" => "Content-Length"
        "user-agent" => "User-Agent"
        "authorization" => "Authorization"
        "host" => "Host"
        "content-type" => "Content-Type"
      ]
      -protocol: "1.1"
      -stream: GuzzleHttp\Psr7\Stream^ {#9568
        -stream: stream resource {@1030
          wrapper_type: "PHP"
          stream_type: "TEMP"
          mode: "w+b"
          unread_bytes: 0
          seekable: true
          uri: "php://temp"
          options: []
        }
        -size: 1902
        -seekable: true
        -readable: true
        -writable: true
        -uri: "php://temp"
        -customMetadata: []
      }
    }
    -response: GuzzleHttp\Psr7\Response^ {#9583}
    -transferTime: 30.163235
    -handlerStats: array:38 [
      "url" => "https://test.myraidbox.de/wp-json/wc/v3/products/10612"
      "content_type" => "text/html"
      "http_code" => 504
      "header_size" => 200
      "request_size" => 2195
      "filetime" => -1
      "ssl_verify_result" => 0
      "redirect_count" => 0
      "total_time" => 30.163235
      "namelookup_time" => 0.004224
      "connect_time" => 0.007388
      "pretransfer_time" => 0.103427
      "size_upload" => 1902.0
      "size_download" => 160.0
      "speed_download" => 5.0
      "speed_upload" => 63.0
      "download_content_length" => 160.0
      "upload_content_length" => 1902.0
      "starttransfer_time" => 30.163099
      "redirect_time" => 0.0
      "redirect_url" => ""
      "primary_ip" => "::ffff:167.235.133.93"
      "certinfo" => []
      "primary_port" => 443
      "local_ip" => "::ffff:172.18.0.7"
      "local_port" => 57572
      "http_version" => 2
      "protocol" => 2
      "ssl_verifyresult" => 0
      "scheme" => "HTTPS"
      "appconnect_time_us" => 103359
      "connect_time_us" => 7388
      "namelookup_time_us" => 4224
      "pretransfer_time_us" => 103427
      "redirect_time_us" => 0
      "starttransfer_time_us" => 30163099
      "total_time_us" => 30163235
      "appconnect_time" => 0.103359
    ]
    -handlerErrorData: 0
  }
}

本地Nginx配置(.docker/nginx/default)

server {
    listen 80 default_server;
    listen 443 ssl;
    server_name backend.brandcanyon.de;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    ssl_certificate     /etc/ssl/certs/localhost.crt;
    ssl_certificate_key /etc/ssl/private/localhost.key;

    client_max_body_size 100M;

    fastcgi_read_timeout 600;
    proxy_read_timeout 600;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

        # for OPTIONS return these headers and HTTP 200 status
        if ($request_method = OPTIONS) {
          add_header Access-Control-Allow-Methods "*";
          add_header Access-Control-Allow-Headers "*";
          add_header Access-Control-Allow-Origin "*";
          return 200;
        }
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ \.php$ {
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

可行解决措施

1. 优化图片上传方式

  • 避免在PUT请求的JSON中嵌入Base64图片:Base64会让请求体积膨胀30%以上,拖慢处理速度。改用WooCommerce的商品图片单独上传接口(POST /wp-json/wc/v3/products/{product_id}/images),先上传图片获取ID,再通过PUT请求将图片ID关联到商品。
  • 压缩图片尺寸:确保单张图片文件大小不超过2MB,减少服务器处理压力。

2. 延长Guzzle请求超时时间

从响应的total_time: 30.163235可以看出,请求刚好在30秒左右超时(Guzzle默认超时为30秒)。手动设置更长的超时时间:

$response = Http::withHeaders([
    'Content-Type' => 'application/json',
])->withBasicAuth($ecart->client_key, $ecart->client_secret)
->timeout(120) // 总请求超时时间设为120秒
->connectTimeout(30) // 连接超时设为30秒
->put(
    $ecart->url.'/wp-json/wc/v3/products/'.$wooId,
    $product
);

3. 调整目标WooCommerce服务器配置

504错误本质是目标服务器的网关(Nginx)等待后端响应超时,需在WooCommerce所在服务器修改:

  • 增加Nginx的proxy_read_timeout、fastcgi_read_timeout参数到120秒以上;
  • 检查PHP配置:确保max_execution_time≥120、memory_limit≥512M、post_max_size和upload_max_filesize≥10M;
  • 查看WooCommerce日志:在WordPress后台「WooCommerce→状态→日志」中检查是否有图片处理相关的PHP错误,定位阻塞点。

4. 网络排查与优化

  • 测试本地到WooCommerce服务器的网络延迟:用ping test.myraidbox.de或traceroute test.myraidbox.de检查是否有高延迟或丢包,必要时切换网络环境;
  • 临时关闭SSL验证排查:若SSL握手慢导致超时,可临时关闭验证(仅用于测试,生产环境禁用):
$response = Http::withHeaders([
    'Content-Type' => 'application/json',
])->withBasicAuth($ecart->client_key, $ecart->client_secret)
->withoutVerifying()
->put(
    $ecart->url.'/wp-json/wc/v3/products/'.$wooId,
    $product
);

5. 生产环境预验证

  • 先在生产环境做小范围测试:上传单张压缩后的小尺寸图片,观察是否超时;
  • 对比环境差异:生产环境通常网络更稳定、服务器配置更高,大概率不会出现本地的超时问题,但仍需提前验证。

补充说明

本地Nginx配置中的proxy_read_timeout和fastcgi_read_timeout已设为600秒,但该配置仅作用于本地后端服务,而当前504错误来自WooCommerce服务器的网关,因此无需调整本地Nginx配置,重点在目标服务器。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:56:06