No upstream placeholder with http.proxy

Hello,

I try to use caddy as proxy for my api and now I’m at the logging part.
Following to https://caddyserver.com/docs/proxy I could use {upstream} as placeholder, but I only get - as value.

Hi @Barbers,

I tested it just now on Caddy 0.11.3.

Caddyfile
:2015 {
  proxy / localhost:2016
  log / stdout "{common} /// {upstream}"
}

:2016 {
  status 200 /
}
Request
~/Projects/test
➜ curl -I localhost:2015
HTTP/1.1 200 OK
Date: Mon, 11 Feb 2019 01:25:01 GMT
Server: Caddy
Server: Caddy
Output
~/Projects/test
➜ caddy
Activating privacy features... done.
http://:2015
http://:2016
::1 - - [11/Feb/2019:11:25:01 +1000] "HEAD / HTTP/1.1" 200 0 /// http://localhost:2016

If you’re fully updated and still having issues, post your Caddyfile and we can take a look.

1 Like

Hi @Whitestrake,

after some time I was able to trace it down to the header I set.
Please try the following.

:2015 {
proxy / localhost:2016
header / Access-Control-Allow-Origin *
log / stdout “{common} /// {upstream}”
}
:2016 {
status 200 /
}

Ahh, yep. Reproduced the issue on my end, it’s a bug.

Could you go to https://github.com/mholt/caddy/issues/new and fill out an issue template for us? Feel free to link this thread.


Looking at the code…

The log directive attaches a Replacer to the ResponseRecorder that each successive directive can add placeholders to (such as proxy adding {upstream}): https://github.com/mholt/caddy/blob/1867ded14c82cd2ab296df8d783bbc880360d37d/caddyhttp/log/log.go#L47-L50

Looks like header actually wraps the ResponseRecorder in a ResponseWriterWrapper which has its own new Replacer: https://github.com/mholt/caddy/blob/1867ded14c82cd2ab296df8d783bbc880360d37d/caddyhttp/header/header.go#L37-L40

I assume that the proxy directive consequently adds the placeholder to the wrapper’s Replacer instead of the recorder’s Replacer: https://github.com/mholt/caddy/blob/1867ded14c82cd2ab296df8d783bbc880360d37d/caddyhttp/proxy/proxy.go#L189-L191

Whereas the recorder’s Replacer is the one that is used to generate the actual log output: https://github.com/mholt/caddy/blob/1867ded14c82cd2ab296df8d783bbc880360d37d/caddyhttp/log/log.go#L84

I’ve determined a workaround.

As long as your proxy is in effect for all requests where you need that header, you can use the header_downstream subdirective to have the proxy itself append the header.

:2015 {
  proxy / localhost:2016 {
    header_downstream Access-Control-Allow-Origin *
  }
  log / stdout "{common} /// {upstream}"
}

:2016 {
  status 200 /
}
~/Projects/test
➜ curl -I localhost:2015
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Date: Tue, 12 Feb 2019 03:09:12 GMT
Server: Caddy
Server: Caddy
::1 - - [12/Feb/2019:13:09:12 +1000] "HEAD / HTTP/1.1" 200 0 /// http://localhost:2016

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