2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-04 11:49:50 -05:00
|
|
|
package reverseproxy
|
|
|
|
|
|
|
|
import (
|
2019-11-15 19:15:33 -05:00
|
|
|
"bytes"
|
2019-05-04 11:49:50 -05:00
|
|
|
"context"
|
2019-09-02 23:01:02 -05:00
|
|
|
"encoding/json"
|
2020-11-25 12:54:23 -05:00
|
|
|
"errors"
|
2019-05-04 11:49:50 -05:00
|
|
|
"fmt"
|
2019-11-15 19:15:33 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-05-04 11:49:50 -05:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-09-03 13:10:11 -05:00
|
|
|
"regexp"
|
2020-06-04 13:06:38 -05:00
|
|
|
"strconv"
|
2019-05-04 11:49:50 -05:00
|
|
|
"strings"
|
2019-11-15 19:15:33 -05:00
|
|
|
"sync"
|
2019-05-04 11:49:50 -05:00
|
|
|
"time"
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-09-14 14:25:26 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"
|
2019-10-29 17:02:40 -05:00
|
|
|
"go.uber.org/zap"
|
2019-05-04 11:49:50 -05:00
|
|
|
"golang.org/x/net/http/httpguts"
|
|
|
|
)
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(Handler{})
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 17:56:09 -05:00
|
|
|
// Handler implements a highly configurable and production-ready reverse proxy.
|
2019-12-29 15:16:34 -05:00
|
|
|
//
|
2019-12-23 14:45:35 -05:00
|
|
|
// Upon proxying, this module sets the following placeholders (which can be used
|
|
|
|
// both within and after this handler):
|
|
|
|
//
|
2019-12-29 15:16:34 -05:00
|
|
|
// Placeholder | Description
|
|
|
|
// ------------|-------------
|
|
|
|
// `{http.reverse_proxy.upstream.address}` | The full address to the upstream as given in the config
|
|
|
|
// `{http.reverse_proxy.upstream.hostport}` | The host:port of the upstream
|
|
|
|
// `{http.reverse_proxy.upstream.host}` | The host of the upstream
|
|
|
|
// `{http.reverse_proxy.upstream.port}` | The port of the upstream
|
|
|
|
// `{http.reverse_proxy.upstream.requests}` | The approximate current number of requests to the upstream
|
|
|
|
// `{http.reverse_proxy.upstream.max_requests}` | The maximum approximate number of requests allowed to the upstream
|
|
|
|
// `{http.reverse_proxy.upstream.fails}` | The number of recent failed requests to the upstream
|
2019-09-02 23:01:02 -05:00
|
|
|
type Handler struct {
|
2019-12-23 14:45:35 -05:00
|
|
|
// Configures the method of transport for the proxy. A transport
|
|
|
|
// is what performs the actual "round trip" to the backend.
|
|
|
|
// The default transport is plaintext HTTP.
|
|
|
|
TransportRaw json.RawMessage `json:"transport,omitempty" caddy:"namespace=http.reverse_proxy.transport inline_key=protocol"`
|
|
|
|
|
|
|
|
// A circuit breaker may be used to relieve pressure on a backend
|
|
|
|
// that is beginning to exhibit symptoms of stress or latency.
|
|
|
|
// By default, there is no circuit breaker.
|
|
|
|
CBRaw json.RawMessage `json:"circuit_breaker,omitempty" caddy:"namespace=http.reverse_proxy.circuit_breakers inline_key=type"`
|
|
|
|
|
|
|
|
// Load balancing distributes load/requests between backends.
|
|
|
|
LoadBalancing *LoadBalancing `json:"load_balancing,omitempty"`
|
|
|
|
|
|
|
|
// Health checks update the status of backends, whether they are
|
|
|
|
// up or down. Down backends will not be proxied to.
|
|
|
|
HealthChecks *HealthChecks `json:"health_checks,omitempty"`
|
|
|
|
|
|
|
|
// Upstreams is the list of backends to proxy to.
|
|
|
|
Upstreams UpstreamPool `json:"upstreams,omitempty"`
|
|
|
|
|
2020-04-17 10:53:06 -05:00
|
|
|
// Adjusts how often to flush the response buffer. A
|
|
|
|
// negative value disables response buffering.
|
2019-12-23 14:45:35 -05:00
|
|
|
// TODO: figure out good defaults and write docs for this
|
|
|
|
// (see https://github.com/caddyserver/caddy/issues/1460)
|
|
|
|
FlushInterval caddy.Duration `json:"flush_interval,omitempty"`
|
|
|
|
|
|
|
|
// Headers manipulates headers between Caddy and the backend.
|
2020-04-17 10:53:06 -05:00
|
|
|
// By default, all headers are passed-thru without changes,
|
|
|
|
// with the exceptions of special hop-by-hop headers.
|
|
|
|
//
|
|
|
|
// X-Forwarded-For and X-Forwarded-Proto are also set
|
|
|
|
// implicitly, but this may change in the future if the official
|
|
|
|
// standardized Forwarded header field gains more adoption.
|
2019-12-23 14:45:35 -05:00
|
|
|
Headers *headers.Handler `json:"headers,omitempty"`
|
|
|
|
|
|
|
|
// If true, the entire request body will be read and buffered
|
|
|
|
// in memory before being proxied to the backend. This should
|
|
|
|
// be avoided if at all possible for performance reasons.
|
|
|
|
BufferRequests bool `json:"buffer_requests,omitempty"`
|
2019-09-02 23:01:02 -05:00
|
|
|
|
2020-05-27 11:15:20 -05:00
|
|
|
// List of handlers and their associated matchers to evaluate
|
|
|
|
// after successful roundtrips. The first handler that matches
|
|
|
|
// the response from a backend will be invoked. The response
|
|
|
|
// body from the backend will not be written to the client;
|
|
|
|
// it is up to the handler to finish handling the response.
|
|
|
|
// If passive health checks are enabled, any errors from the
|
|
|
|
// handler chain will not affect the health status of the
|
|
|
|
// backend.
|
|
|
|
//
|
|
|
|
// Two new placeholders are available in this handler chain:
|
|
|
|
// - `{http.reverse_proxy.status_code}` The status code
|
|
|
|
// - `{http.reverse_proxy.status_text}` The status text
|
|
|
|
HandleResponse []caddyhttp.ResponseHandler `json:"handle_response,omitempty"`
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
Transport http.RoundTripper `json:"-"`
|
2019-09-03 20:06:54 -05:00
|
|
|
CB CircuitBreaker `json:"-"`
|
2019-10-29 17:02:40 -05:00
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
ctx caddy.Context
|
2019-10-29 17:02:40 -05:00
|
|
|
logger *zap.Logger
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (Handler) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.handlers.reverse_proxy",
|
|
|
|
New: func() caddy.Module { return new(Handler) },
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:56:09 -05:00
|
|
|
// Provision ensures that h is set up properly before use.
|
2019-09-02 23:01:02 -05:00
|
|
|
func (h *Handler) Provision(ctx caddy.Context) error {
|
2020-08-07 19:02:24 -05:00
|
|
|
h.ctx = ctx
|
2019-10-29 17:02:40 -05:00
|
|
|
h.logger = ctx.Logger(h)
|
|
|
|
|
2020-10-13 11:35:20 -05:00
|
|
|
// verify SRV compatibility
|
2020-10-01 15:05:39 -05:00
|
|
|
for i, v := range h.Upstreams {
|
|
|
|
if v.LookupSRV == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if h.HealthChecks != nil && h.HealthChecks.Active != nil {
|
|
|
|
return fmt.Errorf(`upstream: lookup_srv is incompatible with active health checks: %d: {"dial": %q, "lookup_srv": %q}`, i, v.Dial, v.LookupSRV)
|
|
|
|
}
|
|
|
|
if v.Dial != "" {
|
|
|
|
return fmt.Errorf(`upstream: specifying dial address is incompatible with lookup_srv: %d: {"dial": %q, "lookup_srv": %q}`, i, v.Dial, v.LookupSRV)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:06:54 -05:00
|
|
|
// start by loading modules
|
2019-09-02 23:01:02 -05:00
|
|
|
if h.TransportRaw != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
mod, err := ctx.LoadModule(h, "TransportRaw")
|
2019-09-02 23:01:02 -05:00
|
|
|
if err != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
return fmt.Errorf("loading transport: %v", err)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-12-10 15:36:46 -05:00
|
|
|
h.Transport = mod.(http.RoundTripper)
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
if h.LoadBalancing != nil && h.LoadBalancing.SelectionPolicyRaw != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
mod, err := ctx.LoadModule(h.LoadBalancing, "SelectionPolicyRaw")
|
2019-09-02 23:01:02 -05:00
|
|
|
if err != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
return fmt.Errorf("loading load balancing selection policy: %s", err)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-12-10 15:36:46 -05:00
|
|
|
h.LoadBalancing.SelectionPolicy = mod.(Selector)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-09-03 20:06:54 -05:00
|
|
|
if h.CBRaw != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
mod, err := ctx.LoadModule(h, "CBRaw")
|
2019-09-03 20:06:54 -05:00
|
|
|
if err != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
return fmt.Errorf("loading circuit breaker: %s", err)
|
2019-09-03 20:06:54 -05:00
|
|
|
}
|
2019-12-10 15:36:46 -05:00
|
|
|
h.CB = mod.(CircuitBreaker)
|
2019-09-03 20:06:54 -05:00
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
|
2020-04-22 20:57:06 -05:00
|
|
|
// ensure any embedded headers handler module gets provisioned
|
|
|
|
// (see https://caddy.community/t/set-cookie-manipulation-in-reverse-proxy/7666?u=matt
|
|
|
|
// for what happens if we forget to provision it)
|
|
|
|
if h.Headers != nil {
|
|
|
|
err := h.Headers.Provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("provisioning embedded headers handler: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// set up transport
|
2019-09-02 23:01:02 -05:00
|
|
|
if h.Transport == nil {
|
2019-09-05 14:14:39 -05:00
|
|
|
t := &HTTPTransport{
|
|
|
|
KeepAlive: &KeepAlive{
|
2019-10-12 00:57:11 -05:00
|
|
|
ProbeInterval: caddy.Duration(30 * time.Second),
|
|
|
|
IdleConnTimeout: caddy.Duration(2 * time.Minute),
|
|
|
|
MaxIdleConnsPerHost: 32,
|
2019-09-05 14:14:39 -05:00
|
|
|
},
|
|
|
|
DialTimeout: caddy.Duration(10 * time.Second),
|
|
|
|
}
|
|
|
|
err := t.Provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("provisioning default transport: %v", err)
|
|
|
|
}
|
|
|
|
h.Transport = t
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// set up load balancing
|
2019-09-02 23:01:02 -05:00
|
|
|
if h.LoadBalancing == nil {
|
|
|
|
h.LoadBalancing = new(LoadBalancing)
|
|
|
|
}
|
|
|
|
if h.LoadBalancing.SelectionPolicy == nil {
|
|
|
|
h.LoadBalancing.SelectionPolicy = RandomSelection{}
|
|
|
|
}
|
|
|
|
if h.LoadBalancing.TryDuration > 0 && h.LoadBalancing.TryInterval == 0 {
|
|
|
|
// a non-zero try_duration with a zero try_interval
|
|
|
|
// will always spin the CPU for try_duration if the
|
2019-09-03 13:10:11 -05:00
|
|
|
// upstream is local or low-latency; avoid that by
|
|
|
|
// defaulting to a sane wait period between attempts
|
2019-09-02 23:01:02 -05:00
|
|
|
h.LoadBalancing.TryInterval = caddy.Duration(250 * time.Millisecond)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-12-10 15:36:46 -05:00
|
|
|
lbMatcherSets, err := ctx.LoadModule(h.LoadBalancing, "RetryMatchRaw")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = h.LoadBalancing.RetryMatch.FromInterface(lbMatcherSets)
|
2019-10-05 17:22:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// set up upstreams
|
2019-09-02 23:01:02 -05:00
|
|
|
for _, upstream := range h.Upstreams {
|
2019-10-11 15:25:39 -05:00
|
|
|
// create or get the host representation for this upstream
|
|
|
|
var host Host = new(upstreamHost)
|
2020-03-27 15:29:01 -05:00
|
|
|
existingHost, loaded := hosts.LoadOrStore(upstream.String(), host)
|
2019-10-11 15:25:39 -05:00
|
|
|
if loaded {
|
|
|
|
host = existingHost.(Host)
|
2019-09-11 19:53:44 -05:00
|
|
|
}
|
2019-10-11 15:25:39 -05:00
|
|
|
upstream.Host = host
|
|
|
|
|
|
|
|
// give it the circuit breaker, if any
|
|
|
|
upstream.cb = h.CB
|
|
|
|
|
|
|
|
// if the passive health checker has a non-zero UnhealthyRequestCount
|
|
|
|
// but the upstream has no MaxRequests set (they are the same thing,
|
|
|
|
// but the passive health checker is a default value for for upstreams
|
|
|
|
// without MaxRequests), copy the value into this upstream, since the
|
|
|
|
// value in the upstream (MaxRequests) is what is used during
|
|
|
|
// availability checks
|
2019-10-29 17:02:40 -05:00
|
|
|
if h.HealthChecks != nil && h.HealthChecks.Passive != nil {
|
|
|
|
h.HealthChecks.Passive.logger = h.logger.Named("health_checker.passive")
|
|
|
|
if h.HealthChecks.Passive.UnhealthyRequestCount > 0 &&
|
|
|
|
upstream.MaxRequests == 0 {
|
|
|
|
upstream.MaxRequests = h.HealthChecks.Passive.UnhealthyRequestCount
|
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// upstreams need independent access to the passive
|
|
|
|
// health check policy because passive health checks
|
|
|
|
// run without access to h.
|
|
|
|
if h.HealthChecks != nil {
|
|
|
|
upstream.healthCheckPolicy = h.HealthChecks.Passive
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
if h.HealthChecks != nil {
|
|
|
|
// set defaults on passive health checks, if necessary
|
|
|
|
if h.HealthChecks.Passive != nil {
|
|
|
|
if h.HealthChecks.Passive.FailDuration > 0 && h.HealthChecks.Passive.MaxFails == 0 {
|
|
|
|
h.HealthChecks.Passive.MaxFails = 1
|
|
|
|
}
|
2020-02-23 16:30:52 -05:00
|
|
|
}
|
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
// if active health checks are enabled, configure them and start a worker
|
|
|
|
if h.HealthChecks.Active != nil &&
|
|
|
|
(h.HealthChecks.Active.Path != "" || h.HealthChecks.Active.Port != 0) {
|
|
|
|
h.HealthChecks.Active.logger = h.logger.Named("health_checker.active")
|
2020-02-23 16:30:52 -05:00
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
timeout := time.Duration(h.HealthChecks.Active.Timeout)
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = 5 * time.Second
|
|
|
|
}
|
2020-02-23 16:30:52 -05:00
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
h.HealthChecks.Active.httpClient = &http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
Transport: h.Transport,
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:25:34 -05:00
|
|
|
for _, upstream := range h.Upstreams {
|
|
|
|
// if there's an alternative port for health-check provided in the config,
|
|
|
|
// then use it, otherwise use the port of upstream.
|
|
|
|
if h.HealthChecks.Active.Port != 0 {
|
|
|
|
upstream.activeHealthCheckPort = h.HealthChecks.Active.Port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
if h.HealthChecks.Active.Interval == 0 {
|
|
|
|
h.HealthChecks.Active.Interval = caddy.Duration(30 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.HealthChecks.Active.ExpectBody != "" {
|
|
|
|
var err error
|
|
|
|
h.HealthChecks.Active.bodyRegexp, err = regexp.Compile(h.HealthChecks.Active.ExpectBody)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("expect_body: compiling regular expression: %v", err)
|
|
|
|
}
|
2020-02-23 16:30:52 -05:00
|
|
|
}
|
|
|
|
|
2020-08-07 19:02:24 -05:00
|
|
|
go h.activeHealthChecker()
|
|
|
|
}
|
2020-02-23 16:30:52 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:15:20 -05:00
|
|
|
// set up any response routes
|
|
|
|
for i, rh := range h.HandleResponse {
|
|
|
|
err := rh.Provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("provisioning response handler %d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 13:10:11 -05:00
|
|
|
// Cleanup cleans up the resources made by h during provisioning.
|
2019-09-02 23:01:02 -05:00
|
|
|
func (h *Handler) Cleanup() error {
|
2019-10-10 16:38:30 -05:00
|
|
|
// TODO: Close keepalive connections on reload? https://github.com/caddyserver/caddy/pull/2507/files#diff-70219fd88fe3f36834f474ce6537ed26R762
|
|
|
|
|
2019-09-03 13:10:11 -05:00
|
|
|
// remove hosts from our config from the pool
|
2019-09-02 23:01:02 -05:00
|
|
|
for _, upstream := range h.Upstreams {
|
2020-11-22 16:50:29 -05:00
|
|
|
_, _ = hosts.Delete(upstream.String())
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-09-03 13:10:11 -05:00
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
2019-12-29 15:12:52 -05:00
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-09-14 14:25:26 -05:00
|
|
|
|
2019-11-15 19:15:33 -05:00
|
|
|
// if enabled, buffer client request;
|
|
|
|
// this should only be enabled if the
|
|
|
|
// upstream requires it and does not
|
|
|
|
// work with "slow clients" (gunicorn,
|
|
|
|
// etc.) - this obviously has a perf
|
|
|
|
// overhead and makes the proxy at
|
|
|
|
// risk of exhausting memory and more
|
2019-12-04 18:28:13 -05:00
|
|
|
// susceptible to slowloris attacks,
|
2019-11-15 19:15:33 -05:00
|
|
|
// so it is strongly recommended to
|
|
|
|
// only use this feature if absolutely
|
|
|
|
// required, if read timeouts are set,
|
|
|
|
// and if body size is limited
|
|
|
|
if h.BufferRequests {
|
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer bufPool.Put(buf)
|
2020-11-22 16:50:29 -05:00
|
|
|
_, _ = io.Copy(buf, r.Body)
|
2019-11-15 19:15:33 -05:00
|
|
|
r.Body.Close()
|
|
|
|
r.Body = ioutil.NopCloser(buf)
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// prepare the request for proxying; this is needed only once
|
|
|
|
err := h.prepareRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError,
|
|
|
|
fmt.Errorf("preparing request for upstream round-trip: %v", err))
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:54:58 -05:00
|
|
|
// we will need the original headers and Host value if
|
|
|
|
// header operations are configured; and we should
|
|
|
|
// restore them after we're done if they are changed
|
|
|
|
// (for example, changing the outbound Host header
|
|
|
|
// should not permanently change r.Host; issue #3509)
|
2019-09-14 14:25:26 -05:00
|
|
|
reqHost := r.Host
|
2020-07-17 18:54:58 -05:00
|
|
|
reqHeader := r.Header
|
|
|
|
defer func() {
|
|
|
|
r.Host = reqHost
|
|
|
|
r.Header = reqHeader
|
|
|
|
}()
|
2019-09-14 14:25:26 -05:00
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var proxyErr error
|
|
|
|
for {
|
|
|
|
// choose an available upstream
|
2020-11-20 14:39:26 -05:00
|
|
|
upstream := h.LoadBalancing.SelectionPolicy.Select(h.Upstreams, r, w)
|
2019-09-02 23:01:02 -05:00
|
|
|
if upstream == nil {
|
|
|
|
if proxyErr == nil {
|
2019-09-14 14:25:26 -05:00
|
|
|
proxyErr = fmt.Errorf("no upstreams available")
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
2020-08-07 19:02:24 -05:00
|
|
|
if !h.LoadBalancing.tryAgain(h.ctx, start, proxyErr, r) {
|
2019-09-02 23:01:02 -05:00
|
|
|
break
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// the dial address may vary per-request if placeholders are
|
|
|
|
// used, so perform those replacements here; the resulting
|
|
|
|
// DialInfo struct should have valid network address syntax
|
2020-03-24 11:53:53 -05:00
|
|
|
dialInfo, err := upstream.fillDialInfo(r)
|
2019-10-11 15:25:39 -05:00
|
|
|
if err != nil {
|
2020-11-25 12:54:23 -05:00
|
|
|
return statusError(fmt.Errorf("making dial info: %v", err))
|
2019-10-11 15:25:39 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 14:14:39 -05:00
|
|
|
// attach to the request information about how to dial the upstream;
|
|
|
|
// this is necessary because the information cannot be sufficiently
|
|
|
|
// or satisfactorily represented in a URL
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-09 12:00:13 -05:00
|
|
|
caddyhttp.SetVar(r.Context(), dialInfoVarKey, dialInfo)
|
2019-09-05 14:14:39 -05:00
|
|
|
|
2019-09-14 14:25:26 -05:00
|
|
|
// set placeholders with information about this upstream
|
2019-12-12 16:31:20 -05:00
|
|
|
repl.Set("http.reverse_proxy.upstream.address", dialInfo.String())
|
|
|
|
repl.Set("http.reverse_proxy.upstream.hostport", dialInfo.Address)
|
|
|
|
repl.Set("http.reverse_proxy.upstream.host", dialInfo.Host)
|
|
|
|
repl.Set("http.reverse_proxy.upstream.port", dialInfo.Port)
|
2020-03-30 12:49:53 -05:00
|
|
|
repl.Set("http.reverse_proxy.upstream.requests", upstream.Host.NumRequests())
|
|
|
|
repl.Set("http.reverse_proxy.upstream.max_requests", upstream.MaxRequests)
|
|
|
|
repl.Set("http.reverse_proxy.upstream.fails", upstream.Host.Fails())
|
2019-09-14 14:25:26 -05:00
|
|
|
|
|
|
|
// mutate request headers according to this upstream;
|
|
|
|
// because we're in a retry loop, we have to copy
|
|
|
|
// headers (and the r.Host value) from the original
|
|
|
|
// so that each retry is identical to the first
|
|
|
|
if h.Headers != nil && h.Headers.Request != nil {
|
|
|
|
r.Header = make(http.Header)
|
|
|
|
copyHeader(r.Header, reqHeader)
|
|
|
|
r.Host = reqHost
|
|
|
|
h.Headers.Request.ApplyToRequest(r)
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// proxy the request to that upstream
|
2020-05-27 11:15:20 -05:00
|
|
|
proxyErr = h.reverseProxy(w, r, dialInfo, next)
|
2019-09-03 20:10:09 -05:00
|
|
|
if proxyErr == nil || proxyErr == context.Canceled {
|
|
|
|
// context.Canceled happens when the downstream client
|
2019-10-05 17:22:05 -05:00
|
|
|
// cancels the request, which is not our failure
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:15:20 -05:00
|
|
|
// if the roundtrip was successful, don't retry the request or
|
|
|
|
// ding the health status of the upstream (an error can still
|
|
|
|
// occur after the roundtrip if, for example, a response handler
|
|
|
|
// after the roundtrip returns an error)
|
|
|
|
if succ, ok := proxyErr.(roundtripSucceeded); ok {
|
|
|
|
return succ.error
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// remember this failure (if enabled)
|
|
|
|
h.countFailure(upstream)
|
|
|
|
|
|
|
|
// if we've tried long enough, break
|
2020-08-07 19:02:24 -05:00
|
|
|
if !h.LoadBalancing.tryAgain(h.ctx, start, proxyErr, r) {
|
2019-09-02 23:01:02 -05:00
|
|
|
break
|
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2020-11-25 12:54:23 -05:00
|
|
|
return statusError(proxyErr)
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareRequest modifies req so that it is ready to be proxied,
|
|
|
|
// except for directing to a specific upstream. This method mutates
|
|
|
|
// headers and other necessary properties of the request and should
|
|
|
|
// be done just once (before proxying) regardless of proxy retries.
|
|
|
|
// This assumes that no mutations of the request are performed
|
|
|
|
// by h during or after proxying.
|
|
|
|
func (h Handler) prepareRequest(req *http.Request) error {
|
2019-11-05 18:29:10 -05:00
|
|
|
// most of this is borrowed from the Go std lib reverse proxy
|
2019-09-05 14:14:39 -05:00
|
|
|
|
2019-05-04 11:49:50 -05:00
|
|
|
if req.ContentLength == 0 {
|
2019-09-02 23:01:02 -05:00
|
|
|
req.Body = nil // Issue golang/go#16036: nil Body for http.Transport retries
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
req.Close = false
|
2019-05-04 11:49:50 -05:00
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// if User-Agent is not set by client, then explicitly
|
|
|
|
// disable it so it's not set to default value by std lib
|
|
|
|
if _, ok := req.Header["User-Agent"]; !ok {
|
|
|
|
req.Header.Set("User-Agent", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqUpType := upgradeType(req.Header)
|
|
|
|
removeConnectionHeaders(req.Header)
|
2019-05-04 11:49:50 -05:00
|
|
|
|
|
|
|
// Remove hop-by-hop headers to the backend. Especially
|
|
|
|
// important is "Connection" because we want a persistent
|
|
|
|
// connection, regardless of what the client sent to us.
|
|
|
|
for _, h := range hopHeaders {
|
2019-09-02 23:01:02 -05:00
|
|
|
hv := req.Header.Get(h)
|
2019-05-04 11:49:50 -05:00
|
|
|
if hv == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if h == "Te" && hv == "trailers" {
|
2019-09-02 23:01:02 -05:00
|
|
|
// Issue golang/go#21096: tell backend applications that
|
2019-05-04 11:49:50 -05:00
|
|
|
// care about trailer support that we support
|
|
|
|
// trailers. (We do, but we don't go out of
|
|
|
|
// our way to advertise that unless the
|
|
|
|
// incoming client request thought it was
|
|
|
|
// worth mentioning)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
req.Header.Del(h)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// After stripping all the hop-by-hop connection headers above, add back any
|
|
|
|
// necessary for protocol upgrades, such as for websockets.
|
|
|
|
if reqUpType != "" {
|
2019-09-02 23:01:02 -05:00
|
|
|
req.Header.Set("Connection", "Upgrade")
|
|
|
|
req.Header.Set("Upgrade", reqUpType)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
|
|
|
// If we aren't the first proxy retain prior
|
|
|
|
// X-Forwarded-For information as a comma+space
|
|
|
|
// separated list and fold multiple headers into one.
|
2019-09-02 23:01:02 -05:00
|
|
|
if prior, ok := req.Header["X-Forwarded-For"]; ok {
|
2019-05-04 11:49:50 -05:00
|
|
|
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
req.Header.Set("X-Forwarded-For", clientIP)
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:33:17 -05:00
|
|
|
if req.Header.Get("X-Forwarded-Proto") == "" {
|
|
|
|
// set X-Forwarded-Proto; many backend apps expect this too
|
|
|
|
proto := "https"
|
|
|
|
if req.TLS == nil {
|
|
|
|
proto = "http"
|
|
|
|
}
|
|
|
|
req.Header.Set("X-Forwarded-Proto", proto)
|
2020-04-17 10:53:06 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:56:09 -05:00
|
|
|
// reverseProxy performs a round-trip to the given backend and processes the response with the client.
|
|
|
|
// (This method is mostly the beginning of what was borrowed from the net/http/httputil package in the
|
|
|
|
// Go standard library which was used as the foundation.)
|
2020-05-27 11:15:20 -05:00
|
|
|
func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, di DialInfo, next caddyhttp.Handler) error {
|
2020-11-22 16:50:29 -05:00
|
|
|
_ = di.Upstream.Host.CountRequest(1)
|
|
|
|
//nolint:errcheck
|
2019-10-11 15:25:39 -05:00
|
|
|
defer di.Upstream.Host.CountRequest(-1)
|
2019-09-02 23:01:02 -05:00
|
|
|
|
|
|
|
// point the request to this upstream
|
2019-10-11 15:25:39 -05:00
|
|
|
h.directRequest(req, di)
|
2019-09-02 23:01:02 -05:00
|
|
|
|
2020-05-18 15:08:11 -05:00
|
|
|
// do the round-trip; emit debug log with values we know are
|
|
|
|
// safe, or if there is no error, emit fuller log entry
|
2019-09-02 23:01:02 -05:00
|
|
|
start := time.Now()
|
|
|
|
res, err := h.Transport.RoundTrip(req)
|
2019-12-10 15:36:46 -05:00
|
|
|
duration := time.Since(start)
|
2020-05-18 15:08:11 -05:00
|
|
|
logger := h.logger.With(
|
2020-03-27 15:29:01 -05:00
|
|
|
zap.String("upstream", di.Upstream.String()),
|
2019-11-05 18:27:51 -05:00
|
|
|
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: req}),
|
2020-05-27 11:15:20 -05:00
|
|
|
zap.Duration("duration", duration))
|
2020-05-18 14:50:40 -05:00
|
|
|
if err != nil {
|
2020-05-18 15:08:11 -05:00
|
|
|
logger.Debug("upstream roundtrip", zap.Error(err))
|
2020-05-18 14:50:40 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-05-18 15:08:11 -05:00
|
|
|
logger.Debug("upstream roundtrip",
|
|
|
|
zap.Object("headers", caddyhttp.LoggableHTTPHeader(res.Header)),
|
2020-05-27 11:15:20 -05:00
|
|
|
zap.Int("status", res.StatusCode))
|
2019-11-05 18:27:51 -05:00
|
|
|
|
2019-09-03 20:06:54 -05:00
|
|
|
// update circuit breaker on current conditions
|
2019-10-11 15:25:39 -05:00
|
|
|
if di.Upstream.cb != nil {
|
2019-12-10 15:36:46 -05:00
|
|
|
di.Upstream.cb.RecordMetric(res.StatusCode, duration)
|
2019-09-03 20:06:54 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// perform passive health checks (if enabled)
|
|
|
|
if h.HealthChecks != nil && h.HealthChecks.Passive != nil {
|
|
|
|
// strike if the status code matches one that is "bad"
|
|
|
|
for _, badStatus := range h.HealthChecks.Passive.UnhealthyStatus {
|
|
|
|
if caddyhttp.StatusCodeMatches(res.StatusCode, badStatus) {
|
2019-10-11 15:25:39 -05:00
|
|
|
h.countFailure(di.Upstream)
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// strike if the roundtrip took too long
|
|
|
|
if h.HealthChecks.Passive.UnhealthyLatency > 0 &&
|
2019-12-10 15:36:46 -05:00
|
|
|
duration >= time.Duration(h.HealthChecks.Passive.UnhealthyLatency) {
|
2019-10-11 15:25:39 -05:00
|
|
|
h.countFailure(di.Upstream)
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 13:06:38 -05:00
|
|
|
// see if any response handler is configured for this response from the backend
|
2020-05-27 11:15:20 -05:00
|
|
|
for i, rh := range h.HandleResponse {
|
2020-06-04 13:06:38 -05:00
|
|
|
if rh.Match != nil && !rh.Match.Match(res.StatusCode, res.Header) {
|
2020-05-27 11:15:20 -05:00
|
|
|
continue
|
|
|
|
}
|
2020-06-04 13:06:38 -05:00
|
|
|
|
|
|
|
repl := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
|
|
|
|
|
|
|
// if configured to only change the status code, do that then continue regular proxy response
|
|
|
|
if statusCodeStr := rh.StatusCode.String(); statusCodeStr != "" {
|
|
|
|
statusCode, err := strconv.Atoi(repl.ReplaceAll(statusCodeStr, ""))
|
|
|
|
if err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
if statusCode != 0 {
|
|
|
|
res.StatusCode = statusCode
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, if there are any routes configured, execute those as the
|
|
|
|
// actual response instead of what we got from the proxy backend
|
|
|
|
if len(rh.Routes) == 0 {
|
2020-05-27 11:15:20 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
|
|
|
repl.Set("http.reverse_proxy.status_code", res.StatusCode)
|
|
|
|
repl.Set("http.reverse_proxy.status_text", res.Status)
|
|
|
|
h.logger.Debug("handling response", zap.Int("handler", i))
|
|
|
|
if routeErr := rh.Routes.Compile(next).ServeHTTP(rw, req); routeErr != nil {
|
|
|
|
// wrap error in roundtripSucceeded so caller knows that
|
|
|
|
// the roundtrip was successful and to not retry
|
|
|
|
return roundtripSucceeded{routeErr}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// Deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
|
|
|
|
if res.StatusCode == http.StatusSwitchingProtocols {
|
|
|
|
h.handleUpgradeResponse(rw, req, res)
|
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
removeConnectionHeaders(res.Header)
|
|
|
|
|
|
|
|
for _, h := range hopHeaders {
|
|
|
|
res.Header.Del(h)
|
|
|
|
}
|
|
|
|
|
2020-05-13 10:52:20 -05:00
|
|
|
// apply any response header operations
|
|
|
|
if h.Headers != nil && h.Headers.Response != nil {
|
|
|
|
if h.Headers.Response.Require == nil ||
|
|
|
|
h.Headers.Response.Require.Match(res.StatusCode, res.Header) {
|
|
|
|
repl := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
|
|
|
h.Headers.Response.ApplyTo(res.Header, repl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 11:49:50 -05:00
|
|
|
copyHeader(rw.Header(), res.Header)
|
|
|
|
|
|
|
|
// The "Trailer" header isn't included in the Transport's response,
|
|
|
|
// at least for *http.Transport. Build it up from Trailer.
|
|
|
|
announcedTrailers := len(res.Trailer)
|
|
|
|
if announcedTrailers > 0 {
|
|
|
|
trailerKeys := make([]string, 0, len(res.Trailer))
|
|
|
|
for k := range res.Trailer {
|
|
|
|
trailerKeys = append(trailerKeys, k)
|
|
|
|
}
|
|
|
|
rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(res.StatusCode)
|
|
|
|
|
2020-07-20 13:14:46 -05:00
|
|
|
// some apps need the response headers before starting to stream content with http2,
|
|
|
|
// so it's important to explicitly flush the headers to the client before streaming the data.
|
2020-08-03 21:50:38 -05:00
|
|
|
// (see https://github.com/caddyserver/caddy/issues/3556 for use case and nuances)
|
|
|
|
if h.isBidirectionalStream(req, res) {
|
2020-07-20 13:14:46 -05:00
|
|
|
if wf, ok := rw.(http.Flusher); ok {
|
|
|
|
wf.Flush()
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
err = h.copyResponse(rw, res.Body, h.flushInterval(req, res))
|
2020-05-27 12:42:19 -05:00
|
|
|
res.Body.Close() // close now, instead of defer, to populate res.Trailer
|
2019-05-04 11:49:50 -05:00
|
|
|
if err != nil {
|
2020-05-27 12:42:19 -05:00
|
|
|
// we're streaming the response and we've already written headers, so
|
|
|
|
// there's nothing an error handler can do to recover at this point;
|
|
|
|
// the standard lib's proxy panics at this point, but we'll just log
|
|
|
|
// the error and abort the stream here
|
|
|
|
h.logger.Error("aborting with incomplete response", zap.Error(err))
|
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(res.Trailer) > 0 {
|
|
|
|
// Force chunking if we saw a response trailer.
|
|
|
|
// This prevents net/http from calculating the length for short
|
|
|
|
// bodies and adding a Content-Length.
|
|
|
|
if fl, ok := rw.(http.Flusher); ok {
|
|
|
|
fl.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(res.Trailer) == announcedTrailers {
|
|
|
|
copyHeader(rw.Header(), res.Trailer)
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, vv := range res.Trailer {
|
|
|
|
k = http.TrailerPrefix + k
|
|
|
|
for _, v := range vv {
|
|
|
|
rw.Header().Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
return nil
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// tryAgain takes the time that the handler was initially invoked
|
2019-10-05 17:22:05 -05:00
|
|
|
// as well as any error currently obtained, and the request being
|
|
|
|
// tried, and returns true if another attempt should be made at
|
|
|
|
// proxying the request. If true is returned, it has already blocked
|
|
|
|
// long enough before the next retry (i.e. no more sleeping is
|
|
|
|
// needed). If false is returned, the handler should stop trying to
|
|
|
|
// proxy the request.
|
2020-08-07 19:02:24 -05:00
|
|
|
func (lb LoadBalancing) tryAgain(ctx caddy.Context, start time.Time, proxyErr error, req *http.Request) bool {
|
2019-09-02 23:01:02 -05:00
|
|
|
// if we've tried long enough, break
|
2019-10-05 17:22:05 -05:00
|
|
|
if time.Since(start) >= time.Duration(lb.TryDuration) {
|
2019-09-02 23:01:02 -05:00
|
|
|
return false
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-10-05 17:22:05 -05:00
|
|
|
|
|
|
|
// if the error occurred while dialing (i.e. a connection
|
|
|
|
// could not even be established to the upstream), then it
|
|
|
|
// should be safe to retry, since without a connection, no
|
|
|
|
// HTTP request can be transmitted; but if the error is not
|
|
|
|
// specifically a dialer error, we need to be careful
|
|
|
|
if _, ok := proxyErr.(DialError); proxyErr != nil && !ok {
|
|
|
|
// if the error occurred after a connection was established,
|
|
|
|
// we have to assume the upstream received the request, and
|
|
|
|
// retries need to be carefully decided, because some requests
|
|
|
|
// are not idempotent
|
|
|
|
if lb.RetryMatch == nil && req.Method != "GET" {
|
|
|
|
// by default, don't retry requests if they aren't GET
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !lb.RetryMatch.AnyMatch(req) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// otherwise, wait and try the next available host
|
2020-08-07 19:02:24 -05:00
|
|
|
select {
|
|
|
|
case <-time.After(time.Duration(lb.TryInterval)):
|
|
|
|
return true
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false
|
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:25:39 -05:00
|
|
|
// directRequest modifies only req.URL so that it points to the upstream
|
|
|
|
// in the given DialInfo. It must modify ONLY the request URL.
|
|
|
|
func (h Handler) directRequest(req *http.Request, di DialInfo) {
|
2020-04-21 21:34:00 -05:00
|
|
|
// we need a host, so set the upstream's host address
|
|
|
|
reqHost := di.Address
|
2019-09-11 19:53:44 -05:00
|
|
|
|
2020-04-21 21:34:00 -05:00
|
|
|
// if the port equates to the scheme, strip the port because
|
|
|
|
// it's weird to make a request like http://example.com:80/.
|
|
|
|
if (req.URL.Scheme == "http" && di.Port == "80") ||
|
|
|
|
(req.URL.Scheme == "https" && di.Port == "443") {
|
|
|
|
reqHost = di.Host
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2020-04-21 21:34:00 -05:00
|
|
|
|
|
|
|
req.URL.Host = reqHost
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
func copyHeader(dst, src http.Header) {
|
|
|
|
for k, vv := range src {
|
|
|
|
for _, v := range vv {
|
|
|
|
dst.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 11:49:50 -05:00
|
|
|
func upgradeType(h http.Header) string {
|
|
|
|
if !httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.ToLower(h.Get("Upgrade"))
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// removeConnectionHeaders removes hop-by-hop headers listed in the "Connection" header of h.
|
|
|
|
// See RFC 7230, section 6.1
|
|
|
|
func removeConnectionHeaders(h http.Header) {
|
|
|
|
if c := h.Get("Connection"); c != "" {
|
|
|
|
for _, f := range strings.Split(c, ",") {
|
|
|
|
if f = strings.TrimSpace(f); f != "" {
|
|
|
|
h.Del(f)
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 11:49:50 -05:00
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
2020-11-25 12:54:23 -05:00
|
|
|
// statusError returns an error value that has a status code.
|
|
|
|
func statusError(err error) error {
|
|
|
|
// errors proxying usually mean there is a problem with the upstream(s)
|
|
|
|
statusCode := http.StatusBadGateway
|
|
|
|
|
|
|
|
// if the client canceled the request (usually this means they closed
|
|
|
|
// the connection, so they won't see any response), we can report it
|
|
|
|
// as a client error (4xx) and not a server error (5xx); unfortunately
|
|
|
|
// the Go standard library, at least at time of writing in late 2020,
|
|
|
|
// obnoxiously wraps the exported, standard context.Canceled error with
|
|
|
|
// an unexported garbage value that we have to do a substring check for:
|
|
|
|
// https://github.com/golang/go/blob/6965b01ea248cabb70c3749fd218b36089a21efb/src/net/net.go#L416-L430
|
|
|
|
if errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "operation was canceled") {
|
|
|
|
// regrettably, there is no standard error code for "client closed connection", but
|
|
|
|
// for historical reasons we can use a code that a lot of people are already using;
|
|
|
|
// using 5xx is problematic for users; see #3748
|
|
|
|
statusCode = 499
|
|
|
|
}
|
|
|
|
return caddyhttp.Error(statusCode, err)
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:56:09 -05:00
|
|
|
// LoadBalancing has parameters related to load balancing.
|
2019-09-02 23:01:02 -05:00
|
|
|
type LoadBalancing struct {
|
2019-12-23 14:45:35 -05:00
|
|
|
// A selection policy is how to choose an available backend.
|
|
|
|
// The default policy is random selection.
|
|
|
|
SelectionPolicyRaw json.RawMessage `json:"selection_policy,omitempty" caddy:"namespace=http.reverse_proxy.selection_policies inline_key=policy"`
|
|
|
|
|
|
|
|
// How long to try selecting available backends for each request
|
|
|
|
// if the next available host is down. By default, this retry is
|
|
|
|
// disabled. Clients will wait for up to this long while the load
|
|
|
|
// balancer tries to find an available upstream host.
|
|
|
|
TryDuration caddy.Duration `json:"try_duration,omitempty"`
|
|
|
|
|
|
|
|
// How long to wait between selecting the next host from the pool. Default
|
|
|
|
// is 250ms. Only relevant when a request to an upstream host fails. Be
|
|
|
|
// aware that setting this to 0 with a non-zero try_duration can cause the
|
|
|
|
// CPU to spin if all backends are down and latency is very low.
|
|
|
|
TryInterval caddy.Duration `json:"try_interval,omitempty"`
|
|
|
|
|
|
|
|
// A list of matcher sets that restricts with which requests retries are
|
|
|
|
// allowed. A request must match any of the given matcher sets in order
|
|
|
|
// to be retried if the connection to the upstream succeeded but the
|
|
|
|
// subsequent round-trip failed. If the connection to the upstream failed,
|
|
|
|
// a retry is always allowed. If unspecified, only GET requests will be
|
|
|
|
// allowed to be retried. Note that a retry is done with the next available
|
|
|
|
// host according to the load balancing policy.
|
|
|
|
RetryMatchRaw caddyhttp.RawMatcherSets `json:"retry_match,omitempty" caddy:"namespace=http.matchers"`
|
2019-09-02 23:01:02 -05:00
|
|
|
|
2019-10-05 17:22:05 -05:00
|
|
|
SelectionPolicy Selector `json:"-"`
|
|
|
|
RetryMatch caddyhttp.MatcherSets `json:"-"`
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 17:56:09 -05:00
|
|
|
// Selector selects an available upstream from the pool.
|
2019-09-02 23:01:02 -05:00
|
|
|
type Selector interface {
|
2020-11-20 14:39:26 -05:00
|
|
|
Select(UpstreamPool, *http.Request, http.ResponseWriter) *Upstream
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hop-by-hop headers. These are removed when sent to the backend.
|
|
|
|
// As of RFC 7230, hop-by-hop headers are required to appear in the
|
|
|
|
// Connection header field. These are the headers defined by the
|
|
|
|
// obsoleted RFC 2616 (section 13.5.1) and are used for backward
|
|
|
|
// compatibility.
|
|
|
|
var hopHeaders = []string{
|
2020-03-20 07:54:28 -05:00
|
|
|
"Alt-Svc",
|
2019-09-02 23:01:02 -05:00
|
|
|
"Connection",
|
|
|
|
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
|
|
|
|
"Keep-Alive",
|
|
|
|
"Proxy-Authenticate",
|
|
|
|
"Proxy-Authorization",
|
|
|
|
"Te", // canonicalized version of "TE"
|
|
|
|
"Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
|
|
|
|
"Transfer-Encoding",
|
|
|
|
"Upgrade",
|
|
|
|
}
|
|
|
|
|
2019-10-05 17:22:05 -05:00
|
|
|
// DialError is an error that specifically occurs
|
|
|
|
// in a call to Dial or DialContext.
|
2020-04-07 09:31:52 -05:00
|
|
|
type DialError struct{ error }
|
|
|
|
|
|
|
|
// TLSTransport is implemented by transports
|
|
|
|
// that are capable of using TLS.
|
|
|
|
type TLSTransport interface {
|
|
|
|
// TLSEnabled returns true if the transport
|
|
|
|
// has TLS enabled, false otherwise.
|
|
|
|
TLSEnabled() bool
|
|
|
|
|
|
|
|
// EnableTLS enables TLS within the transport
|
|
|
|
// if it is not already, using the provided
|
|
|
|
// value as a basis for the TLS config.
|
|
|
|
EnableTLS(base *TLSConfig) error
|
2019-10-05 17:22:05 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:15:20 -05:00
|
|
|
// roundtripSucceeded is an error type that is returned if the
|
|
|
|
// roundtrip succeeded, but an error occurred after-the-fact.
|
|
|
|
type roundtripSucceeded struct{ error }
|
|
|
|
|
2019-11-15 19:15:33 -05:00
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|
2019-09-03 13:10:11 -05:00
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.Provisioner = (*Handler)(nil)
|
|
|
|
_ caddy.CleanerUpper = (*Handler)(nil)
|
2019-09-03 17:56:09 -05:00
|
|
|
_ caddyhttp.MiddlewareHandler = (*Handler)(nil)
|
2019-09-02 23:01:02 -05:00
|
|
|
)
|