https load balancer with fiber is not working

265 Views Asked by At

I am trying to make simple load balancer using fiber in Go. In my computer it's working fine using http.

import (
    "crypto/tls"
    "log"
    "os"

    store "intraGo/stores/session_store"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/proxy"
    "github.com/gofiber/template/html"
)

...

if os.Getenv("CERT_FILE") == "" || os.Getenv("KEY_FILE") == "" {
    // this part is working fine
    app.Use(proxy.Balancer(proxy.Config{Servers: []string{"http://localhost:9000"}}))
    log.Fatal(app.Listen(":" + os.Getenv("SERVER_PORT")))

} else {
    app.Use(proxy.Balancer(proxy.Config{Servers: []string{"https://servername.com"}}))
    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}}

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

    log.Fatal(app.Listener(ln))
}

In the production envirment using https it will give me a http 500 result with the follwing text for every request. The request will never reach the server.

HostClient can't follow redirects to a different protocol, please use Client instead

I checked Go / fiber documentation but did not find anything that could help to solve this issue. I also checked if the https part (like certificates, https server) is working fine by adding this to my app:

app.get("/", (req, res) => { res.send("Hello World!"); });

This worked as expected, so I guess the problem is in the loadbalancer itself.

Found this link while trying to find the solution on the net, I am not sure, but this might be related to the problem: https://github.com/valyala/fasthttp/issues/841

1

There are 1 best solutions below

0
rszf On

After asking the same question on the github page of the package it became clear that https load balancer is not supported.

In case of load balancing and https, it's better to separate the functionalities, like either use nginx to handle https and send HTTP traffic to the application, or handle both https and loadbalancing in nginx.