V2 - Caddy replied with php-fpm error message instead of 404 page

1. My Caddy version (caddy version):


2. How I run Caddy:

/usr/local/bin/caddy run --environ --config /etc/caddy/Caddyfile

a. System environment:

CentOS 8

d. My complete Caddyfile or JSON config:

domain.tld:443 {
     root * /var/www/public
     encode gzip
     tls echelon@domain.tld
     header / {
        # Enable HTTP Strict Transport Security (HSTS) to force clients to always
        # connect via HTTPS (do not use if only testing)
        Strict-Transport-Security "max-age=31536000;"
        # Enable cross-site filter (XSS) and tell browser to block detected attacks
        X-XSS-Protection "1; mode=block"
        # Prevent some browsers from MIME-sniffing a response away from the declared Content-Type
        X-Content-Type-Options "nosniff"
	Access-Control-Allow-Origin  *
	Access-Control-Allow-Methods "GET, POST, OPTIONS"
     php_fastcgi unix//var/run/php-fpm/www.sock
	 log {
       output file /var/log/caddy/access.log
       format single_field common_log
     handle_errors {
      @404 {
         expression {http.error.status_code} == '404'
      respond @404 "Oops 404 Try again"

3. The problem I’m having:

When trying to access non-exist-an php file, caddy serving the “File not found.” response from the php-fpm, instead of 404 erros

4. Error messages and/or full log output:

File not found.

5. What I already tried:

Adding handle_errors as above.

The question is how to serve 404 error page if php-fpm cannot find the php files ?

It’s because 404 is not a server error, so handle_errors is not invoked.

What you want to do will be easier in Caddy 2.1, probably.

Isn’t it the file not found is because caddy/server cannot fine the file?. or it’s because it’s already been passed into php-fpm?

With Caddy v2, when using php_fastcgi, reference index.php if the .php file does not exist:

This may be because you are getting unintended behavior.

1 Like

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