Search code examples
cookiesreverse-proxycaddycaddyfile

Caddy2 pass cookie value from one reverse proxy to another


I need some help working with Caddy2 Server and the Caddyfile.

Some background info: Let's say I would like to reverse proxy on 2 sites named page1.com and page2.com So for example currently page1.com is visible in localhost:8080/page1 and page2.com is visible in localhost:8080/page2

Now assume that /page1 gives me a specific cookie named "myCookie" which has some random value in it. I would like to pass this cookie also to /page2, so I'd have the cookie "myCookie" on both pages.

I know that I can use header_down +Set-Cookie "..." on /page2, but since the value is random, I would need to somehow safe the cookie value from /page1 in order to reuse it or pass it to /page2

And that's where my knowledge reaches its limits, because in every research I did I couldn't find a way to store that value since Caddy2 doesn't seem to support variables.

Does anyone have a clue & is able to help me out with this one?

Thanks in advance!

Best Regards Furkan


Solution

  • Meanwhile I've found a solution on how to accomplish this and decided to post it up here for someone who also may research for this in the future.

    The idea behind that is to change the path of "myCookie" from /page1 to /, so the page2 is also able to access that cookie. I've also renamed that cookie in between just to make it clear that it now isn't only the cookie of /page1

    header_down Set-Cookie "/page1" "/" # this will change the path of the cookie to /
    header_down Set-Cookie "myCookie" "ourCookie"
    header_up Set-Cookie "ourCookie" "myCookie"