Reverse Proxy

How to use Echo as a reverse proxy server?

This recipe demonstrates how you can use Echo as a reverse proxy server and load balancer in front of your favorite applications like WordPress, Node.js, Java, Python, Ruby or even Go. For simplicity, I will use Go upstream servers with WebSocket.

Step 1: Identify upstream target URLs

  1. url1, err := url.Parse("http://localhost:8081")
  2. if err != nil {
  3. e.Logger.Fatal(err)
  4. }
  5. url2, err := url.Parse("http://localhost:8082")
  6. if err != nil {
  7. e.Logger.Fatal(err)
  8. }

Step 2: Setup proxy middleware with upstream targets

In the following code snippet we are using round-robin load balancing technique. You may also use middleware.RandomBalancer.

  1. e.Use(middleware.Proxy(&middleware.RoundRobinBalancer{
  2. Targets: []*middleware.ProxyTarget{
  3. &middleware.ProxyTarget{
  4. URL: url1,
  5. },
  6. &middleware.ProxyTarget{
  7. URL: url2,
  8. },
  9. },
  10. }))

To setup proxy for a sub-route use Echo#Group().

  1. g := e.Group("/blog")
  2. g.Use(middleware.Proxy(...))

Step 3: Start upstream servers

  • cd upstream
  • go run server.go server1 :8081
  • go run server.go server2 :8082

Step 3: Start the proxy server

  1. go run server.go

Step 4: Browse to http://localhost:1323

You should see a webpage with HTTP request being served from “server 1” and WebSocket request from “server 2”.

  1. HTTP
  2. Hello from upstream server server1
  3. WebSocket
  4. Hello from upstream server server2!
  5. Hello from upstream server server2!
  6. Hello from upstream server server2!

Source Code

upstream/server.go

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "time"
  7. "github.com/labstack/echo"
  8. "github.com/labstack/echo/middleware"
  9. "golang.org/x/net/websocket"
  10. )
  11. var index = `
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="UTF-8">
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  18. <title>Upstream Server</title>
  19. <style>
  20. h1, p {
  21. font-weight: 300;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>HTTP</h1>
  27. <p>
  28. Hello from upstream server %s
  29. </p>
  30. <h1>WebSocket</h1>
  31. <p id="output"></p>
  32. <script>
  33. var ws = new WebSocket('ws://localhost:1323/ws')
  34. ws.onmessage = function(evt) {
  35. var out = document.getElementById('output');
  36. out.innerHTML += evt.data + '<br>';
  37. }
  38. </script>
  39. </body>
  40. </html
  41. `
  42. func main() {
  43. name := os.Args[1]
  44. port := os.Args[2]
  45. e := echo.New()
  46. e.Use(middleware.Recover())
  47. e.Use(middleware.Logger())
  48. e.GET("/", func(c echo.Context) error {
  49. return c.HTML(http.StatusOK, fmt.Sprintf(index, name))
  50. })
  51. // WebSocket handler
  52. e.GET("/ws", func(c echo.Context) error {
  53. websocket.Handler(func(ws *websocket.Conn) {
  54. defer ws.Close()
  55. for {
  56. // Write
  57. err := websocket.Message.Send(ws, fmt.Sprintf("Hello from upstream server %s!", name))
  58. if err != nil {
  59. e.Logger.Error(err)
  60. }
  61. time.Sleep(1 * time.Second)
  62. }
  63. }).ServeHTTP(c.Response(), c.Request())
  64. return nil
  65. })
  66. e.Logger.Fatal(e.Start(port))
  67. }

server.go

  1. package main
  2. import (
  3. "net/url"
  4. "github.com/labstack/echo"
  5. "github.com/labstack/echo/middleware"
  6. )
  7. func main() {
  8. e := echo.New()
  9. e.Use(middleware.Recover())
  10. e.Use(middleware.Logger())
  11. // Setup proxy
  12. url1, err := url.Parse("http://localhost:8081")
  13. if err != nil {
  14. e.Logger.Fatal(err)
  15. }
  16. url2, err := url.Parse("http://localhost:8082")
  17. if err != nil {
  18. e.Logger.Fatal(err)
  19. }
  20. e.Use(middleware.Proxy(&middleware.RoundRobinBalancer{
  21. Targets: []*middleware.ProxyTarget{
  22. &middleware.ProxyTarget{
  23. URL: url1,
  24. },
  25. &middleware.ProxyTarget{
  26. URL: url2,
  27. },
  28. },
  29. }))
  30. e.Logger.Fatal(e.Start(":1323"))
  31. }

Maintainers