Nginx 404 document with PHP files

Hello everyone,
I want to make it so whenever someone tries to access a page that doesn’t exist, he gets redirected to lets say 404.php
I could get it done for the following examples:

but cannot get it done for:
http://l2ovc.com/bla.php

I get a blank page with “File not Found” text.
Here is my server configuration:

server {
	listen 80;
	server_name l2ovc.com;
	root /usr/share/nginx/html;
	location / {
		index index.php;
	}
	error_page 404 /404.php;
	error_page 500 502 503 504 /50x.html;
	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
		include fastcgi_params;
	}
}

What changes should I make to it to make it work?

ulthane,

I know nothing about nginx except that it attempts to emulate Apache.

Very simply, the Apache solution is

ErrorDocument 404 /404.php

If that doesn’t work for you (it should), then trying to use mod_rewrite code will not likely be of any benefit. If that’s the case, may I recommend that you go back to the website where you got the nginx and look for their support forum and someone there should know how to implement the ErrorDocument function (check if the requested file exists and, if not, redirect to the specified file) for you.

Regards,

DK

Their official forum seems to be really dead…
Anyways I posted there we’ll see if i’ll get anything, the question is still valid here too incase someone knows…

Syntax for nginx is different as you see I use
error_page 404 /404.php;

But that still doesnt catch .php files…

Managed to get it solved with the following server block:

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;
	server_name l2ovc.com www.l2ovc.com;
	root /usr/share/nginx/html;
	index index.php;
	error_page 404 /404.php;
	error_page 500 502 503 504 /50x.html;
	location ~ \.php$ {
		try_files $uri /404.php;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.