Reverse Proxy isn't working

1. Caddy version (caddy version): V2

2. How I run Caddy: Caddy maintains a set of images which can be run with docker. We are using latest image from Docker Hub. We use a Caddyfile.

a. System environment: Docker running on Openshift

b. Command:

paste command here

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

:4000 {

for app

root * /opt/app-root/src
reverse_proxy /parul/* https://google.com {
header_up Host {http.reverse_proxy.upstream.host}
header_up Sec-WebSocket-Protocol {>Sec-WebSocket-Protocol}
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
flush_interval -1
}
route /marketplace/* {
uri strip_prefix /marketplace
reverse_proxy https://app-digmkt-prod.apps.silver.devops.gov.bc.ca {
header_up Host {http.reverse_proxy.upstream.host}
header_up Sec-WebSocket-Protocol {>Sec-WebSocket-Protocol}
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
flush_interval -1
}
respond /ehlo 200
}

file_server
encode gzip

try_files {path} {path}/ /index.html

log {
output stderr
output stdout
format single_field common_log
level info
}

header / {
# prevent any static html from being cached
Cache-Control “public, max-age=0, must-revalidate”
}
}

3. The problem I’m having:

A route in our website /marketplace/* needs to be served from a different server with our server acting as an proxy.

4. Error messages and/or full log output:

Can’t seem to upload log output because of 4 url limit. I will try to filter out all urls and try again. :frowning:

5. What I already tried:

I tried running a ton of different variations of this caddy configuration, reading up on caddy documentation and trying out different approaches. Nothing works and logs are inconclusive.

6. Links to relevant resources:

You can remove all of these lines, they’re unnecessary/incorrect. Caddy already passes through or sets the appropriate headers:

You can replace this with just handle_path /marketplace/*, which has built-in path prefix stripping logic.

This will only match requests to exactly /, which is probably not what you want. Omit the / to match all requests (or put it within one of the handle blocks, see below).

I’m pretty sure the problem you’re having though is this line:

Caddy sorts directives according to this directive order, so try_files will run before the proxies.

Instead, I’d recommend writing your config like this, with handle blocks:

:4000 {
	handle /parul/* {
		reverse_proxy https://google.com {
			header_up Host {http.reverse_proxy.upstream.host}
		}
	}

	handle_path /marketplace/* {
		reverse_proxy https://app-digmkt-prod.apps.silver.devops.gov.bc.ca {
			header_up Host {http.reverse_proxy.upstream.host}
		}
	}

	handle {
		root * /opt/app-root/src
		try_files {path} {path}/ /index.html
		encode gzip
		file_server
	}
}
1 Like

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