I'm doing a simple reverse proxy operation, while the server running in the background normally works on cloudflare, I start getting an endless number of "connect: connection refused" errors when I get behind the reverse proxy.
I use standard go libraries, no extras optimization of performance improvement.
There is no sample code that I can share here, because I did a very simple process..
The website I'm dealing with receives 4-5 million requests per day, and there is an incredible number of requests live. I had no problems with standard websites, but a website of this scale hard me. Is there a trick you can recommend me?
Jun 19 03:44:33 ubuntu main[492935]: 2021/06/19 03:44:33 http: proxy error: dial tcp xx.xxx.x.xx:80: connect: connection refused
Jun 19 03:44:33 ubuntu main[492935]: 2021/06/19 03:44:33 http: proxy error: dial tcp xx.xxx.x.xx:80: connect: connection refused
... repeated many many times ...
Edit post; After the comments, I wanted to share my code as well.
package main
import (
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
type server struct {
proxy *httputil.ReverseProxy
}
func handler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := &server{}
ob := &resp{ResponseWriter: w}
s.ServeHTTP(ob, r)
log.Printf("%s %s \"%s %s %s\" %d %d \"%s\" \"%s\"", r.RemoteAddr, r.Host, r.Method, r.URL.Path, r.Proto, ob.status, ob.written, r.Referer(), r.UserAgent())
})
}
type resp struct {
http.ResponseWriter
status int
written uint64
wroteHeader bool
}
func (o *resp) Write(p []byte) (bytes int, err error) {
if !o.wroteHeader {
o.WriteHeader(http.StatusOK)
}
bytes, err = o.ResponseWriter.Write(p)
o.written += uint64(bytes)
return
}
func (o *resp) WriteHeader(code int) {
o.ResponseWriter.WriteHeader(code)
if o.wroteHeader {
return
}
o.wroteHeader = true
o.status = code
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
backend, _ := url.Parse("http://10.0.0.10")
s.proxy = httputil.NewSingleHostReverseProxy(backend)
s.proxy.ServeHTTP(w, r)
}
func main() {
base := &server{}
http.Handle("/", base)
s := &http.Server{
Addr: ":80",
Handler: handler(http.DefaultServeMux),
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
ErrorLog: log.New(ioutil.Discard, "", 0),
}
log.Panicln(s.ListenAndServe())
}