Help with Caddy and fastcgi with environment variables

1. Caddy version (caddy version):

v2.5.1 h1:bAWwslD1jNeCzDa+jDCNwb8M3UJ2tPa8UZFFzPVmGKs=

2. How I run Caddy:

a. System environment:

Linux 5.10.0-8-amd64 Debian 5.10.46-5 x86_64 GNU/Linux

b. Command:

systemctl restart caddy 

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:


waifuism.life {
        reverse_proxy :4000

}

treebird.waifuism.life {
    root * /usr/local/share/treebird/dist
    try_files {path} {path}/
    reverse_proxy unix//var/run/fcgiwrap.socket {
            transport fastcgi {
                root /usr/local/share/treebird/dist
                env SCRIPT_FILENAME /usr/local/bin/treebird.cgi
                split .cgi

                }
        }
}

3. The problem I’m having:

I’m trying to set an alternative Frontend in treebird.waifuism.life for my federated social media in waifuism.life, it uses fcgi and the script Treebird was built correctly.
The thing I believe I’m missing is the configuration for Caddy, fcgiwrap was installed and it is working since with this configuration I can see the website 404 pop out in the browser. (see https://treebird.waifuism.life/)
I browsed several threads along with the documentation on caddy and fcgi but I don’t seem to quite understand what I’m missing.

4. Error messages and/or full log output:

I’m not quite sure where to get them logs but I will send them if you find them necessary.

5. What I already tried:

I tried messing with the env variables and reading the documentation for the other two servers that have been able to successfully run Treebird, those being Apache and Nginx, here are those:

Nginx:

server {
       server_name treebird.example.com;

       location @treebird {
                fastcgi_param SCRIPT_FILENAME /usr/local/bin/treebird; # change this to the location of your treebird executable
                fastcgi_param   PATH_INFO       $uri;
                fastcgi_pass   unix:/var/run/fcgiwrap.socket;
                include       fastcgi.conf; #Check your nginx installation for fastcgi.conf or fastcgi_param
       }

       location / {
                root /usr/local/share/treebird; #Change this to the location of the static files
                try_files $uri @treebird;
       }

       listen [::]:443 ssl; 
       listen 443 ssl; 
       ssl_certificate /etc/letsencrypt/live/treebird.example.com/fullchain.pem; 
       ssl_certificate_key /etc/letsencrypt/live/treebird.example.com/privkey.pem; 
       include /etc/letsencrypt/options-ssl-nginx.conf; 
       ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
}

server {
       if ($host = treebird.example.com) {
                return 301 https://$host$request_uri;
       } 


       listen 80;
       listen [::]:80;

       server_name treebird.example.com;
       return 404; 
}

Apache:

# Give access to our directory
<Directory "/usr/local/share/treebird/dist">
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/usr/local/share/treebird/dist"

    # Intercept meta files to be loaded before proxy
    RewriteEngine on
    RewriteRule ^/(.*).css$ /$1.css [L]
    RewriteRule ^/svg/(.*).svg$ /svg/$1.svg [L]
    RewriteRule ^/js/(.*).js /svg/$1.js [L]
    RewriteRule ^/emoji.json$ /emoji.json [L]
    RewriteRule ^/(treebird\_logo|favicon).png$ /$1.png [L]

    # Set PATH_INFO variable
    SetEnvIf Request_URI . proxy-fcgi-pathinfo=1
    ProxyPass / "fcgi://localhost:4008/"
</VirtualHost>

6. Links to relevant resources:

https://caddy.community/t/fcgiwrap-and-caddy/12324

Thank you very much in advance, kind regards. Help with Caddy and fastcgi with environment variables

I think you’re probably missing file_server to serve static files like CSS and JS.

You’ll need to use a request matcher to make only requests that don’t point to static files get proxied to your fastcgi upstream (others should fall through to file_server).

See the php_fastcgi expanded form, which has all the relevant bits you would need to do this correctly.

2 Likes

Hello again, thanks for your reply, with that info a friend of mine and me were able to implement the config file, there’s probably another way to make it but this is the one we found and for now it is working well.

waifuism.life {
    reverse_proxy 127.0.0.1:4000
}

treebird.waifuism.life {
    route {
        handle /js/* {
            root * /usr/local/share/treebird
            try_files {path}
            file_server
        }
        handle /img/* {
            root * /usr/local/share/treebird
            try_files {path}
            file_server
        }
        handle /svg/* {
            root * /usr/local/share/treebird
            try_files {path}
            file_server
        }
        handle /*.css {
            root * /usr/local/share/treebird
            try_files {path}
            file_server
        }
        handle /*.png {
            root * /usr/local/share/treebird
            try_files {path}
            file_server
        }
        handle /* {
            reverse_proxy * unix//var/run/fcgiwrap.socket {
                transport fastcgi {
                    env SCRIPT_FILENAME /usr/local/bin/treebird.cgi
                }
            }
        }
    }
}

Thank you very much for your help.

Yeah, you can significantly simplify that.

waifuism.life {
    reverse_proxy 127.0.0.1:4000
}

treebird.waifuism.life {
	@static path /js/* /img/* /svg/* /*.css /*.png
	handle @static {
		root * /usr/local/share/treebird
		file_server
	}

	handle {
		reverse_proxy unix//var/run/fcgiwrap.socket {
			transport fastcgi {
				env SCRIPT_FILENAME /usr/local/bin/treebird.cgi
			}
		}
	}
}
2 Likes

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