cURL冻结Apache并在404错误时超时



我有一个关于cURL请求的非常奇怪的问题。基本上我有一些代码,我用它来包装一个遗留的PHP应用程序在Symfony2 (http://symfonybricks.com/it/brick/wrap-legacy-php-code-by-a-symfony2-application)。它们是基本的cURL命令,应该拉出网页并显示它。然而,我注意到以下几点:

  • 如果我对现有文件进行查询,它工作正常
  • 如果我对一个不存在的文件进行查询,cURL超时(因为我已经设置了8秒的超时时间),并且在此期间,它冻结了整个服务器,我无法从任何其他设备访问我的网站,直到它实际超时。

下面是我的代码:

/**
 * @Route("/", defaults={"controller" = "index.php", "controller2" = "", "controller3" = ""})
 * @Route("/{controller}", defaults={"controller2" = "index.php", "controller3" = ""})
 * @Route("/{controller}/", defaults={"controller2" = "index.php", "controller3" = ""})
 * @Route("/{controller}/{controller2}", defaults={"controller3" = ""})
 * @Route("/{controller}/{controller2}/", defaults={"controller3" = ""})
 * @Route("/{controller}/{controller2}/{controller3}", defaults={"controller3" = "index.php"})
 */
public function getLegacyResourceAction($controller, $controller2, $controller3, Request $request)
{

    $path_to_legacy_code = "http://XXX/";
    $originalController = $request->getPathInfo();
    $originalQueryString = $request->getQueryString();
   $url = "{$path_to_legacy_code}{$originalController}?{$originalQueryString}";
    //open connection
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    //Timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
    curl_setopt($ch, CURLOPT_TIMEOUT, 8); 
    curl_setopt($ch,  CURLOPT_MAXREDIRS, 5);       
    $stderr = fopen("{$this->container->getParameter('kernel.root_dir')}/logs/curl.txt", "a");
    curl_setopt($ch, CURLOPT_STDERR, $stderr);

    //echo "Login stuff in ".$this->container->getParameter('kernel.root_dir')."/logs/curl.txt";

    curl_setopt($ch, CURLOPT_COOKIESESSION, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');  
    $result = curl_exec($ch);

    if (false === $result) {
        echo curl_error($ch);
        exit;
    }
    curl_close($ch);
    fclose($stderr);
    $Response = new Response($result);

    return $Response;

这是我从一个有效的响应中得到的日志:

* About to connect() to www.xxx.com port 80 (#0)
*   Trying 178.32.223.113...
* connected
* Connected to www.xxx.com (178.32.223.113) port 80 (#0)
> GET /web/legacy/index.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefo$
Host: www.xxx.com
Accept: */*
Cookie: idto=116; PHPSESSID=a159rcjvh0fk6otukqqq9bkrd5
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
< Date: Sat, 05 Sep 2015 04:26:32 GMT
< Server: Apache/2.2.22 (Debian)
< X-Powered-By: PHP/5.5.28-1~dotdeb+7.1
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html
<
* Connection #0 to host www.xxx.com left intact
* Closing connection #0

下面是无效响应的代码:

* About to connect() to www.xxx.com port 80 (#0)
*   Trying 178.32.223.113...
* connected
* Connected to www.xxx.com (178.32.223.113) port 80 (#0)
> GET /web/legacy/whatever.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefo$
Host: www.xxx.com
Accept: */*
Cookie: idto=116; PHPSESSID=a159rcjvh0fk6otukqqq9bkrd5
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* Operation timed out after 8001 milliseconds with 0 bytes received
* Closing connection #0
* About to connect() to www.xxx.com port 80 (#0)
*   Trying 178.32.223.113...
* connected
* Connected to www.xxx.com (178.32.223.113) port 80 (#0)
> GET /web/legacy/legacy/whatever.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefo$
Host: www.xxx.com
Accept: */*
Cookie: idto=116; PHPSESSID=a159rcjvh0fk6otukqqq9bkrd5
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* additional stuff not fine transfer.c:1037: 0 0
* Operation timed out after 8001 milliseconds with 0 bytes received
* Closing connection #0
* About to connect() to www.xxx.com port 80 (#0)
*   Trying 178.32.223.113...
* connected
* Connected to www.xxx.com (178.32.223.113) port 80 (#0)
> GET /web/legacy/legacy/legacy/whatever.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefo$
Host: www.xxx.com
Accept: */*

curl_exec($ch);直到超时才执行,不给我检查404错误或其他东西的机会。我已经找了好几天了,还没找到。

谢谢你!

EDIT:

好的,我通过删除这一行来解决这个问题:

curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');

现在它给出一个正确的404错误。我不知道为什么没有这条线它还能工作。问题是,如果没有这一行,我就会破坏遗留代码中的一些会话内容!

EDIT 2:

I've changed:

  • curl_setopt($ch, curlopt_cookisession, 0);

  • curl_setopt($ch, curlopt_cookisession, 1);

现在它的工作与404错误和我的会话。我不知道为什么。

EDIT:

好吧,我想我终于明白是怎么回事了:

  • 在每个cURL请求中,它写入cookie.txt文件
  • 如果有人查询404 url,在超时之前,它会锁定cookie.txt文件,这就是为什么整个事情似乎被冻结。

根据我的理解,最好的解决方案是为每个用户生成不同的cookie.txt文件,以防止文件锁定。

我终于找到了这个底部,原来我有两个问题:

  • 没有超时配置,所以如果我试图查询一个不存在的网页,它会无限循环。
  • 我对每个人都使用相同的cookie文件,如果有人被困在前面提到的循环中,它将文件锁定cookie文件,阻止其他用户访问该网站。

相关内容

  • 没有找到相关文章

最新更新