nginx 显示空白的 PHP 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15423500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:12:50  来源:igfitidea点击:

nginx showing blank PHP pages

nginxphp

提问by Mike Glaz

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.

我已经使用 php5-fpm 设置了一个 nginx 服务器。当我尝试加载网站时,我得到一个没有错误的空白页面。Html 页面可以正常提供,但 php 不能。我尝试在 php.ini 中打开 display_errors 但没有运气。php5-fpm.log 没有产生任何错误,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:

这是我的 nginx 错误日志:

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"

回答by spacepille

replace

代替

include fastcgi_params;

with

include fastcgi.conf;

and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf

并在 nginx.conf 中删除 fastcgi_param SCRIPT_FILENAME ...

回答by Julian H. Lam

For reference, I am attaching my locationblock for catching files with the .phpextension:

作为参考,我附上了我的location用于捕获带有.php扩展名的文件的块:

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.

仔细检查/path/to/fastcgi-params,并确保它存在并且可以被 nginx 用户读取。

回答by Constant Meiring

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)

也有这个问题,终于在这里找到了解决方案。简而言之,您需要将以下行添加到您的 nginx fastcgi 配置文件(Ubuntu 12.04 中的 /etc/nginx/fastcgi_params)

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

回答by Diego Pino

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

许多用户都在这个线程中,希望找到在使用nginx+php-fpm 时显示空白页面的解决方案,我就是其中之一。这是在阅读了这里的许多答案以及我自己的调查(更新到 php7.2)后我最终做了什么的回顾:

1) Open /etc/php/7.2/fpm/pool.d/www.confand check the value of parameter listen.

1) 打开/etc/php/7.2/fpm/pool.d/www.conf并检查参数的值listen

listen = /var/run/php/php7.2-fpm.sock

2) Parameter listenshould match fastcgi_passparameter in your site configuration file (i,e: /etc/nginx/sites-enabled/default).

2)参数listen应与fastcgi_pass站点配置文件中的参数匹配(即:)/etc/nginx/sites-enabled/default

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

3) Check the file actually exists:

3)检查文件是否实际存在:

$ 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-fpmis not running, so you need to restart it:

4) 如果不存在,则表示php7.2-fpm没有运行,因此您需要重新启动它:

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


With regard to the locationsection in /etc/nginx/sites-enabled/default:


关于 中的location部分/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.confexists at location /etc/nginx/:

检查文件是否snippets/fastcgi-php.conf存在于位置/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.

该文件包含 php7.2-fpm 所需的变量定义列表。这些变量是直接定义的,或者通过包含一个单独的文件来定义的。

 include fastcgi.conf;

This file is located at /etc/nginx/fastcgi.confand it looks like:

这个文件位于,/etc/nginx/fastcgi.conf它看起来像:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
...
fastcgi_param  REDIRECT_STATUS    200;

nginxincludes two possible parameter files: fastcgi_paramsand fastcgi.conf. The difference between both is the definition of variable SCRIPT_FILENAME:

nginx包括两个可能的参数文件:fastcgi_paramsfastcgi.conf。两者的区别在于变量的定义SCRIPT_FILENAME

$ diff fastcgi_params fastcgi.conf 
1a2
> fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

To make a long story short, fastcgi.confshould always work. If for some reason you're set up is using fastcgi_params, you should define SCRIPT_FILENAME:

长话短说,fastcgi.conf应该始终有效。如果由于某种原因您设置的是使用fastcgi_params,您应该定义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:

现在重新加载nginx配置:

$ sudo nginx -s reload

And check a php file is displayed correctly. For instance:

并检查一个 php 文件是否正确显示。例如:

/var/www/html/test.php

/var/www/html/test.php

<pre><?php var_export($_SERVER)?></pre>

Where /var/www/htmlis the path to the document root.

/var/www/html文档根目录的路径在哪里。

If despite all that, you're still seeing a blank file, make sure your php.inihas short_open_tagenabled (if you're testing a PHP page with short tags).

如果尽管如此,您仍然看到一个空白文件,请确保php.inishort_open_tag启用(如果您正在测试带有短标签的 PHP 页面)。

回答by Tim

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

确保你在 /etc/nginx/fastcgi_params 中有这个

fastcgi_param SCRIPT_FILENAME $request_filename;

fastcgi_param SCRIPT_FILENAME $request_filename;

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

谁知道为什么没有这个?这必须集体浪费的时间!

回答by tu-Reinstate Monica-dor duh

I wrote a short C program that returns the environment variables passed from nginx to the fastCGI application.

我写了一个简短的 C 程序,它返回从 nginx 传递给 fastCGI 应用程序的环境变量。

#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

将此保存到文件中,例如 fcgi_debug.c

To compile it, first install gccand libfcgi-dev, then run:

要编译它,首先安装gcclibfcgi-dev,然后运行:

gcc -o fcgi_debug fcgi_debug.c -lfcgi

To run it, install spawn-fcgi, then run:

要运行它,请安装spawn-fcgi,然后运行:

spawn-fcgi -p 3000 -f /path/to/fcgi_debug

Then, change your nginx fcgi config to point to the debug program:

然后,将您的 nginx fcgi 配置更改为指向调试程序:

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

重启nginx,刷新页面,你应该会看到浏览器中出现了所有参数供你调试!:-)

回答by CNSKnight

These hints helped me with my Ubuntu 14.04 LTS install,

这些提示帮助我安装了 Ubuntu 14.04 LTS,

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

此外,我需要转上short_open_tag/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

回答by nomulex

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

将此添加到/etc/nginx/conf.d/default.conf

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

回答by McLovin

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.

万一有人遇到这个问题,但以上答案都没有解决他们的问题,我遇到了同样的问题并且最难追踪它,因为我的配置文件是正确的,我的 ngnix 和 php-fpm 作业运行良好,并且任何错误日志都没有出现错误。

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 Onin my case.

愚蠢的错误,但我从未检查过我的 php.ini 文件中的 Short Open Tag 变量,该变量设置为short_open_tag = Off. 由于我的 php 文件使用的是<?而不是<?php,因此页面显示为空白。On在我的情况下,应该将 Short Open Tag 设置为。

Hope this helps someone.

希望这可以帮助某人。

回答by Manish M Demblani

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.

出现这个问题的原因是因为nginx中的fastcgi配置没有按要求运行,就位或处理,他们响应为html数据。您可以通过两种可能的方式配置 nginx 以避免此问题。

  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;
    }
    
  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. 方法二:

    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.

这两种方法都可以正常工作,您可以继续使用其中的任何一种。它们几乎执行相同的操作,只有很少的区别。