1.什么是反向代理
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
2.常用用场景
网关
Web服务器
3.常用软件
nginx
4.Go实现反向代理代理
import ( "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "net/http" "net/http/httputil" "strconv" "strings" ) func main() { app := gin.Default() app.GET("/ping", WithHeader) app.Run(":8080") } func WithHeader(ctx *gin.Context) { cp := ctx.Copy() cp.Request.URL.Path = "user/ping" var simpleHostProxy = httputil.ReverseProxy{ Director: func(req *http.Request) { req.URL.Scheme = "http" req.URL.Host = "127.0.0.1" req.Host = "127.0.0.1" }, } simpleHostProxy.ServeHTTP(ctx.Writer, cp.Request) }