dongzhaobai5982
2015-10-08 19:39
浏览 468
已采纳

在Nginx和localhost上,Curl无法使用PHP

When I send the Curl request from local PHP script to the local server via SSL (running nginx 1.9.4 on Windows via WPN-XM) then the browser is "waiting" until it gets the 504 error. The PHP-fpm is probably dead, because the web server does not proccess any other request.

When I send the same request to the production server it works correctly.

When I send the Curl request from command line, then it works.

When I open the final resource in web browser then it works.

I have spent 4 hours googling and reading Stac Owerflow, but did not find any other situation like I have.

Thanks!


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://webserver.local/resource");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // just on local
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // just on local
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$response = curl_exec($ch);

图片转代码服务由CSDN问答提供 功能建议

当我通过SSL将Curl请求从本地PHP脚本发送到本地服务器时(在Windows上运行nginx 1.9.4) 通过WPN-XM)然后浏览器“等待”直到它得到504错误。 PHP-fpm可能已经死了,因为Web服务器不会处理任何其他请求。

当我向生产服务器发送相同的请求时,它可以正常工作。

当我从命令行发送Curl请求时,它就能正常工作。

当我在Web浏览器中打开最终资源时,它可以工作。 \ 我已经花了4个小时谷歌搜索和阅读Stac Owerflow,但没有找到像我这样的任何其他情况。

谢谢!


 &lt;?php 
 $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,“https://webserver.local/resource”); 
 ncurl_setopt($ ch,  CURLOPT_RETURNTRANSFER,TRUE); 
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers); 
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,FALSE);  //只在本地
curl_setopt上($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);  //只在本地
curl_setopt($ ch,CURLOPT_HEADER,true); 
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,“POST”); 
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ content); 
 $ response = curl_exec($ ch  ); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjia4097 2015-10-09 15:06
    已采纳

    This script will not work with the default configuration of WPN-XM v0.8.6, because there is only one php-cgi process listening in the background, but your example needs (at least) two of them. php-cgi is already used by the script doing the curl request and so Nginx is unable to forward it to php-cgi. That means you will first run into a blank page with a loading indicator and then hit the connection timeout.

    The underlying problem is that php-cgi does not automatically spawn new processes (when needed). The issue is discussed over here: https://github.com/WPN-XM/WPN-XM/issues/323

    There are two solutions:

    Update 03-2016:

    To solve the situation for the WPN-XM stack i've added php-cgi-spawn\spawn.exe by default. This allows to spawn multiple PHP daemons. The spawner will be used in PHP version below v7.1.

    PHP v7.1 will have this solution implemented and provide better FCGI MultiPlexing out-of-the-box.


    spawn-fcgi

    The first solution is a modification to start.bat. You would simply put spawn-fcgi in front of php-cgi, like so:

    spawn-fcgi -f "%_dir%\php-cgi.exe" -a 127.0.0.1 -p 9100 -C 6 -F 4 -P "%_dir%..\temp\php.pid"

    I have no clue where this tool is hiding, maybe there is a standalone download somewhere, but its possibly part of the lighttpd distribution for windows. I think i will compile it from source and make it available for WPN-XM.

    php upstream pool

    The second solution needs two small steps to get a PHP worker pool up and running.

    • The first step is to alter the start.bat file to start multiple php-cgi daemons, each listening at a different port. We add some more php-cgi starts:

      :start-php
          echo Starting PHP FastCGI...
      
          set PHP_FCGI_MAX_REQUESTS=0
          set PHP_FCGI_CHILDREN=4
      
          %HIDECONSOLE% %~dp0bin\php\php-cgi.exe -b 127.0.0.1:9100 -c %~dp0bin\php\php.ini
          %HIDECONSOLE% %~dp0bin\php\php-cgi.exe -b 127.0.0.1:9101 -c %~dp0bin\php\php.ini
          %HIDECONSOLE% %~dp0bin\php\php-cgi.exe -b 127.0.0.1:9102 -c %~dp0bin\php\php.ini
          %HIDECONSOLE% %~dp0bin\php\php-cgi.exe -b 127.0.0.1:9103 -c %~dp0bin\php\php.ini
      
    • The next step is to modify server\bin ginx\conf ginx.conf and activate the php_pool, instead of using the single upstream.

      Simply look for fastcgi_pass php; and change it to fastcgi_pass php_pool;.

    This change will activate the following upstream pool, which is already defined:

    upstream php_pool {
        server 127.0.0.1:9100 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9101 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9102 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9103 weight=1 max_fails=3 fail_timeout=20s;
    }
    

    That's all.

    Run start.bat and then your "curl post to localhost" example should work.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题