Caddy & PHP on Windows: Random 502 errors

1. Caddy version (caddy version):

v.2.2.1

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:

localhost:443

root * ./

templates
file_server browse
php_fastcgi 127.0.0.1:6545 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 
	
			{
			   "request":{
				  "remote_addr":"[::1]:57139",
				  "proto":"HTTP/2.0",
				  "method":"GET",
				  "host":"localhost:2015",
				  "uri":"/index.php",
				  "headers":{
					 "Sec-Ch-Ua":[
						"\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\""
					 ],
					 "Sec-Ch-Ua-Mobile":[
						"?0"
					 ],
					 "User-Agent":[
						"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
					 ],
					 "Sec-Fetch-Site":[
						"cross-site"
					 ],
					 "Accept-Encoding":[
						"gzip, deflate, br"
					 ],
					 "Cookie":[
						"jenkins-timestamper-offset=-3600000; PHPSESSID=382f28e736a90e3ad12404c09822b662"
					 ],
					 "Cache-Control":[
						"max-age=0"
					 ],
					 "Upgrade-Insecure-Requests":[
						"1"
					 ],
					 "Accept":[
						"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
					 ],
					 "Sec-Fetch-Mode":[
						"navigate"
					 ],
					 "Sec-Fetch-User":[
						"?1"
					 ],
					 "Sec-Fetch-Dest":[
						"document"
					 ],
					 "Accept-Language":[
						"en-GB,en;q=0.9,en-US;q=0.8,de;q=0.7"
					 ]
				  },
				  "tls":{
					 "resumed":false,
					 "version":772,
					 "cipher_suite":4865,
					 "proto":"h2",
					 "proto_mutual":true,
					 "server_name":"localhost"
				  }
			   },
			   "duration":2.2779794,
			   "status":502,
			   "err_id":"6g4d3dep2",
			   "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.