Reversy proxy - how to modify header request

1. The problem I’m having:

Hello.
I am trying to reverse proxy to an external server (Internet) from LAN.
On that reversy_proxy I need to change the host and referrer headers, but with everything I do I can’t modify these headers.

2. Error messages and/or full log output:

2024/05/02 11:25:20.240 ←[34mINFO←[0m   http.log.access handled request {"request": {"remote_ip": "::1", "remote_port": "59955", "client_ip": "::1", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8080", "uri": "/xxx/yyyyy/GETPHRASES/Page", "headers": {"Sec-Fetch-Site": ["same-origin"], "Sec-Fetch-Dest": ["empty"], "Referer": ["http://localhost:8080/xxx/yyyyy/runclient/Page"], "Accept-Language": ["es-AR,es-US;q=0.9,es-419;q=0.8,es;q=0.7"], "X-Vtscadaphraseids": ["REASON_STR_HOSTNOTINLIST"], "Sec-Ch-Ua-Platform": ["\"Windows\""], "Sec-Fetch-Mode": ["cors"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "Accept-Encoding": ["gzip, deflate, br, zstd"], "Connection": ["keep-alive"], "Sec-Ch-Ua": ["\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\""], "X-Vtscadaanywhere": ["true"], "Sec-Ch-Ua-Mobile": ["?0"], "Accept": ["*/*"]}}, "bytes_read": 0, "user_id": "", "duration": 0.0647577, "size": 66, "status": 200, "resp_headers": {"Server": ["Caddy", "VTScada"], "Cache-Control": ["no-cache, no-store"], "Date": ["Thu, 2 May 2024 11:25:20 GMT"], "Content-Type": ["application/json; charset=\"utf-8\""], "Content-Length": ["66"]}}

3. Caddy version:

caddy_2.8.0-beta.1_windows_amd64

4. How I installed and ran Caddy:

I created a caddyfile in same folder as caddy.exe
inside a bat file
caddy run

a. System environment:

Windows 10 Pro 22H2

b. Command:

caddy run

c. Service/unit/compose file:

d. My complete Caddy config:

:8080 {
	log
	reverse_proxy /xxx* https://www.example.com {
		header_up host {upstream_hostport}
	}
	rewrite / /xxx/
}

5. Links to relevant resources:

The “full log output” you posted on point 2 is the access-log and the "host": "localhost:8080" from "request" is how the request arrived at Caddy.

If you want to see the more details from the reverse proxy communication with https://www.example.com, you can enable debug logging and watch out for http.handlers.reverse_proxy.

To modify the “Referer”, which has the original value of http://localhost:8080/xxx/yyyyy/runclient/Page you can use again header_up (see reverse_proxy (Caddyfile directive) — Caddy Documentation):

header_up sets, adds (with the + prefix), deletes (with the - prefix), or performs a replacement (by using two arguments, a search and replacement) in a request header going upstream to the backend.

1 Like

Steffen.

Thank you.

Now is possible to see the changes for host header.

To change Referer header string, only with regex or is there a function like replace?

Regards.

Marcos.

There is, see above excerpt from the reverse_proxy documentation:

or performs a replacement (by using two arguments, a search and replacement)

For example, this should be working:

header_up Referer "runclient" "chicken-dinner"

Ok, thanks.

In documentation is not explained how to use regex sintax with variables.

I need to change host in Referer header to upstream variable

This code doesn’t work

Header_up Referer ‘{upstream}{Referer}(/tmb/.)’

I tried to concatenate upstream variable with Referer header without host and port.

Thanks.

Does your Referer always contain localhost:8080?
If so, use this:

header_up Referer "localhost:8080" "{upstream_hostport}"

or

header_up Referer "http://localhost:8080" "https://{upstream_hostport}"

Take care about case sensitivity regarding caddy directives. Header_up is wrong, it must be header_up.

1 Like