🦁 goproxy is a proxy server which can forward http or https requests to remote servers./ goproxy 是一个反向代理服务器,支持转发 http/https 请求。
Engilish|🇨🇳中文
goproxy is a load-balancing, reverse-proxy server implemented in go, supporting cache( in memory or Redis). As a load-balancing server, it supports 4 algorithms: Randomized Algorithm, Weight Round Robin Algorithm, Power of Two Choices (P2C) Algorithm, IP Hash Algorithm, Consistent Hashing with Bounded Loads Algorithm, besides, goproxy can dominate the http requests: filtering and blocking specific requests and even rewriting them.
Sometimes your program needs to call some third party API and wants to customize the responses from it, in that case, goproxy will be your great choice.
go get github.com/panjf2000/goproxy
Besides, you also need a Redis to support caching responses if you enable Redis config in goproxy.
cd $GOPATH/src/github.com/panjf2000/goproxy
go build
goproxy uses cfg.toml as its configurations file which is located in /etc/proxy/cfg.toml
of your server, you should create a cfg.toml in there previously, here is a typical example:
# toml file for goproxy
title = "TOML config for goproxy"
[server]
port = ":8080"
reverse = true
proxy_pass = ["127.0.0.1:6000"]
# 0 - random, 1 - loop, 2 - power of two choices(p2c), 3 - hash, 4 - consistent hashing, 5 - least load
inverse_mode = 2
auth = false
cache = true
cache_timeout = 60
cache_type = "redis"
log = 1
log_path = "./logs"
user = { agent = "proxy" }
http_read_timeout = 10
http_write_timeout = 10
[redis]
redis_host = "localhost:6379"
redis_pass = ""
max_idle = 5
idle_timeout = 10
max_active = 10
[mem]
capacity = 1000
cache_replacement_policy = "LRU"
There should be a binary named goproxy
as the same of project name after executing the go build
command and that binary can be run directly to start a goproxy server.
The running goproxy server listens in the port set in cfg.toml and it will forward your http requests to the back-end servers set in cfg.toml by going through that port in goproxy.
Up to present, goproxy has implemented all basic functionalities like reverse-proxy, load-blancing, http caching, http requests controlling, etc and if you want to customize the responses more accurately, you can implement a new handler by inheriting (not a strict statement as there is no OO in golang) from the ProxyServer struct located in handlers/proxy.go and overriding its method named ServeHTTP, then you are allowed to write your own logic into it.