nginx showing blank PHP pages

2019-01-20 22:46发布

问题:

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"

回答1:

For reference, I am attaching my location block for catching files with the .php extension:

location ~ \.php$ {
    include /path/to/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}

Double-check the /path/to/fastcgi-params, and make sure that it is present and readable by the nginx user.



回答2:

replace

include fastcgi_params;

with

include fastcgi.conf;

and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf



回答3:

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;


回答4:

Many users fall in this thread expecting to find a solution for blank pages being displayed while using nginx+php5-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:

1) Open /etc/php5/fpm/pool.d/www.conf and check the value of parameter location.

location = /var/run/php5-fpm.sock

2) Parameter location should match fastcgi_pass parameter in your nginx.conf file.

fastcgi_pass unix:/var/run/php5-fpm.sock;

3) Check the file actually exists:

$ file /var/run/php5-fpm.sock
/var/run/php5-fpm.sock: socket

4) If it doesn't exist that means php5-fpm is not running, so you need to restart it:

$ sudo /etc/init.d/php5-fpm restart
[ ok ] Restarting php5-fpm (via systemctl): php5-fpm.service.


With regard to the location section:

location ~ \.php$ {
    include fastcgi_params;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
}

Check the file fastcgi_params exists at location /etc/nginx/:

$ file /etc/nginx/fastcgi_params 
/etc/nginx/fastcgi_params: ASCII text

Generally this file contains a list of variable definitions required by php5-fpm:

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 using fastcgi_params, you should define SCRIPT_FILENAME:

location ~ \.php$ {
    include fastcgi_params;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    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.



回答5:

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!



回答6:

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



回答7:

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


回答8:

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

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;


回答9:

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.



回答10:

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.



回答11:

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.



回答12:

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.



回答13:

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



回答14:

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


回答15:

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.



标签: nginx php