[nginx] nginx showing blank PHP pages

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 display_errors in php.ini but no luck. php5-fpm.log is not producing any errors and neither is nginx.

nginx.conf

server {
    listen 80;
    root /home/mike/www/606club;
    index index.php index.html;
    server_name mikeglaz.com www.mikeglaz.com;
    error_log /var/log/nginx/error.log;
    location ~ \.php$ {
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

EDIT

here's my nginx error log:

2013/03/15 03:52:55 [error] 1020#0: *55 open() "/home/mike/www/606club/robots.txt" failed (2: No such file or directory), client: 199.30.20.40, server: mikeglaz.com, request: "GET /robots.txt HTTP/1.1", host: "mikeglaz.com"

This question is related to nginx php

The answer is


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.


If you getting a blank screen, that may be because of 2 reasons:

  1. Browser blocking the Frames from being displayed. In some browsers the frames are considered as unsafe. To overcome this you can launch the frameless version of phpPgAdmin by

    http://-your-domain-name-/intro.php

  2. You have enabled a security feature in Nginx for X-Frame-Options try disabling it.


In case anyone is having this issue but none of the above answers solve their problems, I was having this same issue and had the hardest time tracking it down since my config files were correct, my ngnix and php-fpm jobs were running fine, and there were no errors coming through any error logs.

Dumb mistake but I never checked the Short Open Tag variable in my php.ini file which was set to short_open_tag = Off. Since my php files were using <? instead of <?php, the pages were showing up blank. Short Open Tag should have been set to On in my case.

Hope this helps someone.


This solved my issue:

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;
}

Make sure you've got this in /etc/nginx/fastcgi_params

fastcgi_param SCRIPT_FILENAME $request_filename;

Who knows why this isn't there already? The amount of time this must collectively waste!


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.


These hints helped me with my Ubuntu 14.04 LTS install,

In addition I needed to turn on the short_open_tag in /etc/php5/fpm/php.ini

$ sudo kate /etc/php5/fpm/php.ini

short_open_tag = On

$ sudo service php5-fpm restart
$ sudo service nginx reload

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 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! :-)


Also had this issue and finally found the solution here. In short, you need to add the following line to your nginx fastcgi config file (/etc/nginx/fastcgi_params in Ubuntu 12.04)

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

location ~ [^/]\.php(/|$) {
         fastcgi_pass unix:/PATH_TO_YOUR_PHPFPM_SOCKET_FILE/php7.0-fpm.sock;
         include fastcgi_params;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

Good luck


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


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.

  1. 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;
        }
    
  2. 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.


Add this in /etc/nginx/conf.d/default.conf:

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

This is my vhost for UBUNTU 18.04+apache+php7.2

server {
    listen 80;
    server_name test.test;
    root /var/www/html/{DIR_NAME}/public;
    location / {
        try_files $uri /index.php?$args;
    }
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
}

The last line makes it different than the other answers.


replace

include fastcgi_params;

with

include fastcgi.conf;

and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf