0

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?

5
  • OK, found an error that ChatGPT filed to mention, I had a ? in the index.php$is_args$args part like this: index.php?$is_args$args But my question still stands. What is the best way to do a global error page? Commented Oct 2, 2024 at 12:34
  • Can you give an example of what does not work. For example, a URL you have tested, the result you obtained, and the result you expected. Commented Oct 3, 2024 at 9:07
  • Yes, the https:/myVSdomain/nonExistingfile.html (or php) works fine. But the https:/myVSdomain/nonExistingDir/ (with index.php or any other) loads the https:/myVSdomain/index.php as if it was /nonExistingDir/index.php (one problem with that is that relative links do not work here) Its not a huge problem, I can work with that, but it would be beter if it was transfered to the actual https:/myVSdomain/index.php url Commented Oct 3, 2024 at 13:36
  • To sum it up: I would like for the URL https:/myVSdomain/nonExistingDir/bla.html to "go" to https:/myVSdomain/index.php and not https:/myVSdomain/nonExistingDir/index.php even though its the same file. Commented Oct 3, 2024 at 13:37
  • Also, with this config, the error handling index.php has to be IN EVERY Virtual Server! I want to have ONE file in ONE location to handle everything! (I guess that if the definition wan not /index.php but https//myMainDomain/error.php would be better... so how do I do that?) Commented Oct 3, 2024 at 14:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.