Add parameter to control mounting behavior

Some load balancers will strip prefixes on their way to the chronograf
backend, others won't. The "--prefix-routes" parameter forces all
requests to the backend to have the prefix specified in "--basepath".
Omitting it will only cause routes to be rewritten in rendered
templates and assumes that the load balancer will remove the prefix.

Use with Caddy
==============

An easy way to test this out is using the free Caddy http server at
http://caddyserver.com.

This Caddyfile will work with the options `--basepath /chronograf
--prefix-routes` set:

```
localhost:2020 {
  proxy /chronograf localhost:8888
  log stdout
}
```

This Caddyfile will work with only the option `--basepath /chronograf`
set:

```
localhost:2020 {
  proxy /chronograf localhost:8888 {
    except /chronograf
  }
  log stdout
}
```
pull/1168/head
Tim Raymond 2017-04-03 16:41:05 -04:00
parent f35de37257
commit d75ee187e6
2 changed files with 12 additions and 9 deletions

View File

@ -20,11 +20,12 @@ const (
// MuxOpts are the options for the router. Mostly related to auth.
type MuxOpts struct {
Logger chronograf.Logger
Develop bool // Develop loads assets from filesystem instead of bindata
Basepath string // URL path prefix under which all chronograf routes will be mounted
UseAuth bool // UseAuth turns on Github OAuth and JWT
TokenSecret string
Logger chronograf.Logger
Develop bool // Develop loads assets from filesystem instead of bindata
Basepath string // URL path prefix under which all chronograf routes will be mounted
PrefixRoutes bool // Mounts all backend routes under route specified by the Basepath
UseAuth bool // UseAuth turns on Github OAuth and JWT
TokenSecret string
ProviderFuncs []func(func(oauth2.Provider, oauth2.Mux))
}
@ -44,7 +45,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
// Set route prefix for all routes if basepath is present
var router chronograf.Router
if opts.Basepath != "" {
if opts.Basepath != "" && opts.PrefixRoutes {
router = &MountableRouter{
Prefix: opts.Basepath,
Delegate: hr,
@ -60,7 +61,9 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
// know about the route. This means that we never have unknown
// routes on the server.
hr.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if opts.Basepath != "" {
// The assets handler is always unaware of basepaths, so it needs to always
// be removed before sending requests to it
if opts.Basepath != "" && opts.PrefixRoutes {
r.URL.Path = r.URL.Path[len(opts.Basepath):]
}
compressed.ServeHTTP(rw, r)

View File

@ -14,9 +14,7 @@ import (
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/bolt"
"github.com/influxdata/chronograf/canned"
"github.com/influxdata/chronograf/influx"
"github.com/influxdata/chronograf/layouts"
clog "github.com/influxdata/chronograf/log"
"github.com/influxdata/chronograf/oauth2"
"github.com/influxdata/chronograf/uuid"
@ -71,6 +69,7 @@ type Server struct {
ReportingDisabled bool `short:"r" long:"reporting-disabled" description:"Disable reporting of usage stats (os,arch,version,cluster_id,uptime) once every 24hr" env:"REPORTING_DISABLED"`
LogLevel string `short:"l" long:"log-level" value-name:"choice" choice:"debug" choice:"info" choice:"error" default:"info" description:"Set the logging level" env:"LOG_LEVEL"`
Basepath string `short:"p" long:"basepath" description:"A URL path prefix under which all chronograf routes will be mounted" env:"BASE_PATH"`
PrefixRoutes bool `long:"prefix-routes" description:"Force chronograf server to require that all requests to it are prefixed with the value set in --basepath" env:"PREFIX_ROUTES"`
ShowVersion bool `short:"v" long:"version" description:"Show Chronograf version info"`
BuildInfo BuildInfo
Listener net.Listener
@ -220,6 +219,7 @@ func (s *Server) Serve(ctx context.Context) error {
UseAuth: s.useAuth(),
ProviderFuncs: providerFuncs,
Basepath: basepath,
PrefixRoutes: s.PrefixRoutes,
}, service)
// Add chronograf's version header to all requests