Curl does not work from PHP on Nginx and localhost Curl does not work from PHP on Nginx and localhost curl curl

Curl does not work from PHP on Nginx and localhost


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\nginx\conf\nginx.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.