Returning status code from php_fastcgi context

1. Caddy version (caddy version):

2.4.6

2. How I run Caddy:

Installed like officially recommended (cloudsmith). Ubuntu 20.04.
No changes or divergences from the official scripts.

d. My complete Caddyfile or JSON config:

(errorpages) {
	handle_errors {
		@404 expression `{http.error.status_code} == 404`
		@403 expression `{http.error.status_code} == 403`
		@generic_error expression `({http.error.status_code} != 403 && {http.error.status_code} != 404)`

		root * /var/www/handle_errors
		rewrite @404 /404.html
		rewrite @403 /403.html
		rewrite @generic_error /generic.html
		templates
		file_server
	}
}

www.example.com {
	root * /var/www/mautic

	import errorpages

	encode zstd gzip

	php_fastcgi 127.0.0.1:9000 {
		@error status 500 503
		handle_response @error {
			#copy_response_headers @error {
				#include status
			#}
			root * /var/www/handle_errors
			rewrite * /generic.html
			header Status {http.reverse_proxy.status_code}
			file_server
		}
	}

	file_server
}

3. The problem I’m having:

I can’t figure out how to return the proper HTTP Error Status (5xx) when my PHP scripts throw an error, using php_fastcgi. Currently I see HTTP Status 200 on the response with the given config above.

4. Error messages and/or full log output:

No errors. Only when playing with “copy_response_headers” I see:

Error during parsing: unrecognized directive: copy_response_headers - are you sure your Caddyfile structure (nesting and braces) is correct?

which might be caused by me using it wrong or some caddyfile/json thing

5. What I already tried:

Adding the status code with the “header” directive myself (see config).

The documentation on “handle_errors” tells:

Note that certain directives, for example reverse_proxy which may write a response with an HTTP status which is classified as an error, will not trigger the error routes.

I was glad to find out I can pass thru some directives within php_fastcgi and went that road but still can’t figure out the right way to use it. It somehow works because my errorpages snippet is used.

How can I make the status code “survive” so that the errorpages snippet can work with it?

Ah, sorry, that directive is only available on the latest pre-release, v2.5 beta: Release v2.5.0-beta.1 · caddyserver/caddy · GitHub

Oh, I’m ahead of my time :laughing:

So - is there another way at the moment to catch server errors (500-511) from upstream and handle them, keeping the status code?

(and btw: I noticed I don’t need the file_server directive within handle_response)

The file_server directive has a status option you can set to override the status it writes instead of 200:

The status code of a response is not a header, it’s a special field that is written before the headers. So special handling needs to be done for that.

		@error status 500 503
		handle_response @error {
			root * /var/www/handle_errors
			rewrite * /generic.html
			file_server {
				status {http.reverse_proxy.status_code}
			}
		}
1 Like

Hi Francis,

thanks, this works!

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