Search code examples
gonetwork-programmingmicroservicesreverse-proxytraefik

Traefik v2 reverse proxy without Docker


I have a dead simple Golang microservice (no Docker, just simple binary file) which returns simple message on GET-request.

curl -XGET 'http://localhost:36001/api/operability/list'

{"message": "ping 123"}

Now I want to do reverse proxy via Traefik-v2, so I've made configuration file "traefik.toml":

[global]
  checkNewVersion = false
  sendAnonymousUsage = false

[entryPoints]
    [entryPoints.web]
    address = ":8090"

    [entryPoints.traefik]
    address = ":8091"

[log]
    level = "DEBUG"
    filePath = "logs/traefik.log"
[accessLog]
    filePath = "logs/access.log"

[api]
    insecure = true
    dashboard = true

[providers]
  [providers.file]
    filename = "traefik.toml"

# dynamic conf
[http]
    [http.routers]
        [http.routers.my-router]
            rule = "Path(`/proxy`)"
            service = "my-service"
            entryPoints = ["web"]
    [http.services]
        [http.services.my-service.loadBalancer]
            [[http.services.my-service.loadBalancer.servers]]
                url = "http://localhost:36001"

Starting Traefik (I'm using binary distribution):

traefik --configFile=traefik.toml

Now dashboard on port 8091 works like a charm, but I struggle with reverse proxy request. I suppose it should look like this (based on my configuration file):

curl -XGET 'http://localhost:8090/proxy/api/operability/list'

But all I get it's just:

404 page not found

The question is: is there any mistake in configuration file or is it just a request typo?

edit: My configuration file is based on answers in this questions:

  1. Simple reverse proxy example with Traefik
  2. Traefik v2 as a reverse proxy without docker

edit #2: Traefik version info:

traefik version
Version:      2.4.9
Codename:     livarot
Go version:   go1.16.5
Built:        2021-06-21T16:17:58Z
OS/Arch:      windows/amd64

Solution

  • I've managed to find the answer.

    1. I'm not that smart if I've decided that Traefik would take /proxy and simply redicrect all request to /api/*. The official docs (https://doc.traefik.io/traefik/routing/routers/) says that (I'm quoting):

    Use Path if your service listens on the exact path only. For instance, Path: /products would match /products but not /products/shoes.

    Use a Prefix matcher if your service listens on a particular base path but also serves requests on sub-paths. For instance, PathPrefix: /products would match /products but also /products/shoes and /products/shirts. Since the path is forwarded as-is, your service is expected to listen on /products.

    1. I did not use any middleware for replacing substring of path

    Now answer as example.

    First at all: code for microservice in main.go file

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "{\"message\": \"ping 123\"}")
    }
    
    func main() {
        http.HandleFunc("/operability/list", handler)
        log.Fatal(http.ListenAndServe(":36001", nil))
    }
    

    Now, configuration file for Traefik v2 in config.tom file:

    [global]
      checkNewVersion = false
      sendAnonymousUsage = false
    
    [entryPoints]
        [entryPoints.web]
        address = ":36000"
    
        [entryPoints.traefik]
        address = ":8091"
    
    [log]
        level = "DEBUG"
        filePath = "logs/traefik.log"
    [accessLog]
        filePath = "logs/access.log"
    
    [api]
        insecure = true
        dashboard = true
    
    [providers]
      [providers.file]
        debugLogGeneratedTemplate = true
        # Point this same file for dynamic configuration
        filename = "config.toml"
        watch = true
    
    [http]
        [http.middlewares]
            [http.middlewares.test-replacepathregex.replacePathRegex]
                # We need middleware to replace all "/proxy/" with "/api/"
                regex = "(?:^|\\W)proxy(?:$|\\W)"
                replacement = "/api/"
    
        [http.routers]
            [http.routers.my-router]
                # We need to handle all request with pathes defined as "/proxy/*"
                rule = "PathPrefix(`/proxy/`)"
                service = "my-service"
                entryPoints = ["web"]
                # Use of defined middleware for path replacement
                middlewares = ["test-replacepathregex"]
    
        [http.services]
            [http.services.my-service.loadBalancer]
                [[http.services.my-service.loadBalancer.servers]]
                    url = "http://localhost:36001/"
    

    Start microservice:

    go run main.go
    

    Start traefik:

    traefik --configFile config.toml
    

    Now check if microservice works correctly:

    curl -XGET 'http://localhost:36001/api/operability/list'

    {"message": "ping 123"}

    And check if Traefik v2 does job well too:

    curl -XGET 'http://localhost:36000/proxy/operability/list'

    {"message": "ping 123"}