【问题标题】:Curl request to same server intermittently times out对同一服务器的卷曲请求间歇性超时
【发布时间】:2013-12-03 01:48:47
【问题描述】:

我在 php 中开发了一个代理服务,它使用 php-curl 在同一服务器上请求另一个 url。有时效果很好,但有时需要很长时间,而且很多时候会超时。

此应用程序不使用会话,因此不会是会话锁定问题。我在调用 curl_exec 之前尝试了 session_write_close() 作为测试,它没有任何区别。

这种不一致行为的原因是什么?我希望它能够立即响应,因为它所做的唯一工作就是提供 302 重定向。我在下面粘贴了我的代理功能。

protected function proxy( $pURL,  $opts = array() ){

    $defaults = array(
        'headers' => array(),
        'follow' => true,
        'return' => false,
        'return_headers' => false,
        'referer' => fp_get_config( 'referer_override' ),
        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        'timeout' => 30,
        'connect_timeout' => 10,
        'fresh_connect' => false
    );

    $options = array_merge( $defaults, $opts );

    extract( $options );

    $c = curl_init( $pURL );

    curl_setopt_array( $c, array(
        CURLOPT_HEADER => $return_headers,
        CURLOPT_USERAGENT => $user_agent,
        CURLOPT_REFERER => $referer,
        CURLOPT_CONNECTTIMEOUT => $connect_timeout,
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => $follow,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_FRESH_CONNECT => $fresh_connect,
        CURLOPT_AUTOREFERER => true
    ));

    //session_write_close();

    $response = curl_exec( $c );

    if ( $response === false )
        die("Proxy Error: " . curl_error( $c ) );


    curl_close( $c );

    if ( $return )
        return $response;
    else {

        if ( $return_headers ){

            list( $headerblock, $body ) = explode("\r\n\r\n", $response, 2);
            $headers = explode("\r\n", $headerblock );

            foreach( $headers as $header )
                header( $header );

            echo $body;

        } else
            echo $response;
    }

    exit;

}

【问题讨论】:

  • 我会尝试设置CURLOPT_MAXREDIRS
  • 我不会让 curl 在这个特定的实例中处理重定向,这意味着 $follow 总是会是错误的,因为我要将标头直接代理到浏览器。如果这是一个问题,我想它会始终如一地发生,而不是间歇性地发生。
  • 您将需要提供一些挂起的 URL...

标签: curl nginx php


【解决方案1】:

原来发生的事情是这样的:我们配置了两个负载平衡器以实现高可用性。每当一磅向另一磅发出 curl 请求时,它就会被防火墙阻止,从而导致连接超时。修复防火墙规则解决了这个问题。

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2011-03-13
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多