How to set up reverse proxy server with routes to other servers?


#1

Think about this situation where i have my main website i.e
https://localhost:1323 – my main website.
Let’s say i have WordPress or another server running my blog. In this situation i want my blog to appear as a link from my main website or appear as part of my main site i.e

https://localhost:1323/blog – where blog is another server somewhere.

How do i set up my proxy server this way?

Below is a working example not necessarily using Echo fully.

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
  Scheme: "http",
  Host:   "http://127.0.0.1:8080",
})
e.Any("/blog", echo.WrapHandler(proxy))

#2

Echo lets you define middleware at a group level. So something like below should work:

e := echo.New()
e.Group("/blog*", middleware.Proxy())

#3

Perfect. It works! Just in case someone else needs it –

url1, err := url.Parse("http://127.0.0.1:8080")
if err != nil {
  e.Logger.Fatal(err)
}
e.Group("/blog", middleware.Proxy(middleware.ProxyConfig{
  Balancer: &middleware.RoundRobinBalancer{
    Targets: []*middleware.ProxyTarget{
      &middleware.ProxyTarget{
        URL: url1,
      },
    },
  },
}))

#4

If you don’t want to write code, you can take advantage of project Armor.


#5

What do you recommend for creating a Blog system or CMS to go with a echo website? There doesn’t seem to be many options in Golang


#6

What do you recommend for creating a Blog system or CMS to go with a echo website? There doesn’t seem to be many options in Golang

Can you elaborate? Do you want to use blogging system or create one?


#7

I need to use one for now.


#8

You can use https://gohugo.io. We use it in our blog and documentation.


#9

Thanks, that is what i have been trying to setup.

Also, upon isolating my code further. I found that the proxy code below does not work. I don’t think it ever worked. It shows a not found error. What could be the problem?

url1, err := url.Parse("www.yahoo.com")
if err != nil {
	e.Logger.Fatal(err)
}
e.Group("/myblog", middleware.Proxy(middleware.ProxyConfig{
	Balancer: &middleware.RoundRobinBalancer{
		Targets: []*middleware.ProxyTarget{
			&middleware.ProxyTarget{
				URL: url1,
			},

		},
	},
}))

#10

Try

e.Group("/myblog*")`

#11

Still no luck. Not found error


#12

Probably you are getting 404 from upstream server, try changing it.


#13

It is a 404. But what am i changing?


#14

Try to use localhost upstream server


#15

Upstream server works. I guess i misunderstood what a proxy server does.


#16

I just write this:

package main

import (
	"net/url"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
)

func main() {
	e := echo.New()
	e.Use(middleware.Recover())
	e.Use(middleware.Logger())

	url1, err := url.Parse("www.yahoo.com")
if err != nil {
	e.Logger.Fatal(err)
}
e.Group("/myblog", middleware.Proxy(middleware.ProxyConfig{
	Balancer: &middleware.RoundRobinBalancer{
		Targets: []*middleware.ProxyTarget{
			&middleware.ProxyTarget{
				URL: url1,
			},

		},
	},
}))
	e.Logger.Fatal(e.Start(":1323"))
}

and get this error :
cannot use middleware.ProxyConfig literal (type middleware.ProxyConfig) as type middleware.ProxyBalancer in argument to middleware.Proxy:
middleware.ProxyConfig does not implement middleware.ProxyBalancer (missing Next method)


#17

It should be middleware.ProxyWithConfig


#18

To learn how to do the proxy settings visit https://babasupport.org/apple/iphone-error-4005/ and it is better to know some methods of networking. You will also get to know about it in more details.