Support subpaths when serving stash through a reverse proxy (#1719)

* Support subpaths when serving stash through a reverse proxy
* Add README documentation

Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
InfiniteTF
2021-09-21 06:12:10 +02:00
committed by GitHub
parent f3c8407c40
commit a4ed9515c7
9 changed files with 44 additions and 9 deletions

View File

@@ -258,7 +258,15 @@ func Start() {
if ext == ".html" || ext == "" {
data, _ := uiBox.Find("index.html")
_, _ = w.Write(data)
prefix := ""
if r.Header.Get("X-Forwarded-Prefix") != "" {
prefix = strings.TrimRight(r.Header.Get("X-Forwarded-Prefix"), "/")
}
baseURLIndex := strings.Replace(string(data), "%BASE_URL%", prefix+"/", 2)
baseURLIndex = strings.Replace(baseURLIndex, "base href=\"/\"", fmt.Sprintf("base href=\"%s\"", prefix+"/"), 2)
_, _ = w.Write([]byte(baseURLIndex))
} else {
isStatic, _ := path.Match("/static/*/*", r.URL.Path)
if isStatic {
@@ -372,11 +380,22 @@ func BaseURLMiddleware(next http.Handler) http.Handler {
} else {
scheme = "http"
}
baseURL := scheme + "://" + r.Host
prefix := ""
if r.Header.Get("X-Forwarded-Prefix") != "" {
prefix = strings.TrimRight(r.Header.Get("X-Forwarded-Prefix"), "/")
}
port := ""
forwardedPort := r.Header.Get("X-Forwarded-Port")
if forwardedPort != "" && forwardedPort != "80" && forwardedPort != "8080" {
port = ":" + forwardedPort
}
baseURL := scheme + "://" + r.Host + port + prefix
externalHost := config.GetInstance().GetExternalHost()
if externalHost != "" {
baseURL = externalHost
baseURL = externalHost + prefix
}
r = r.WithContext(context.WithValue(ctx, BaseURLCtxKey, baseURL))