Skip to content

Instantly share code, notes, and snippets.

@comp500
Last active September 8, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comp500/9732e88cf04a3b069bab92e8d0752a57 to your computer and use it in GitHub Desktop.
Save comp500/9732e88cf04a3b069bab92e8d0752a57 to your computer and use it in GitHub Desktop.
Caddy documentation for regular expression replacement (https://github.com/mholt/caddy/pull/2144) (licensed public domain/CC0)

Syntax

header_upstream name value
header_downstream name value
header_upstream name regex replacement
header_downstream name regex replacement
  • (existing header_upstream doc)
  • header_upstream can also be used with an extra argument, where the field name is name, the regular expression to match in it is regex, and the value to replace the match with is value. This option allows values within headers to be modified based on those values, can be specified multiple times for multiple headers and for multiple modifications of the same header. Capture groups are supported and substituted into the replacement (as $1, $2, etc.) and request placeholders can be used in the replacement. The regular expressions use Go's syntax described here.
  • (existing header_downstream doc)
  • header_downstream can also be used with an extra argument, in the same way header_upstream can.

Examples

Rewrite redirects (301, 302, 307 etc) going to localhost so that they go to example.com instead:

proxy / localhost:8080 {
	header_downstream Location localhost example.com
}

Remove a cookie being sent from the backend server to the client, and a cookie being sent from the client to the backend server:

proxy / localhost:8080 {
	header_downstream Set-Cookie "^Tracking=.*" ""
	header_upstream Cookie "(.*)(^Tracking=[^;]*; |; Tracking=[^;]*|^Tracking=[^;]*$)(.*)" "$1 $3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment