Reverse proxy for a specific path

I’m trying to reverse proxy a site to a specific path on the backend.

The user should access “http://external.address.com” and on the backend the application is served from “http://server:8083/SpecificPath”. With my actual config “SpecificPath” is carried on to the external address, the response turns into “http://external.address.com/SpecificPath”.

my Caddyfile:

external.address.com:80 {
    proxy / server:8083/SpecificPath {
        proxy_header Host {host}
    }
}

It’s like I need the without directive the other way, from the backend to caddy.

Is it possible?

So no matter what the original request path is, you always want the proxy to access server:8083/SpecificPath?

The reason your hostname is being changed to external.address is because you’re telling it to preserve the Host header… surely that is intentional?

Hello Matt, thanks for the reply!

Yes, that address will be always served to that path. I don’t know if i can use capture groups so the url’s can be exchanged back and forth, like this:

external.address.com:80 {
    proxy /(.*) server:8083/SpecificPath/{1} {
        proxy_header Host {host}
    }
}

Researching for this before posting here, i’ve found this issue: https://github.com/mholt/caddy/issues/444 if I remove the “proxy_header Host {host}” the same behavior (redirect to upstream address) occurs.

If a redirect is happening, it’s because your web app is causing the redirect. Caddy isn’t doing a redirect, not with that Caddyfile.

1 Like

Aside from the redirect, the specific path reverse proxy as I mention on the first post is possible?

Not that I know of; @abiosoft, do you know any clever tricks for something like that? Never heard of this before.

What I can think of is a hack like this using rewrite.

external.address.com:80 {

    rewrite / /SpecificPath

    proxy /SpecificPath server:8083 {
        proxy_header Host {host}
    }

}
1 Like

@abiosoft that does the trick! even the redirect works with this configuration. The external address still have the “SpecficPath”, but the application works like a charm. The redirect is from a login filter.

@David_Hofmann I’m on the same situation you described on your post, working with a Java EE server (Wildfly 10 in this case). This configuration above might help.

Thanks for the support! Looking forward to use caddy as the main entrance web server here in the company.

1 Like

Not Really @Caramujo, thanks to catch up though. The configuration abobe will work except for the sessions, sessiones will not work because your server is using a cookie session with path /SpecificPath and the browser is accessing just the root “/” which caddy rewrites to your /SpecifPath, so sessions are broken. Unless there is some way to have some directive like 'rewritecookiepath /SpecificPath / ', which is not there at the moment

@Caramujo I actually solved the cookie problem with this code

@WebListener
public class ConfigListener implements ServletContextListener {

    public static final boolean prod = true;
    
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        SessionCookieConfig scc =
                servletContext.getSessionCookieConfig();
        scc.setPath(prod ? "/" : servletContext.getContextPath());

    }

    public void contextDestroyed(ServletContextEvent sce) {
       
    }
} 

so when in production it uses “/” as cookie path and not “/Specificpath”

2 Likes

@David_Hofmann the approach here keeps the “SpecificPath” on the external address, yor cookie workaround is not necessary.

I couldn’t make it work like:

  • Client enter url: http://external.address.com
  • Caddy proxy the request to http://server:8083/SpecificPath
  • The application redirect the user to /SpecificaPath/login
  • Caddy translates that to http://external.address.com/login <= That does not happen

The Caddyfile proposed by @abiosoft does this:

  • Client enter url: http://external.address.com
  • Caddy rewrite the request to /SpecificPath
  • Caddy then proxy the request to http://server:8083/SpecificPath
  • The application redirect the user to /SpecificaPath/login
  • Caddy translates that to http://external.address.com/SpecificPath/login <= “SpecificPath” kept
1 Like

@Caramujo Ok I understand. In my case I needed to hide “/SpecifPath” just to make the url look prettier.

Yup, that was my motivation too.

Beside the session problem, it worked? I’ve tried your config without success…

If it did work, your application have any redirects, or some login filter?

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