183

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"
Mike Glaz
  • 5,352
  • 8
  • 46
  • 73

18 Answers18

363

replace

include fastcgi_params;

with

include fastcgi.conf;

and remove fastcgi_param SCRIPT_FILENAME ... in nginx.conf

spacepille
  • 4,071
  • 2
  • 15
  • 5
  • 18
    This fixed it for me. The `.conf` has one extra configuration parameter missing from `_params`. – Malvineous Jul 06 '13 at 04:12
  • Adding this line (linking to /opt/nginx/conf/fastcgi.conf on my ubuntu 12.0.4) just worked for me, thanks ! – Kood Sep 03 '13 at 15:29
  • 8
    This (and, of course, `/etc/init.d/nginx restart`) also fixed it for me on Debian Testing after an `nginx` upgrade on 10 September 2014. – severin Sep 10 '14 at 15:46
  • 6
    This fixed it for me as well after updating nginx to 1.6.2 (Sept. 2014 update). Gotta love updates that break things randomly. – Mahn Sep 21 '14 at 17:10
  • Also, the new SCRIPT_FILENAME directive in fastcgi.conf breaks certain aliases so careful with that. – Mahn Sep 21 '14 at 17:24
  • 30
    Here is the backstory of `fastcgi_params` vs `fastcgi.conf`: http://blog.martinfjordvald.com/2013/04/nginx-config-history-fastcgi_params-versus-fastcgi-conf/ – Levite Sep 23 '14 at 09:48
  • Turns out defining SCRIPT_FILENAME as $request_filename within fastcgi.conf such as `fastcgi_param SCRIPT_FILENAME $request_filename;` works fine, it's just including fastcgi_params or defining it in-line what got broken. – Mahn Sep 25 '14 at 09:53
  • 4
    This code doesn't work - i don't know why; This code works, I don't know why. you saved me several hours. – Alex Jan 27 '15 at 09:23
  • 2
    This answer should be the best, because after upgrade from nginx 1.6.2 to 1.8.0 this all what should be done to make it work – Oleg Abrazhaev May 31 '15 at 11:04
  • one line command to fix `sudo find ./ -type f -exec sed -i 's/include fastcgi_params;/include fastcgi.conf;/g' {} \;` – Oleg Abrazhaev Jun 01 '15 at 16:39
  • 2
    This fixed it for me after upgrading from nginx 1.4.x to 1.8.x! – Nate Oct 25 '15 at 14:19
  • 2
    This fixed it for me. I upgraded from nginx 1.4 to 1.8.1 on ubuntu 14.04. Worked in 1.4 and stopped working in 1.8.1. Thanks, made my day. – emccracken Feb 13 '16 at 20:26
  • IN my case also => SCRIPT_FILENAME $fastcgi_script_name – user956584 Jul 25 '16 at 16:04
  • This works on macOS 10.12.5, just change "include fastcgi.conf;" to "include /path/to/fastcgi.conf;" [Julian H. Lam 's answer](https://stackoverflow.com/a/15424808/2296043) also works for me. – ahyong Jun 02 '17 at 17:11
  • It helped for me + I put full path to fastcgi.conf. Ubuntu 16.04.2 + nginx from official reps + php7 from official reps. – Dmytro Jun 06 '17 at 06:09
  • 1
    Thanks a lot! It also seems that using this instead of the frightening SCRIPT_FILENAME setting serves the website faster! – Gregordy May 26 '19 at 02:21
  • 3
    Updated link to backstory: https://blog.martinfjordvald.com/nginx-config-history-fastcgi_params-versus-fastcgi-conf/ – Jerther Jul 29 '20 at 19:30
  • Thanks for that @Jerther, just wanted to ask if someone actually had a working link :) – FrankyBoy Oct 14 '21 at 11:36
  • My Angle is here. thank you – Abadis Aug 04 '23 at 09:01
266

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.

masoud
  • 55,379
  • 16
  • 141
  • 208
