Custom Nginx Error Pages, PHP + FPM

I am trying to create some custom error pages but cannot make 500 work.

I have the following configuration:

server { listen 80; root /var/www/devsite; index index.php; server_name devsite; error_page 403 = /error.php?code=403; error_page 404 = /error.php?code=404; error_page 500 = /error.php?code=500; location / { try_files $uri =404; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

At first I thought it could be because it is a PHP file, so I changed:

 error_page 500 = /error.php?code=500; 

To static page:

 error_page 500 /500.html 

But it still just shows a blank page with a 500 response code when I interrupt some PHP code to call it.

Then I tried to make this the last rule inside location ~ \.php$ , but the same thing will happen. Any ideas why the custom page 500 won't work?

I also notice that if you try to access a file with a deny access that has the extension .php, it will not show the user page 403 and will not show the embedded page. Is there a way to make a rule also closed .php files?

+6
source share
1 answer

The piece you are missing is fastcgi_intercept_errors . Without this directive, Nginx will not touch responses from CGI servers if they are valid:

Determines whether FastCGI server responses with codes greater than or equal to 300 should be sent to the client or redirected to nginx for processing using the error_page directive.

You need to put the following in your PHP processing location:

 fastcgi_intercept_errors on; 

On one side, you may not need = to your error_page lines (depending on your intended use). This syntax instructs Nginx to use the response code returned from the PHP script that you point to instead of the source response code:

If the error response is processed by the proxy server or FastCGI server, and the server can return different response codes (for example, 200, 302, 401 or 404) ... respond with the code that it returns.

+14
source

All Articles