Fastcgi using unix socket with Caddy 2

1. My Caddy version (caddy -version):

v2.0 beta 6

2. How I run Caddy:

$ sudo ./caddy2_beta6_linux_amd64 start --adapter caddyfile --config /mnt/vagrant/Caddyfile 

I run caddy by manually starting it and loading/adapting a caddyfile. I created certificates for my local machine using mkcert and import them in the caddyfile.

a. System environment:

I’m running Caddy on a relatively clean Vagrant box running Ubuntu 18.04.

b. Command:

$ sudo ./caddy2_beta6_linux_amd64 start --adapter caddyfile --config /mnt/vagrant/Caddyfile 

d. My complete Caddyfile:

caddy2.test:443 {

  root * /var/www/caddy2/web

  tls /etc/ssl/bcaddy2.test.crt /etc/ssl/caddy2.test.key

  php_fastcgi unix:/var/run/php/php7.2-fpm.sock

3. The problem I’m having:

The problem I’m having is that the Caddyfile adapter in Caddy 2 does not seem to allow unix sockets as the PHP Fastcgi upstream.
I thought I’d ask for help here before creating an Issue on Github.

4. Error messages and/or full log output:

Caddy 2 beta 6 starts correctly:

2019/10/16 10:44:41 Caddy 2 admin endpoint listening on localhost:2019
2019/10/16 10:44:41 [INFO][cache:0xc0001f88c0] Started certificate maintenance routine
2019/10/16 10:44:41 [WARNING] Stapling OCSP: no OCSP stapling for [caddy2.test]: no OCSP server specified in certificate
2019/10/16 10:44:41 [INFO] tls: Cleaned up storage unit(s)
2019/10/16 10:44:41 [INFO][caddy2.test] Skipping automatic certificate management because one or more matching certificates are already loaded
2019/10/16 10:44:41 [INFO] Enabling automatic TLS certificate management for []
2019/10/16 10:44:41 [INFO] Enabling automatic HTTP->HTTPS redirects for [caddy2.test]
2019/10/16 10:44:41 Caddy 2 serving initial configuration
Successfully started Caddy (pid=1703)

When calling the website in my browser Caddy returns a 500 error and logs the following:

2019/10/16 10:53:09 [ERROR] [GET /index.php] making dial info: upstream unix:/var/run/php/php7.2-fpm.sock: invalid dial address unix:/var/run/php/php7.2-fpm.sock: address var/run/php/php7.2-fpm.sock: missing port in address

Of course I could set my fastcgi to listen on a TCP/IP socket, but since the webserver and fastcgi run on the same machine I’d prefer to use a unix socket.
I’d like to use the Caddyfile adapter in production as well to make sure the configuration is automatically loaded when starting/restarting/recovering from a crash.

Am I doing something wrong, or is this simply not implemented in Caddy 2 or the Caddyfile adapter (yet)?

1 Like

Thanks for trying Caddy 2 @stiffes!

You should be able to use unix sockets for fastcgi.

Instead of:

php_fastcgi unix:/var/run/php/php7.2-fpm.sock

Try this:

php_fastcgi unix//var/run/php/php7.2-fpm.sock

Notice the colon is gone.

The first forward slash delineates the network (socket) type from the address of the socket. The second one is the start of the file path.

(It’s not obvious for now, but this is documented here: Home · caddyserver/caddy Wiki · GitHub)


Thank @matt, that did the trick.

I thought I’d stick to the Caddyfile at first when trying out v2. I looked all over the Caddyfile adapter section and did not think to do a quick search for “socket” :man_facepalming:

Maybe this could be added to the php_fastcgi section, to help other like me who are not reading the whole document thoroughly.

Also: we love the move towards a completely open source approach. This was one thing holding us as a company back from trying Caddy with our projects. Now we are completely on board :tada:

1 Like

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