I would like to have the same error pages set up in the various sites hosted on my server. In other words, every server block should have the same error settings:

    error_page 404 403 /404.php;
    error_page 500 502 503 504 /50x.php;
    location = /404.php { root /home/main; allow all; }
    location = /50x.php { root /home/main; }

Then, inside the PHP of each error page, I check for $_REQUEST['HTTP_HOST'] and show the message accordingly. This is nice and easy to manage the error messages of all websites, where the design for this doesn't need to change much.

Issue: this works for HTML files and URLs without any specific extension. But if the non-existent URL (i.e, one which should produce a 404) contains a .file extension, it doesn't work.

example.com/nonexistent-folder/ ---> This works, shows the 404.php content example.com/nonexistent.gif ---> This doesn't work

The second URL shows in Chrome as ERR_INVALID_RESPONSE.

A CURL header check probably sheds some light:

        curl -I https://example.com/nonexistent.gif
        HTTP/1.1 404 Not Found
        Server: nginx
        Date: Sat, 18 Feb 2017 18:19:48 GMT
        Content-Type: application/x-httpd-php
        Content-Length: 1840
        Connection: keep-alive

The Content-type is PHP. Is this an issue? For a GIF non-existent file, does the Content-type need to be GIF? If not, what's causing this ERR_INVALID_RESPONSE in browsers?

  • Just to check, I forced a header in the 404.php of GIF in case the REQUEST_URI of the current request contained GIF. It doesn't work in this case at all as the PHP then doesn't execute as PHP. – PKHunter Feb 18 '17 at 18:39
  • 1
    Did you forget to set the Content-Type: in your PHP script? – Michael Hampton Feb 18 '17 at 19:18
  • What should I set it to? – PKHunter Feb 18 '17 at 20:07
  • Probably text/html or something along those lines. You are serving a 404 page, right? – Michael Hampton Feb 18 '17 at 20:12
  • Making the header text/html just before the echo $html; makes the file download, instead of executing and showing in the browser. Note that the $html is the variable that captures the contents of the 404 page, with variables that capture the various domains (logo, title, fonts, etc). – PKHunter Feb 19 '17 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.