Skip to content

Commit 52e2f0d

Browse files
authored
Merge pull request #13 from Muxi-X/feat/https
fix: autocert error
2 parents 0faebb0 + 66f6213 commit 52e2f0d

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

cmd/cixing-gateway/main.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ func main() {
120120
func newReverseProxy(backend *url.URL) http.Handler {
121121
proxy := httputil.NewSingleHostReverseProxy(backend)
122122

123-
// Keep upstream path exactly as requested.
124-
proxy.Rewrite = func(r *httputil.ProxyRequest) {
125-
r.SetURL(backend)
126-
// Preserve original path + query.
127-
r.Out.URL.Path = r.In.URL.Path
128-
r.Out.URL.RawPath = r.In.URL.RawPath
129-
r.Out.URL.RawQuery = r.In.URL.RawQuery
130-
r.Out.Host = backend.Host
131-
r.Out.Header.Set("X-Forwarded-Host", r.In.Host)
132-
r.Out.Header.Set("X-Forwarded-Proto", "https")
123+
// NewSingleHostReverseProxy sets a default Director.
124+
// Override it here (and DO NOT set Rewrite) to keep Go 1.26+ happy.
125+
proxy.Director = func(req *http.Request) {
126+
forwardedHost := req.Host
127+
128+
// Keep req.URL.Path / RawPath / RawQuery intact.
129+
req.URL.Scheme = backend.Scheme
130+
req.URL.Host = backend.Host
131+
req.Host = backend.Host
132+
133+
req.Header.Set("X-Forwarded-Host", forwardedHost)
134+
req.Header.Set("X-Forwarded-Proto", "https")
133135
}
134136

135137
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
@@ -148,11 +150,7 @@ func newReverseProxy(backend *url.URL) http.Handler {
148150
return nil
149151
}
150152

151-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
152-
// Normalize Host for upstream routing.
153-
r.Host = backend.Host
154-
proxy.ServeHTTP(w, r)
155-
})
153+
return proxy
156154
}
157155

158156
func redactedURLString(u *url.URL) string {

0 commit comments

Comments
 (0)