I want to send a request to a specific URL to another docker container and all others I want to serve static content, but I keep getting a 404 error

1. Output of caddy version:

v2.6.1 h1:EDqo59TyYWhXQnfde93Mmv4FJfYe00dO60zMiEt+pzo=

2. How I run Caddy:

I am using docker-compose with a caddy and matrixdotorg/synapse image.

a. System environment:

Docker on Bodhi Linux

b. Command:

sudo docker-compose up -d

c. Service/unit/compose file:

version: '3'

services:
    synapse:
        image: matrixdotorg/synapse:latest
        container_name: synapse                                
        restart: unless-stopped
        volumes:
            - /home/me/Docker/Synapse/data:/data
        environment:                                                
            - SYNAPSE_SERVER_NAME=synhang.host
            - SYNAPSE_REPORT_STATS=no
            - VIRTUAL_HOST=synhang.ddns.net

    caddy:
        image: caddy:latest
        container_name: synapse-caddy
        restart: unless-stopped
        ports:
            - 80:80
            - 443:443
        volumes:
            - ./Caddyfile:/etc/caddy/Caddyfile
            - ./site:/srv
            - ./caddy_data:/data:rw
            - ./caddy_config:/config:rw

d. My complete Caddy config:

synhang.ddns.net {
	tls myemail@proton.me
	reverse_proxy /chat synapse:8008
	file_server browse
}

3. The problem I’m having:

Basically I want to serve static content to all requests to synhang.ddns.net, however, if a request goes to synhang.ddns.net/chat i want it to reverse proxy to the synapse container.

With the Caddyfile shown above I can see static content when I load https://synhang.ddns.net but when I try to go to https://synhang.ddns.net/chat/ I get 404 No Such Resource error. I thought the issue might just be because the chat directory didn’t exist, so I went ahead and made one. Still get the same error

If I have the following Caddyfile, It serves the synapse server just fine.

synhang.ddns.net {
   tls myemail@proton.me
   reverse_proxy synapse:8008
}

What am I not doing right?

4. Error messages and/or full log output:

Paste logs/commands/output here.
USE THE PREVIEW PANE TO MAKE SURE IT LOOKS NICELY FORMATTED.

5. What I already tried:

6. Links to relevant resources:

Path matching is exact in Caddy, so /chat will only match exactly /chat and nothing else. You want /chat* instead, to also match /chat/.

Also, you probably should specify root anyways, even if you’re using the default directory of /srv. Add root * /srv to your config for peace of mind.

Ok, because /chat/ is just an arbitrary URI i made up, there isn’t any chat directory on the upstream server I am pointing it to, hence the 404.

The real path is /_matrix/static. But I want /chat to redirect to /_matrix/static, how can I achieve this. Is there an equivalent to ‘alias’ with Nginx?

You can do this:

handle_path /chat* {
	rewrite * /_matrix/static{uri}
	reverse_proxy synapse:8008
}

The handle_path directive will strip /chat from the URI, and then rewrite will prepend it with /_matrix/static afterwards ({uri} is the current URI, after stripping).

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