I have setup an nginx server with php5-fpm. When I try to load the site I get a blank page with no errors. Html pages are served fine but not php. I tried turning on disp
None of the above answers worked for me - PHP was properly rendering everything except pages that relied on mysqli, for which it was sending a blank page with a 200 response code and not throwing any errors. As I'm on OS X, the fix was simply
sudo port install php56-mysql
followed by a restart of PHP-FPM and nginx.
I was migrating from an older Apache/PHP setup to nginx, and failed to notice the version mismatch in the driver for php-mysql
and php-fpm
.
The reason this problem occurs is because the fastcgi configurations in nginx do not function as required and in place or processing, they respond as html data. There are two possible ways in which you can configure your nginx to avoid this problem.
Method 1:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-fpm:
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
Method 2:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include snippets/fastcgi-php.conf;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
Both the methods would work properly, you can go ahead and take any one of them. They almost perform the same operations with a very few difference.
I had a similar error , but in combination with Nextcloud. So in case this did not work, try: Having a look at the Nginx manual.
I wrote a short C program that returns the environment variables passed from nginx to the fastCGI application.
#include <stdlib.h>
#include <fcgi_stdio.h>
extern char **environ;
int main(int argc, char **argv) {
char *envvar;
int i;
int count = 0;
while(FCGI_Accept() >= 0) {
printf("Content-type: text/html\n\n"
"<html><head><title>FastCGI Call Debug Tool</title></head>\n"
"<body><h1>FastCGI Call Debugging Tool</h1>\n"
"<p>Request number %d running on host <i>%s</i></p>\n"
"<h2>Environment Variables</h2><p>\n",
++count, getenv("SERVER_NAME"));
i = 0;
envvar = environ[i];
while (envvar != NULL) {
printf("%s<br/>",envvar);
envvar = environ[++i];
}
printf("</p></body></html>\n");
}
return 0;
}
Save this to a file, e.g. fcgi_debug.c
To compile it, first install gcc
and libfcgi-dev
, then run:
gcc -o fcgi_debug fcgi_debug.c -lfcgi
To run it, install spawn-fcgi
, then run:
spawn-fcgi -p 3000 -f /path/to/fcgi_debug
Then, change your nginx fcgi config to point to the debug program:
fastcgi_pass 127.0.0.1:3000;
Restart nginx, refresh the page, and you should see all the parameters appear in your browser for you to debug! :-)
Many users fall in this thread expecting to find a solution for blank pages being displayed while using nginx+php-fpm, me being one of them. This is a recap of what I ended up doing after reading many of the answers here plus my own investigations (updated to php7.2):
1) Open /etc/php/7.2/fpm/pool.d/www.conf
and check the value of parameter listen
.
listen = /var/run/php/php7.2-fpm.sock
2) Parameter listen
should match fastcgi_pass
parameter in your site configuration file (i,e: /etc/nginx/sites-enabled/default
).
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
3) Check the file actually exists:
$ file /var/run/php/php7.2-fpm.sock
/var/run/php/php7.2-fpm.sock: socket
4) If it doesn't exist that means php7.2-fpm is not running, so you need to restart it:
$ sudo /etc/init.d/php7.2-fpm restart
[ ok ] Restarting php7.2-fpm (via systemctl): php7.2-fpm.service.
With regard to the location
section in /etc/nginx/sites-enabled/default
:
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
Check the file snippets/fastcgi-php.conf
exists at location /etc/nginx/
:
$ file /etc/nginx/snippets/fastcgi-php.conf
/etc/nginx/snippets/fastcgi-php.conf: ASCII text
This file contains a list of variable definitions required by php7.2-fpm. The variables are defined directly or through an include of a separated file.
include fastcgi.conf;
This file is located at /etc/nginx/fastcgi.conf
and it looks like:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
...
fastcgi_param REDIRECT_STATUS 200;
nginx includes two possible parameter files: fastcgi_params and fastcgi.conf. The difference between both is the definition of variable SCRIPT_FILENAME
:
$ diff fastcgi_params fastcgi.conf
1a2
> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
To make a long story short, fastcgi.conf should always work. If for some reason you're set up is using fastcgi_params, you should define SCRIPT_FILENAME
:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Now reload nginx configuration:
$ sudo nginx -s reload
And check a php file is displayed correctly. For instance:
/var/www/html/test.php
<pre><?php var_export($_SERVER)?></pre>
Where /var/www/html
is the path to the document root.
If despite all that, you're still seeing a blank file, make sure your php.ini
has short_open_tag
enabled (if you're testing a PHP page with short tags).
I had a similar problem, nginx was processing a page halfway then stopping. None of the solutions suggested here were working for me. I fixed it by changing nginx fastcgi buffering:
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
After the changes my location
block looked like:
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
include fastcgi_params;
}
For details see https://www.namhuy.net/3120/fix-nginx-upstream-response-buffered-temporary-file-error.html