30 04 2016
How to connect to PHP-FPM directly to resolve issues with blank pages
Seeing blank pages when using Nginx and PHP-FPM can be frustrating. The following quick guide will show you how to connect to PHP-FPM directly so that you can determine if it is the reason for the blank pages.
I recommend trying some simple approaches first to see if the blank pages are caused by syntax errors or missing code/extensions. You can do this by checking your Nginx error logs and php error logs for clues and ensuring that PHP-FPM is up and running. If nothing obvious sticks out, then proceed with the guide.
In my case, my wordpress homepage and random pages where showing blank pages.
The nginx error log was showing
readv() failed (104: Connection reset by peer) while reading upstream
When I tried to request the website in chrome, it would crash and alert me to either wait or kill the page.
Using curl on the command line, I requested my homepage and the response code was 200 indicating success. However the response was truncated and this message displayed:
curl: (18) transfer closed with outstanding read data remaining
After combing through Nginx and PHP-FPM configuration options, testing PHP-FPM using sockets and TCP, the issue was unresolved.
The issue turned out to be a segfault in PHP when running the latest 7.0.6 version. That’s why some pages would work fine and some pages would return blank. After rolling back, the issue was resolved.
Nginx is also lenient with its response codes, in this case, since the PHP process ended abruptly, Nginx assumed it was successful and returned a 200 status code.
In the rest of this guide, I’ll show you how to test this yourself.
On Debian/Ubuntu, you will need to install the cgi-fcgi tool:
sudo apt-get install libfcgi0ldbl
On RHEL/Centos, cgi-fcgi is included in the fcgi package
yum --enablerepo=epel install fcgi
Now you can run the following bash script to call PHP-FPM directly:
#!/bin/sh SCRIPT_FILENAME=/path/to/site/index.php \ REQUEST_URI=/ \ QUERY_STRING= \ REQUEST_METHOD=GET \ cgi-fcgi -bind -connect /run/php/php7.0-fpm.sock
This can be used to render a wordpress homepage directly using PHP-FPM.
You will need to update the path to your PHP-FPM socket, or if you use TCP, you will need to replace that line with the following:
cgi-fcgi -bind -connect 127.0.0.1:9000
If you need to pass in more fastcgi parameters, check the ones that Nginx uses. Here are the default parameters:
fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param HTTPS $https if_not_empty;
When I ran fcgi tool, I was able to see the segfault in PHP with the message:
corrupted double-linked list
This is a great way to test PHP in isolation and determine if Nginx or PHP-FPM is the cause of your blank pages.