Reverse proxy for POST, file server for GET

1. The problem I’m having:

Hello! I’m trying to get Caddy to both reverse proxy POST requests to another port on localhost, and to act as a file server for GET requests. I’d prefer to use the command line interface if possible, or otherwise a Caddyfile. I tried searching, but I couldn’t find anything on this.

2. Error messages and/or full log output:

[none]

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

I followed the usual method for debian, i.e. adding the custom repo.

a. System environment:

Debian 11.7, x86-64, with systemd, without docker.

b. Command:

I tried asking Google Bard, but I think it made it up. It said to run both of these commands:

caddy reverse-proxy --from example.com --to example.com:8081 --method POST
caddy file-server --root /path/to/your/static/files --domain example.com

c. Service/unit/compose file:

[none]

d. My complete Caddy config:

[none - I’m trying to use the CLI]

5. Links to relevant resources:

[none]

I’m mobile now so I can only be brief, but can expand later if still necessary.

You’ll have to use the Caddyfile. I wouldn’t take Bard’s answers as accurate for such usages. What you need is the method subdirective of the reverse_proxy directive. See the Rewrite section here.

Brilliant, thank you. Would I be right in thinking that this is an example of a Caddyfile that achieves this goal?

:80 {
    reverse_proxy localhost:8080 {
        method POST
    }
    file_server
}

I know AI is inaccurate, particularly for these things, but Bard suggested this:

:80 {
    route {
        POST /* {
            reverse_proxy localhost:8080  # Replace with the address of your backend server
        }
        GET /* {
            file_server
        }
    }
}

Is one of these better than the other, do you know?

Oh, sorry, I misunderstood the question. I thought you want to convert POST to GET.

To confirm, you want POST requests to be reverse_proxy-ed but GET requests to go to file_server, right?

If yes, then you’re looking for this:

example.com {
   @post method POST
    @get method GET

    reverse_proxy @post localhost:8080
    file_server @get
}
1 Like

You are a gentleman and a scholar, thank you sir!

2 Likes

I’d do it like this:

example.com {
	@proxy not method GET
	reverse_proxy @proxy localhost:8080

	file_server
}

This avoids needing two matchers, and it makes sure all methods are covered (including DELETE and PUT, etc).

1 Like

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