Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Nginx to call PHP file to handle 404 errors under php-fpm

Tags:

php

nginx

I'm trying to configure Nginx to send ALL 404s to a php file for futher processing. I have not got it working. With try_files I get a default 404 and without try_files I get no input file specified. This is what I have so far:

server {
    listen 192.168.100.44:80;

    location / {
        index  index.html;
    }
    root /var/www/test.example.com;

    error_page  404              /404.php;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #try_files      $uri = 404;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
like image 249
Gabe Avatar asked Oct 31 '12 16:10

Gabe


1 Answers

The answer was to add the line: fastcgi_intercept_errors on;

Then, in order to handle the 404 and possibly return a different status code from the PHP script: error_page 404 /404.php; Had to simply be changed to: error_page 404 = /404.php;

Credit to the kind people in the Nginx IRC channel who took a few moments of their time to show me the right direction.

like image 89
Gabe Avatar answered Oct 05 '22 00:10

Gabe