Caddy & PHP on Windows: Random 502 errors

1. Caddy version (caddy version):


2. How I run Caddy:

I am executing the Caddy.exe and php_cgi via a script simply calling caddy.exe run

a. System environment:

Windows 10

b. Command:

caddy run

d. My complete Caddyfile or JSON config:


root * ./

file_server browse
php_fastcgi php

3. The problem I’m having:

I am trying to run a PHP backend on Caddy, when I open up localhost:443/index.php it sometimes works perfectly fine and I am receiving what I want to receive but other times (roughly every 2nd to 3rd time I refresh the site) I get an 502 Bad Gateaway Error, it randomly works and randomly doesn’t

Note: It does not completely crash - refreshing/calling the site after getting the 502 error works again

4. Error messages and/or full log output:

	http.log.error  dialing backend: dial tcp: lookup php: no such host 
						"\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\""
						"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
						"gzip, deflate, br"
						"jenkins-timestamper-offset=-3600000; PHPSESSID=382f28e736a90e3ad12404c09822b662"
			   "err_trace":"reverseproxy.(*Handler).ServeHTTP (reverseproxy.go:441)"

5. What I already tried:

Following this 502 Bad Gateway or this examples/winphp at master · caddyserver/examples · GitHub
trying different PHP Versins (from 7 to 8)

In Caddy v2, there’s no longer a need for the php preset that v1 had for fastcgi.

Remove the php at the end of your php_fastcgi line.

Basically what’s happening is Caddy thinks php is a hostname for a fastcgi server, so it tries to connect to that, half the time, using the built-in load balancing features of the underlying proxy module.

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