Reverse proxy requests to append to subdirectory

I posted this in the github, so Matt kindly directed me to the forums. Sorry! :slight_smile:

Just wanted to start out and say what awesome work everyone is doing on this project! Was looking to start out and learn something new and landed here while doing some research for other servers. I managed to get caddy setup as a reverse proxy for most of my apps but having a little trouble with the rewrite rules to work for glances WebUI. I looked at the glances wiki and they gave this example for Nginx:

Nginx:

location / {
if ($http_referer ~ “^https?://[^/]+/glances”){
rewrite ^/(.*) /glances/$1 redirect;
}
}

location /glances/ {
rewrite /glances/(.*) /$1 break;
proxy_pass http://localhost:61208/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

I’m totally new to this so I think i’m just lost as to what is actually being sent or received for the host and remote computers using the second rewrite + proxy_set_headers being used in the glances location block? Any help would be greatly appreciated, thanks!

Edit: Looks like i need to make sure all requests appended to the subdirectory of domain are proxied to host as well.

1 Like

This works

example.com {
    rewrite {
        if {>Referer} has /glances
        to /glances/{path}
    }
    proxy /glances localhost:61208 {
        without /glances
        proxy_header Host {host}
        proxy_header X-Real-IP {remote}
        proxy_header X-Forwarded-Proto {scheme}
    }
}

The only thing that isn’t working is /glances/10 and I’m not sure why.


Alternatively use a subdomain

glances.example.com {
    proxy / localhost:61208 {
        proxy_header Host {host}
        proxy_header X-Real-IP {remote}
        proxy_header X-Forwarded-Proto {scheme}
    }
}
1 Like

Oh man thanks so much!!!

I thought using the locations would be easier for me to create a dashboard that houses all the apps and i could just call each with “/app” no matter if I’m using the dashboard locally or through a domain. So basically if i wanted to use the dashboard using localhost address i wouldn’t need to go through the domain at all?

I read through the docs but is there any other good resources you’d recommend that i could read up on to get a better idea of how to use caddy and take full advantage of all it features?

Thanks again!

You can listen on multiple addresses easily:

example.com localhost mydomain.com {

}

Of course you could always just use localhost:61208 for local access.

For docs all you need is Welcome — Caddy Documentation

Beyond that I’d suggest browsing this forum to see how other people are using Caddy.
You could even read other people’s Caddyfile on Github

1 Like

Hmm not sure /glances/10 is used…? But right now it looks like all the system information it collects is showing up so might not be an issue. At least now i have a clearer idea of whats going on and have a better reference of how i can utilize the rewrite directive.

Thanks!

/glances/10 only changes the refresh rate, so it’s not that important if it does/doesn’t work.

You’re welcome!

Also i don’t think i ever would have known to use the “without /glances” you added!

1 Like

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