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条回答
可以哭但决不认输i
2楼-- · 2019-01-20 22:44

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

查看更多
Summer. ? 凉城
3楼-- · 2019-01-20 22:46

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.

查看更多
走好不送
4楼-- · 2019-01-20 22:46

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;
}
查看更多
Summer. ? 凉城
5楼-- · 2019-01-20 22:48

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
查看更多
Lonely孤独者°
6楼-- · 2019-01-20 22:48

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.

查看更多
成全新的幸福
7楼-- · 2019-01-20 22:49

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.

查看更多
登录 后发表回答