Serving cgi files from a different directory

1. Caddy version (caddy version):


2. How I run Caddy:

serving a web site with perl scripts

a. System environment:


b. Command:

systemctl reload caddy

c. Service/unit/compose file:

d. My complete Caddyfile or JSON config: {

	root * /var/www/html

	root * /var/www/cgi
	reverse_proxy unix//var/run/fcgiwrap.socket {
	transport fastcgi { split .cgi }


3. The problem I’m having:

we don’t know how to write the correct caddyfile

4. Error messages and/or full log output:

either the file_server works or the cgi works

5. What I already tried:

we would like to serve regular files from the /var/www/html dir and cgi scripts from the/var/www/cgi dir

how do we combine these two into the caddyfile ?

Please be more specific. Run caddy version to see the version.

Well, you need to use a request matcher to decide how to handle requests. You probably need to use a request matcher with handle directives to isolate those two chunks of config from eachother.

It’s not clear from what you’ve posted how requests to your server will look, so I can’t help you with that without more information.

version v2.4.0 h1

in the folder /var/www/cgi we keep scripts like add.cgi update.cgi delete.cgi

in the folder /var/www/html we keep Index.html sample.txt form.html image files, additional folders etc. {

	handle /var/www/html/* {

	handle /var/www/cgi* {
		reverse_proxy unix//var/run/fcgiwrap.socket {
		transport fastcgi { split .cgi }


something like this ?

No. You need to think about what requests should reach what path.

When I say requests, I mean: what aspect of the request itself tells you whether it should be handled as CGI or handled as a static file?

What URI do your requests look like? For those, which should be run as CGI, and which are just regular files?

The root directive defines which filesystem path Caddy should look for files to serve or proxy via fastcgi. That’s separate from request matching, because your requests won’t have the path /var/www/cgi in them.

if the URL extension is .cgi it should look into /var/www/cgi


for any other extension it should look into /var/www/html


is this the fastest way ? since I guess caddy has to parse each URL and look for .cgi or you do recommend a better way to handle script files ?

It would be better if you put all your files in one place instead of two. Having the roots split up only complicates things.

If you put them in the same place, then all you’d need is a matcher like this to only handle *.cgi files via the fastcgi proxy (the rest will fall through to file_server).

	root * /var/www/html

	@cgi path *.cgi
	reverse_proxy @cgi unix//var/run/fcgiwrap.socket {
		transport fastcgi {
			split .cgi


that’s great thank you!

is it possible to tell the server to load index.cgi as default in a dir in addition to index.html ?

You can only have one index at a time. It doesn’t make sense to have two. How would Caddy choose otherwise?

But if you want index.cgi to be your default, you can add this to your config:

try_files {path} /index.cgi

This makes Caddy check if there’s a file that exists at the given request path, and if not, it’ll rewrite the request path to /index.cgi instead.

1 Like

perfect thank you!

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