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

🤗 [Question]: How to use Balancer with https? #2307

Closed
3 tasks done
rosszfej opened this issue Jan 19, 2023 · 9 comments
Closed
3 tasks done

🤗 [Question]: How to use Balancer with https? #2307

rosszfej opened this issue Jan 19, 2023 · 9 comments

Comments

@rosszfej
Copy link

rosszfej commented Jan 19, 2023

Question Description

I want to create a simple load balancer using Go. I have created a test version on my PC for HTTP using the examples on the https://docs.gofiber.io/api/middleware/proxy and it was working fine. I modified it for HTTPS and tried it on the production server but I was getting HTTP-500 response with the following text all the time:
HostClient can't follow redirects to a different protocol, please use Client instead

I googled this text and found that it's from fasthttp:
ErrHostClientRedirectToDifferentScheme = errors.New("HostClient can't follow redirects to a different protocol, please use Client instead")

I've found this bug report in fasthttp, which mentions ErrHostClientRedirectToDifferentScheme but I am not sure if this is related and even it is, I am not sure how to apply the workaround mentioned there in fiber:
valyala/fasthttp#841

( I have modified my https implementation to work as a simple web server and it was working fine (there is no problem with the certificates, etc.. ), so i am pretty much sure that the problem is in the Balancer)

This is the relevant part of my source code:

proxy.WithTlsConfig(&tls.Config{
	InsecureSkipVerify: true,
})

proxy.WithClient(&fasthttp.Client{
	NoDefaultUserAgentHeader: true,
	DisablePathNormalizing:   true,
})

app.Use(proxy.Balancer(
	proxy.Config{
		Servers: []string{"https://myproductionserver.eu"},
	}))
cer, err := tls.LoadX509KeyPair(os.Getenv("CERT_FILE"), os.Getenv("KEY_FILE"))
if err != nil {
	log.Fatal(err)
}

config := &tls.Config{Certificates: []tls.Certificate{cer}, InsecureSkipVerify: true}

ln, err := tls.Listen("tcp", ":"+os.Getenv("SERVER_PORT"), config)
if err != nil {
	panic(err)
}

log.Fatal(app.Listener(ln))

Really appreciate your help with this..

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@welcome
Copy link

welcome bot commented Jan 19, 2023

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@rosszfej
Copy link
Author

i have found the same issue was reported a while ago: #1855
and in the comments someone wrote : Currently, proxy.Balancer works only for http -> HTTP.
it would be nice to have this info added to the documentation

@li-jin-gou
Copy link
Contributor

li-jin-gou commented Jan 20, 2023

refer to https:/gofiber/fiber/blob/master/middleware/proxy/config.go#L51 and you can set tls.config when use Balancer or LbClient.

@rosszfej
Copy link
Author

thanks for the answer. I am a bit confused. On the other thread, someone wrote this was not implemented. you suggested a solution which does not work for me. is this feature working, or is it only me having problems getting it working? thank you very much..

@li-jin-gou
Copy link
Contributor

thanks for the answer. I am a bit confused. On the other thread, someone wrote this was not implemented. you suggested a solution which does not work for me. is this feature working, or is it only me having problems getting it working? thank you very much..

I'll try

@ryanbekhen ryanbekhen mentioned this issue Feb 3, 2023
11 tasks
@ryanbekhen
Copy link
Member

@rosszfej Maybe you could try using the proxy.BalancerForward middleware in version v2.42.0.

@mstoetzer
Copy link

While this is technically interesting to implement it like you did, I would suggest to let other tools do the load balancing and TLS termination (e.g. Nginx, HAProxy or Traefik). Is it technically possible for you to use such tools or are you needed to implement the things as part of your application?

For example, your application needs to be restarted, when you change your TLS certificate to load the new certificate file.

@gaby
Copy link
Member

gaby commented Feb 13, 2023

That's true, it could also be done using a Proxy. It does no harm to implement this on Fiber itself though.

@rosszfej
Copy link
Author

hi all, thanks for the answers.. what finally did, i used nginx to handle https and used the HTTP load balancer in fiber. it's working fine this way.. thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants