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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站