Julian H. Lam
  • 25,501
  • 13
  • 46
  • 73
  • is $fastcgi_script_name a variable or a filename...I can't seem to find it. – Mike Glaz Mar 15 '13 at 15:55
  • 3
    your solution was a part of it. The other part is here http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm – Mike Glaz Mar 15 '13 at 17:12
  • 163
    the last line with 'SCRIPT_FILENAME' did the trick for me :) thanks – Stijn Leenknegt Mar 13 '14 at 22:36
  • 3
    thank you, that last line did the trick (it's not needed when using the 1.0 version of nginx that's shipped with Centos). I'd love to see the documentation around all of this improve. – Jorre Mar 27 '14 at 16:23
  • /path/to/www/dir - has to be included subfolder. for example if www dir is /var/www and your project is in folder "project" then it will look like: SCRIPT_FILENAME /var/www/project$fastcgi_script_name; – Dariux Nov 03 '14 at 13:08
  • 1
    You're welcome, glad to know it still helps people after all these years. However, please note @spacepile's updated answer below, which is probably better. – Julian H. Lam Feb 24 '15 at 23:38
  • Here (https://docs.moodle.org/dev/Install_Moodle_On_Ubuntu_with_Nginx/PHP-fpm) the documentation of moodle says that the `fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;` goes into `/etc/nginx/fastcgi_params`, in my case I installed nginx into opt directory, and in my version the `fastcgi_params` files is in this directory `/opt/nginx/conf/fastcgi_params`. – mariowise Jun 25 '15 at 13:17
  • +1 ...a post from 2013 and just saved my production site in 2016... This happened to me after upgrading nginx from 1.4.6 to 1.8.1 As much as I read beforehand, I never saw mention of this coming in nginx docs. – Slenny Mar 11 '16 at 09:06
  • 2
    I ran into the same issue and the solution for me was to add the SCRIPT_FILENAME as described here (without the slash /). But what drove me crazy is why I actually need to do this? We had another nginx install (older than 1.9) and there this line was not necessary. I found this https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/, and if you compare it with your fastcgi_params you'll see that most probably it not the same as the listed online version and you'll see that SCRIPT_FILENAME is not in there. Why's that? Go figure... – Daniel Dimitrov Apr 15 '16 at 05:20
  • Adding just this line worked for me `include /path/to/fastcgi_params;` – Ravi Soni Apr 08 '22 at 09:15
57

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;
Constant Meiring
  • 3,285
  • 3
  • 40
  • 52
55

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

Diego Pino
  • 11,278
  • 1
  • 55
  • 57
25

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!

Tim
  • 4,471
  • 5
  • 36
  • 42
  • 1
    +1 A seeming advantage of "fastcgi_param SCRIPT_FILENAME $request_filename;" is unlike "$document_root/$fastcgi_script_name", "$request_filename" adapts the path to Nginx's "alias" directive. See: http://nginx.org/en/docs/http/ngx_http_core_module.html#variables , http://nginx.org/en/docs/http/ngx_http_core_module.html#alias , and Martin Fjordvald's blog cited by Levit above ( http://blog.martinfjordvald.com/2013/04/nginx-config-history-fastcgi_params-versus-fastcgi-conf/ ). In my location block, it solved the blank page issue with php71-fpm and Nginx installed with Homebrew on El Capitan. – Slack Undertow Aug 08 '17 at 21:18
  • OMG that was it!! What a faff, then this one line solved it. – AEngineer Feb 09 '21 at 15:23
15

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

  • 1
    Note, you need headers and spawn-fcgi. On Debian/Ubuntu you can get it by `apt-get install spawn-fcgi libfcgi-dev`. – pevik Nov 20 '15 at 09:26
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
mariowise
  • 2,167
  • 2
  • 21
  • 34
CNSKnight
  • 567
  • 7
  • 14
6

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

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
Brad
  • 159,648
  • 54
  • 349
  • 530
nomulex
  • 521
  • 5
  • 5
  • 2
    Would you mind please providing a bit more detail on what exactly OP's problem is and how your answer solves OP's problem? – wookie919 Sep 14 '14 at 22:38
  • 1
    This solved the same problem for me on Ubuntu 15.04 with nginx 1.8.0 and php-fpm 5.6.4-4ubuntu6. I'd love to know what this is actually doing and why it isn't included with the default nginx config file, but I am at least happy that I've now codified it in a Dockerfile. – James Williams May 31 '15 at 15:21
  • 1
    Putting the value in `/etc/nginx/fastcgi_params` file would be more proper I think. – Arda Sep 02 '15 at 09:57
4

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.

McLovin
  • 1,455
  • 3
  • 19
  • 37
2

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.

Manish M Demblani
  • 910
  • 1
  • 10
  • 21
2
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

gounane
  • 381
  • 2
  • 6
1

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.

DarkMukke
  • 2,469
  • 1
  • 23
  • 31
  • 2
    there is an accepted answer with 200+ votes - the question is 5 years old.. I personally don't see this answer adding a lot to the question .. – treyBake Dec 17 '18 at 15:34
0

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.

jczaplew
  • 1,715
  • 1
  • 17
  • 21
0

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

Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37
0

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.

Community
  • 1
  • 1
Nahid Ali
  • 636
  • 3
  • 11
0

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;
}
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
Ruberandinda Patience
  • 3,435
  • 3
  • 20
  • 18
0

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.

DeDenker
  • 397
  • 3
  • 14
0

These steps solved my issue.

nginx version: nginx/1.17.10

Step1 : add these into default.conf. Please note that im using 7.4. if u are using other version change fastcgi_pass unix socket path to the correct pgp version.

location ~ \.php$ {
    root                    /var/www/html;
    fastcgi_pass            unix:/run/php/php7.4-fpm.sock;
    fastcgi_index           index.php;
    include                 fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Step 2: add these into fastcgi_params

fastcgi_param SCRIPT_FILENAME $request_filename;

Step 3:

restart nginx

Kimberlee Ho
  • 447
  • 1
  • 6
  • 23