mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
Improve web redirection and 404 behavior.
Previously we redirected any non-existent path to the root (or path prefix). The new behavior: With no path prefix: - "" -> "/" - "/biz" -> 404 With path prefix of "/foo/bar": - "" -> "/foo/bar/" - "/" -> "/foo/bar/" - "/foo/bar" -> "/foo/bar/" - "/biz" -> /foo/bar/biz" (anything not starting with the path prefix gets the prefix prepended) - "/foo/bar/biz" -> 404
This commit is contained in:
parent
52ef225b83
commit
fcd33bbfd8
24
web/web.go
24
web/web.go
|
@ -64,9 +64,21 @@ func (ws WebService) ServeForever(pathPrefix string) {
|
|||
http.Error(w, "", 404)
|
||||
}))
|
||||
|
||||
http.Handle(pathPrefix, prometheus.InstrumentHandler(
|
||||
pathPrefix, ws.StatusHandler,
|
||||
))
|
||||
http.HandleFunc("/", prometheus.InstrumentHandlerFunc(pathPrefix, func(rw http.ResponseWriter, req *http.Request) {
|
||||
// The "/" pattern matches everything, so we need to check
|
||||
// that we're at the root here.
|
||||
if req.URL.Path == pathPrefix {
|
||||
ws.StatusHandler.ServeHTTP(rw, req)
|
||||
} else if req.URL.Path == strings.TrimRight(pathPrefix, "/") {
|
||||
http.Redirect(rw, req, pathPrefix, http.StatusFound)
|
||||
} else if !strings.HasPrefix(req.URL.Path, pathPrefix) {
|
||||
// We're running under a prefix but the user requested something
|
||||
// outside of it. Let's see if this page exists under the prefix.
|
||||
http.Redirect(rw, req, pathPrefix+strings.TrimLeft(req.URL.Path, "/"), http.StatusFound)
|
||||
} else {
|
||||
http.NotFound(rw, req)
|
||||
}
|
||||
}))
|
||||
http.Handle(pathPrefix+"alerts", prometheus.InstrumentHandler(
|
||||
pathPrefix+"alerts", ws.AlertsHandler,
|
||||
))
|
||||
|
@ -102,12 +114,6 @@ func (ws WebService) ServeForever(pathPrefix string) {
|
|||
http.Handle(pathPrefix+"-/quit", http.HandlerFunc(ws.quitHandler))
|
||||
}
|
||||
|
||||
if pathPrefix != "/" {
|
||||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, pathPrefix, http.StatusFound)
|
||||
}))
|
||||
}
|
||||
|
||||
log.Infof("Listening on %s", *listenAddress)
|
||||
|
||||
// If we cannot bind to a port, retry after 30 seconds.
|
||||
|
|
Loading…
Reference in a new issue