oxy icon indicating copy to clipboard operation
oxy copied to clipboard

Client Host header not sent

Open martensson opened this issue 10 years ago • 10 comments

Hi @klizhentas !

First thanks for this great library and open sourcing it. I am currently using it in a different project (https://github.com/martensson/moxy) and it's working great so far. But I encountered a deal breaking bug that would be nice to fix, which is that oxy does not respect the clients Host header being sent. This causes problems when you want to route traffic to nodes that respond differently depending on vhost settings.

Being a transparent reverse proxy the library should maintain all header as they are unless its a way to override them at least. I checked the code and saw a quick fix to the problem, and there is a PR that you can check out.

If you believe there is a better way of doing it please tell me, if not I will have to fork oxy just because of this small fix, and I hope not :/

Thanks!

Best regards, Benjamin

martensson avatar Jun 29 '15 06:06 martensson

@martensson Is the Host header the only one that doesn't pass to the origins?

KensoDev avatar Jul 01 '15 01:07 KensoDev

@KensoDev from my tests it seems all other Headers are passed just fine, it's just the Host header that is handled differently by Go for reasons I still don't know why. There are a few exceptions thought (https://github.com/mailgun/oxy/blob/master/forward/headers.go#L22-L31) that should normally be safe to remove.

But the X-Forwarded-Host that is added by oxy is also wrong, since its just a copy of the normal Host Header.

I did some more digging into the code and saw that we can actually just remove the following two lines: https://github.com/mailgun/oxy/blob/master/forward/fwd.go#L125-L126 and it would solve the whole problem. Even better than my previous PR and saves two unnecessary lines of code. :)

martensson avatar Jul 01 '15 08:07 martensson

+1, have this same problem. currently we hacked in a patch to copy the host header and XFH from the original request, not from the target url:

--- a/Godeps/_workspace/src/github.com/mailgun/oxy/forward/fwd.go
+++ b/Godeps/_workspace/src/github.com/mailgun/oxy/forward/fwd.go
@@ -147,5 +147,11 @@ func (f *Forwarder) copyRequest(req *http.Request, u *url.URL) *http.Request {
        if f.rewriter != nil {
                f.rewriter.Rewrite(outReq)
        }
+
+       // TODO(pquerna): HACK HACK HACK HACK HACK HACK
+       // We need to fix Header Host mapping in vulcan.
+       outReq.Header.Set(XForwardedHost, req.Host)
+       outReq.Host = req.Host
+
        return outReq
 }

pquerna avatar Aug 11 '15 18:08 pquerna

CC @klizhentas

pquerna avatar Aug 11 '15 18:08 pquerna

@pquerna this is already fixed in a PR some weeks ago :)

martensson avatar Aug 12 '15 07:08 martensson

Hi! I'm still experiencing the same bug. Is it really fixed in that PR?

uaalto avatar Oct 02 '15 09:10 uaalto

@uaalto it should work fine, did you set the option? https://godoc.org/github.com/mailgun/oxy/forward#PassHostHeader

martensson avatar Oct 05 '15 11:10 martensson

Hi, Yes I did. However, I ended up building a custom middleware because I needed further control, but IRCC, the URL had an empty host. It might have been caused by any of my other middlewares, so unless other users have the same issue again I think it can be ignored.

Thanks!

uaalto avatar Oct 05 '15 11:10 uaalto

hmm I am running my own proxy with oxy in the core without problem, and I am getting the client host headers as well. Well I am glad it worked out for you! :)

martensson avatar Oct 05 '15 12:10 martensson

couldn't get it working in vulcan, so we are still applying the patch to oxy.

pquerna avatar Oct 05 '15 23:10 pquerna