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

在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);
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料