Proxy pass and rewrite

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

a. System environment:

Ubuntu 18

b. Command:

caddy start

d. My complete Caddyfile or JSON config:

status.pingr.io {
  route {
    rewrite * /status/Mlm1zrAx
    reverse_proxy https://app.pingr.io {
      header_up Host {http.reverse_proxy.upstream.hostport}
    }
  }
 }
  #tls {
  #  on_demand
  #}
}

3. The problem I’m having:

I have status pages which are located at addresses like https://app.pingr.io/status/{id}
Now I want users to be able to use custom domains which will be redirected using CNAME.

To begin with this task I decided to try something simple. When user goes to https://status.pingr.io I want that he’s proxied to https://app.pingr.io/status/Mlm1zrAx/. The “Mlm1zrAx” is just hardcoded for now. But of course I want that it’d be transparent for user, meaning it’s not a redirect but proxy, so that url doesn’t change.

Next step is to proxy: custom-domain.comhttps://app.pingr.io/status/custom-domain.com, so that I can link users domain to my status page.

4. Error messages and/or full log output:

5. What I already tried:

I know that I probably missing something simple, and I don’t fully understand web servers configuration.

What I tried is I actually created the caddyfile, and tried to use rewrite + reverse_proxy.

However what I get right now, is that files served from app.pingr.io have wrong Content-Type. All files (css/js) have text/html, so as a result I got " We’re sorry but pinger doesn’t work properly without JavaScript enabled. Please enable it to continue."

6. Links to relevant resources:

https://status.pingr.io
https://app.pingr.io/status/Mlm1zrAx

I think you want to do your rewrite like this:

rewrite * /status/Mlm1zrAx{uri}

The difference is that this will prefix all URLs, but what you were doing was rewriting all URLs to just /status/Mlm1zrAx, meaning every request, even the ones for JS files, were being rewritten to exactly /status/Mlm1zrAx. Instead, I think you want to proxy to /status/Mlm1zrAx/js/app.js or something like that.

Hm, I guess not. Look, the js/css files are located at https://app.pingr.io/js/....

So, when user goes to status.pingr.io it should behave like he opened app.pingr.io/status/{id}. And if I go directly to app.pingr.io/status/{id}, then all the js/css are loaded at app.pingr.io/js/...

I don’t know if it may help, but previously when I tried to do this with nginx, I found a sample config which had sub_filter and he kind of replaced ulrs in html code or something like that

Then you only want to match requests to /?

Do this instead:

rewrite / /status/Mlm1zrAx

But like you said, ultimately you’ll probably need to filter the responses, which is not fun.

@matt is working on a plugin that does that, but it’s not open sourced yet, but will be soon:

1 Like

Strange, right now I get redirected to the login page.

However if I go to https://app.pingr.io/status/Mlm1zrAx it’s not under authorization, I can see the page I need.

I’m not sure I understand rewrite directive correctly.

If I have rewrite * /bar, it means that if I go to site.com/foo I’ll get redirected to site.com/bar, right?

Then, even if rewrite / /status/some-id is better it shouldn’t affect. In my case if I entered status.pingr.io/whatever/whatever... I’ll get get proxied to app.pingr.io/status/id, right? Like, no matter what user typed after site.com/, I’ll get redirected exactly to /status/some-id

Path matching in Caddy v2 is exact-match, so rewrite / /bar will only rewrite requests on / and will not rewrite requests on /foo.

The reason you’re getting kicked to the login page is likely because the authentication cookie is only stored for the app.pingr.io domain, and not status.pingr.io

Ah well, so do I get it right. I go to status.pingr.io and receive javascript files from app.pingr.io. These javascript works like “When user on the root route “/” he should be redirected to login page”. Since the javascript doesn’t know that he was served from /status/{id}.

So I need to have a separate repository for status.pingr.io I guess…

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