How to serve multi static web site with different route

1. Caddy version (caddy version):

v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

sudo apt install caddy

a. System environment:

Ubuntu 18.04.2 LTS, fresh and clear.

b. Command:

sudo caddy start --config Caddyfile --watch

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

myWebSiteFoo.com {
  reverse_proxy /t1 localhost:8760
  reverse_proxy /t1/* localhost:8760

  reverse_proxy /t2 localhost:8761
  reverse_proxy /t2/* localhost:8761
}

localhost:8760 {
  root * /home/username/app/Test/t1/dist
  file_server
}

localhost:8761 {
  root * /home/username/app/Test/t2/dist
  file_server
}

3. The problem I’m having:

I want to host my static web sites under a same domain, split it with routes. I.E.
Visit myWebSiteFoo.com/t1 the server returns the file /home/username/app/Test/t1/dist, and
Visit myWebSiteFoo.com/t2 returns /home/username/app/Test/t2/dist

But I do got response with curl https://localhost:8761 on the server. (The response is the content of index.html.) (BTW, curl localhost:8761 just returns “Client sent an HTTP request to an HTTPS server”.)

If I use caddy just to reverse_proxy, not use as a file_server, and use another process like Node.js to host my website on localhost:port, the whole thing works well. Visit myWebSiteFoo.com/t2 can get response.

Is something wrong with my caddyfile?

4. Error messages and/or full log output:

No error, the request returns blank.

5. What I already tried:

Caddyfile directives like: route, rewrite, redir, root, uri strip_prefix.
Config the website with a base url, i.e curl https://localhost:8761/t2 on server works correctly.

6. Links to relevant resources:

N/A

Did you try writing separate site block for the two routes like this:

myWebSiteFoo.com/t1/* {
  reverse_proxy  localhost:8760
}

myWebSiteFoo.com/t2/* {
  reverse_proxy localhost:8761
}

see https://caddyserver.com/docs/caddyfile/concepts#addresses

Proxying here is a pretty strange approach for this.

This is probably what you’re looking for:

myWebSiteFoo.com {
	rewrite /t1 /t1/
	handle /t1/* {
		uri strip_prefix /t1
		root * /home/username/app/Test/t1/dist
		file_server
	}

	rewrite /t2 /t2/
	handle /t2/* {
		uri strip_prefix /t2
		root * /home/username/app/Test/t2/dist
		file_server
	}
}

In Caddy v2.1 (beta 1 is available now on github releases if you want to try it out) this can be simplified by using handle_path instead of handle which avoids needing uri strip_prefix.

Some explanation – I’m using a rewrite to ensure that a trailing / exists so that the matcher on handle will handle requests without the trailing /. The handle directive groups together some handlers to run if the request is matched.

The reason uri strip_prefix is necessary is that Caddy’s file server looks for files on disk using the root + the request path. That means that without stripping the path prefix, a request like /t1/foo.js would be looked for on disk at /home/username/app/Test/t1/dist/t1/foo.js but it sounds like you want it to look at /home/username/app/Test/t1/dist/foo.js.

2 Likes

This works but I also need caddy as a file server. thx.

Cool. This works as I expected, thx.