From 04d473b3e533db0cc1b9abed28b9953941da499e Mon Sep 17 00:00:00 2001 From: Abhilash Gnan Date: Mon, 27 May 2019 17:47:29 +0200 Subject: [PATCH] HTTP Server: Serve Grafana with a custom URL path prefix (#17048) Adds a new [server] setting `serve_from_sub_path`. By enabling this setting and using a subpath in `root_url` setting, e.g. `root_url = http://localhost:3000/grafana`, Grafana will be accessible on `http://localhost:3000/grafana`. By default it is set to `false` for compatibility reasons. Closes #16623 --- conf/defaults.ini | 3 +++ conf/sample.ini | 3 +++ docs/sources/installation/configuration.md | 9 +++++++++ pkg/api/http_server.go | 6 +++++- pkg/setting/setting.go | 17 +++++++++++------ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index ca49f121269..ec83a5ea1a5 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -47,6 +47,9 @@ enforce_domain = false # The full public facing url root_url = %(protocol)s://%(domain)s:%(http_port)s/ +# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons. +serve_from_sub_path = false + # Log web requests router_logging = false diff --git a/conf/sample.ini b/conf/sample.ini index de684bc98f6..dc7d5bc5467 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -48,6 +48,9 @@ # If you use reverse proxy and sub path specify full url (with sub path) ;root_url = http://localhost:3000 +# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons. +;serve_from_sub_path = false + # Log web requests ;router_logging = false diff --git a/docs/sources/installation/configuration.md b/docs/sources/installation/configuration.md index a865234ebeb..af0261032d4 100644 --- a/docs/sources/installation/configuration.md +++ b/docs/sources/installation/configuration.md @@ -154,6 +154,15 @@ callback URL to be correct). > in front of Grafana that exposes it through a subpath. In that > case add the subpath to the end of this URL setting. +### serve_from_sub_path + +Serve Grafana from subpath specified in `root_url` setting. By +default it is set to `false` for compatibility reasons. + +By enabling this setting and using a subpath in `root_url` above, e.g. +`root_url = http://localhost:3000/grafana`, Grafana will be accessible on +`http://localhost:3000/grafana`. + ### static_root_path The path to the directory where the front end files (HTML, JS, and CSS diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 7ec4fbaa3b3..d2094b33cb1 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -30,7 +30,7 @@ import ( "github.com/grafana/grafana/pkg/setting" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "gopkg.in/macaron.v1" + macaron "gopkg.in/macaron.v1" ) func init() { @@ -227,6 +227,10 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() { m.Use(middleware.AddDefaultResponseHeaders()) + if setting.ServeFromSubPath && setting.AppSubUrl != "" { + m.SetURLPrefix(setting.AppSubUrl) + } + m.Use(macaron.Renderer(macaron.RenderOptions{ Directory: path.Join(setting.StaticRootPath, "views"), IndentJSON: macaron.Env != macaron.PROD, diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 65e4e0e2502..a6c07d232e1 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -47,10 +47,11 @@ var ( var ( // App settings. - Env = DEV - AppUrl string - AppSubUrl string - InstanceName string + Env = DEV + AppUrl string + AppSubUrl string + ServeFromSubPath bool + InstanceName string // build BuildVersion string @@ -205,8 +206,9 @@ type Cfg struct { Logger log.Logger // HTTP Server Settings - AppUrl string - AppSubUrl string + AppUrl string + AppSubUrl string + ServeFromSubPath bool // Paths ProvisioningPath string @@ -610,8 +612,11 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error { if err != nil { return err } + ServeFromSubPath = server.Key("serve_from_sub_path").MustBool(false) + cfg.AppUrl = AppUrl cfg.AppSubUrl = AppSubUrl + cfg.ServeFromSubPath = ServeFromSubPath Protocol = HTTP protocolStr, err := valueAsString(server, "protocol", "http")