Caramujo
(Jorge Luiz Correa Bernhard Tautz)
July 28, 2016, 7:21pm
1
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?
1 Like
matt
(Matt Holt)
July 29, 2016, 5:45am
2
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?
Caramujo
(Jorge Luiz Correa Bernhard Tautz)
July 29, 2016, 11:34am
3
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.
matt
(Matt Holt)
July 30, 2016, 9:28pm
4
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
Caramujo
(Jorge Luiz Correa Bernhard Tautz)
July 31, 2016, 3:15pm
5
Aside from the redirect, the specific path reverse proxy as I mention on the first post is possible?
matt
(Matt Holt)
August 1, 2016, 8:45pm
6
Not that I know of; @abiosoft , do you know any clever tricks for something like that? Never heard of this before.
abiosoft
(Abiola Ibrahim)
August 1, 2016, 9:12pm
7
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}
}
}
2 Likes
Caramujo
(Jorge Luiz Correa Bernhard Tautz)
August 2, 2016, 11:22am
8
@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
Caramujo
(Jorge Luiz Correa Bernhard Tautz)
August 2, 2016, 4:50pm
11
@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.
Caramujo
(Jorge Luiz Correa Bernhard Tautz)
August 2, 2016, 6:02pm
13
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?
system
(system)
Closed
October 31, 2016, 6:02pm
14
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.