Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse Proxy into Docker containers with Caddy vs Nginx #205

Closed
joelchen opened this issue Jul 29, 2015 · 6 comments
Closed

Reverse Proxy into Docker containers with Caddy vs Nginx #205

joelchen opened this issue Jul 29, 2015 · 6 comments
Labels
discussion 💬 The right solution needs to be found

Comments

@joelchen
Copy link

Here is my experiment of reverse proxy into Docker containers with Caddy vs Nginx.

Docker gateway IP of 172.17.42.1 is used to achieve it on Nginx, rather than linking other Docker containers with Caddy container, because by linking containers it creates dependency/coupling, if one of my Docker containers crashes, Caddy container will fail to start, and I would like my reverse proxy to be up regardless of the health of my Docker containers.

Here is a sample of Nginx configuration:

upstream tomcat {
        server 172.17.42.1:8080;
}

server {
        location /webapp {
                proxy_pass http://tomcat;
        }

This Nginx configuration works for all of requests starting with /webapp. I tried to replicate it on Caddy, and this is the best configuration I could formulate:

0.0.0.0 {
        proxy /webapp 172.17.42.1:8080
}

This Caddy configuration works for some requests like http://example.com/webapp/ and http://example.com/webapp/home/index.jsp. However, it redirects browser to http://172.17.42.1:8080/webapp/ when the request is http://example.com/webapp, and redirects browser to http://172.17.42.1:8080/webapp/home/login.jsp when JavaScript document.myform.submit() is called on <form name="myform" action='/webapp/home/login.jsp' method=POST>. Instead of form submission, request using JavaScript XMLHttpRequest object's open('POST', 'http://' + window.location.host +'/webapp/home/login.jsp', true) followed by send() works.

I know that reverse proxy into Docker containers is not officially supported in Caddy when I discovered this issue, but I hope that the results of my experiment will move things forward.

Warmest Regards,
Joel

@mholt
Copy link
Member

mholt commented Aug 25, 2015

Hey Joel, thanks for looking into this. You say that the browser is being redirected when you use the proxy directive? As in, the browser is getting a 3xx HTTP status code?

@mholt mholt added the discussion 💬 The right solution needs to be found label Sep 17, 2015
@Ekranos
Copy link

Ekranos commented Dec 16, 2015

I guess im late to the party...
I tumbled into the same problem when i want to reverse proxy into Discourse. Acutally i found an article, why nginx was able to do that nicely and caddy can't.
http://www.cyberciti.biz/faq/proxy_redirect-change-replace-location-refresh-response-headers/

Actually nginx modifies the Location header only. I already hardcoded a fix for myself into caddy, but i have absolutely no idea how i could contribute with good code though.

I just added some lines here:
https://github.com/mholt/caddy/blob/master/middleware/proxy/reverseproxy.go#L158
to:

 if lu, err := res.Location(); err == nil {
        if strings.HasPrefix(lu.Host, "localhost") || strings.HasPrefix(lu.Host, "forum.flauschcraft.de") {
            lu.Host = "forum.flauschcraft.de"
            res.Header.Set("Location", lu.String())
        }
    }

This fixes it for me, but i just came to the conclusion for that ugly fix after trying to figure out how it is supposed and failing after a few hours.

Hope it helps.

novaeye added a commit to novaeye/caddy that referenced this issue Dec 18, 2015
@joelchen
Copy link
Author

@mholt I might have used the wrong word. I reverted to Nginx since then, and I do not remember whether the browser was getting a 3xx status code.
@Ekranos Is the fix by @novaeye working for you?

@Ekranos
Copy link

Ekranos commented Jan 11, 2016

@joelchen It doesn't work :(
I still get redirected to localhost:8082..

@abiosoft
Copy link

Is it something can be fixed by adding proxy_header Host {host} as done in this issue #446 ?

@mholt
Copy link
Member

mholt commented Jan 17, 2016

That suggestion should work, so, closing. Unless there's anything else on this.

@mholt mholt closed this as completed Jan 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion 💬 The right solution needs to be found
Projects
None yet
Development

No branches or pull requests

4 participants