I have a setup with 10-20 virtual servers. I want to have an error file that ALL the 404 (or 403) errors from any virtual site will go and I want that file to be php not html.
And when I say all, I mean EVERYTHING. Like: http://mysite.bla/bla http://mysite.bla/bla.html http://mysite.bla/bla.php http://mysite.bla/bla/ http://mysite.bla/bla/bla.html http://mysite.bla/bla/bla.php (and any other combination).
I asked ChatGPT and any the info that it gave me was good for only few of those cases.
I have this code (for one of those 443-servers) but it does not work even locally on that server:
location ~ \.php$ {
try_files $uri /index.php$is_args$args; # Try to serve the PHP file; if it doesn't exist, return 404
include fastcgi_params; # Include standard FastCGI parameters
fastcgi_pass unix:/opt/homebrew/var/run/php-fpm.sock; # Specify the PHP-FPM socket
fastcgi_index index.php; # Default index file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Path to the script
}
error_page 404 /index.php; # or /error.php (maybe better)
# and maybe add all those: 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511;
location = /index.php { # or /error.php (maybe better)
ssi on; # do I need that?
auth_basic off; # do I need that?
include fastcgi_params; # Include standard FastCGI parameters
fastcgi_pass unix:/opt/homebrew/var/run/php-fpm.sock; # Specify the PHP-FPM socket
fastcgi_index index.php; # Default index file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Path to the script
internal; # Ensure this location block is internal only
}
location / {
try_files $uri $uri/ /index.php$is_args$args; # Redirect to index.php if file not found
}
So, what is the proper way to configure NginX (both for 1 server and for all) for a PHP error page?