nginx showing blank PHP pages

2019-01-20 22:27发布

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"

标签: nginx php
15条回答
爷、活的狠高调
2楼-- · 2019-01-20 22:33

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.

查看更多
唯我独甜
3楼-- · 2019-01-20 22:33

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-20 22:41

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.

查看更多
欢心
5楼-- · 2019-01-20 22:42

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

查看更多
混吃等死
6楼-- · 2019-01-20 22:43

replace

include fastcgi_params;

with

include fastcgi.conf;

and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf

查看更多
爷、活的狠高调
7楼-- · 2019-01-20 22:43

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

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
查看更多
登录 后发表回答