Strip all query parameters from reverse proxy

1. The problem I’m having:

I’m attempting to only proxy the actual path to our backend, but every permutation of rewrites still includes the query string. Essentially we want to transparently strip the query string and prevent it from reaching the backend service. Unfortunately we cannot control or account for the query strings passed to this service and thus we must remove them in aggregate.

For example, the curl command below includes the query string:

$ curl 'https://localhost/test?format=json&abcd=1' -vk

The backend (simple node app to print the response), sees the following URI:

/?format=json&abcd=1

However, if I simply print the response directly from Caddy via

respond @stripQuery {re.stripQuery.1}

I see the expected result:

/test

I would like for the backend to only receive /test.

Any suggestions and alternative solutions are much appreciated!

2. Error messages and/or full log output:

No errors, just unexpected results.

3. Caddy version:

v2.7.5 h1:HoysvZkLcN2xJExEepaFHK92Qgs7xAiCFydN5x5Hs6Q=

4. How I installed and ran Caddy:

Downloaded from Caddy website.

a. System environment:

Fedora 37

b. Command:

caddy run

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

{
	admin off

	local_certs

	log {
		level INFO
		format json
	}
	debug
}

:443 {
	bind 0.0.0.0

	tls internal {
		on_demand
	}

	log {
		level INFO
		format json
	}

	@stripQuery vars_regexp stripQuery {uri} ([^?]+)(\?.*)
	#respond @stripQuery {re.stripQuery.1}
	reverse_proxy http://localhost:3001 {
		rewrite {re.stripQuery.1}
	}
}

5. Links to relevant resources:

I think you can simply do rewrite * {path}? (rewrite directive, not the reverse_proxy option)

1 Like

Unfortunately that didn’t work.

	#@stripQuery vars_regexp stripQuery {uri} ([^?]+)(\?.*)
	rewrite * {path}
	reverse_proxy http://localhost:3001 {
		#rewrite {re.stripQuery.1}
	}

The backend service now receives the full path and query: /test?format=json&abcd=1.

The question mark is important, it signals to rewrite to also transform the query.

1 Like

Doh! My eyes are tired.

Thank you, that’s exactly what I need. Works perfectly.

2 Likes

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