Redirect POST request to external server

1. Caddy version (caddy version):

v2.4.6 h1:HGkGICFGvyrodcqOOclHKfvJC0qTU7vny/7FhYp9hNw=

2. How I run Caddy:

/caddy start

a. System environment:

Debian 4.19.208-1

d. My complete Caddyfile or JSON config:

:80 {
       reverse_proxy /api 

3. The problem I’m having:

I wrote a script that makes POST requests with JSON data to a remote server.
And some reason this request needs to do from a particular (whitelist) IP which binding on the server.
So I decide on this whitelist IP server to install caddy server and just redirect my request from 1 to 2 through caddy server. No need to modify my request just need to redirect.

With provided config, I have an issue - yes redirect is working but redirect to
But it’s not what I need.
Please help me handle this, thanks!

I’ve tried this config, but with https:// I get error config. Also tried without https:// - but nothing works

route /api {
	uri strip_prefix /api

Okay, there’s a few things going on:

  • Caddy’s path matching is exact. So if you match /api, then only /api will be matched, and not /api/foo. So use /api* if you want to match all URLs.

  • If you want to strip a path prefix, you can use handle_path /api* (instead of route + uri strip_prefix).

  • If you need to rewrite the request to a different base path, then you need to use the rewrite directive. The reverse_proxy module doesn’t perform rewrites itself. So maybe something like rewrite * /path1/path2{uri} if you want to add a new prefix (after having stripped /api).

  • If you need to proxy to an HTTPS upstream, you probably need to configure headers like this: reverse_proxy (Caddyfile directive) — Caddy Documentation

  • Does your upstream have a publicly trusted certificate? If so, then it’ll probably work fine as-is. If not, and it’s a self-signed cert, then you’ll need to configure trust via the transport http options.

1 Like

Its works!
Thank you!!!

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