Skip to content

Commit

Permalink
Merge pull request #1705 from aledbf/proxy-redirect
Browse files Browse the repository at this point in the history
Add annotation for setting proxy_redirect
  • Loading branch information
aledbf committed Nov 14, 2017
2 parents f19a3be + c5b0c8a commit 90eff7d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
9 changes: 9 additions & 0 deletions docs/user-guide/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ The following annotations are supported:
|[ingress.kubernetes.io/proxy-read-timeout](#custom-timeouts)|number|
|[ingress.kubernetes.io/proxy-next-upstream](#custom-timeouts)|string|
|[ingress.kubernetes.io/proxy-request-buffering](#custom-timeouts)|string|
|[ingress.kubernetes.io/proxy-redirect-from](#proxy-redirect)|string|
|[ingress.kubernetes.io/proxy-redirect-to](#proxy-redirect)|string|
|[ingress.kubernetes.io/rewrite-target](#rewrite)|URI|
|[ingress.kubernetes.io/secure-backends](#secure-backends)|true or false|
|[ingress.kubernetes.io/server-alias](#server-alias)|string|
Expand Down Expand Up @@ -352,6 +354,13 @@ In some scenarios is required to have different values. To allow this we provide
- `ingress.kubernetes.io/proxy-next-upstream`
- `ingress.kubernetes.io/proxy-request-buffering`

### Proxy redirect

With the annotations `ingress.kubernetes.io/proxy-redirect-from` and `ingress.kubernetes.io/proxy-redirect-to` it is possible to set the text that should be changed in the `Location` and `Refresh` header fields of a proxied server response (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect)
Setting "off" or "default" in the annotation `ingress.kubernetes.io/proxy-redirect-to` disables `ingress.kubernetes.io/proxy-redirect-to`
Both annotations will be used in any other case
By default the value is "off".

### Custom max body size

For NGINX, 413 error will be returned to the client when the size in a request exceeds the maximum allowed size of the client request body. This size can be configured by the parameter [`client_max_body_size`](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size).
Expand Down
41 changes: 29 additions & 12 deletions internal/ingress/annotations/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import (

// Config returns the proxy timeout to use in the upstream server/s
type Config struct {
BodySize string `json:"bodySize"`
ConnectTimeout int `json:"connectTimeout"`
SendTimeout int `json:"sendTimeout"`
ReadTimeout int `json:"readTimeout"`
BufferSize string `json:"bufferSize"`
CookieDomain string `json:"cookieDomain"`
CookiePath string `json:"cookiePath"`
NextUpstream string `json:"nextUpstream"`
PassParams string `json:"passParams"`
RequestBuffering string `json:"requestBuffering"`
BodySize string `json:"bodySize"`
ConnectTimeout int `json:"connectTimeout"`
SendTimeout int `json:"sendTimeout"`
ReadTimeout int `json:"readTimeout"`
BufferSize string `json:"bufferSize"`
CookieDomain string `json:"cookieDomain"`
CookiePath string `json:"cookiePath"`
NextUpstream string `json:"nextUpstream"`
PassParams string `json:"passParams"`
ProxyRedirectFrom string `json:"proxyRedirectFrom"`
ProxyRedirectTo string `json:"proxyRedirectTo"`
RequestBuffering string `json:"requestBuffering"`
}

// Equal tests for equality between two Configuration types
Expand Down Expand Up @@ -72,10 +74,15 @@ func (l1 *Config) Equal(l2 *Config) bool {
if l1.PassParams != l2.PassParams {
return false
}

if l1.RequestBuffering != l2.RequestBuffering {
return false
}
if l1.ProxyRedirectFrom != l2.ProxyRedirectFrom {
return false
}
if l1.ProxyRedirectTo != l2.ProxyRedirectTo {
return false
}

return true
}
Expand Down Expand Up @@ -143,5 +150,15 @@ func (a proxy) Parse(ing *extensions.Ingress) (interface{}, error) {
rb = defBackend.ProxyRequestBuffering
}

return &Config{bs, ct, st, rt, bufs, cd, cp, nu, pp, rb}, nil
prf, err := parser.GetStringAnnotation("proxy-redirect-from", ing, a.r)
if err != nil || rb == "" {
prf = defBackend.ProxyRedirectFrom
}

prt, err := parser.GetStringAnnotation("proxy-redirect-to", ing, a.r)
if err != nil || rb == "" {
prt = defBackend.ProxyRedirectTo
}

return &Config{bs, ct, st, rt, bufs, cd, cp, nu, pp, prf, prt, rb}, nil
}
1 change: 1 addition & 0 deletions internal/ingress/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ func NewDefault() Configuration {
ProxyCookiePath: "off",
ProxyNextUpstream: "error timeout invalid_header http_502 http_503 http_504",
ProxyRequestBuffering: "on",
ProxyRedirectFrom: "off",
SSLRedirect: true,
CustomHTTPErrors: []int{},
WhitelistSourceRange: []string{},
Expand Down
19 changes: 10 additions & 9 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,15 +874,16 @@ func (n *NGINXController) createServers(data []*extensions.Ingress,

bdef := n.GetDefaultBackend()
ngxProxy := proxy.Config{
BodySize: bdef.ProxyBodySize,
ConnectTimeout: bdef.ProxyConnectTimeout,
SendTimeout: bdef.ProxySendTimeout,
ReadTimeout: bdef.ProxyReadTimeout,
BufferSize: bdef.ProxyBufferSize,
CookieDomain: bdef.ProxyCookieDomain,
CookiePath: bdef.ProxyCookiePath,
NextUpstream: bdef.ProxyNextUpstream,
RequestBuffering: bdef.ProxyRequestBuffering,
BodySize: bdef.ProxyBodySize,
ConnectTimeout: bdef.ProxyConnectTimeout,
SendTimeout: bdef.ProxySendTimeout,
ReadTimeout: bdef.ProxyReadTimeout,
BufferSize: bdef.ProxyBufferSize,
CookieDomain: bdef.ProxyCookieDomain,
CookiePath: bdef.ProxyCookiePath,
NextUpstream: bdef.ProxyNextUpstream,
RequestBuffering: bdef.ProxyRequestBuffering,
ProxyRedirectFrom: bdef.ProxyRedirectFrom,
}

// generated on Start() with createDefaultSSLCertificate()
Expand Down
10 changes: 10 additions & 0 deletions internal/ingress/defaults/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ type Backend struct {
// Parameters for proxy-pass directive (eg. Apache web server).
ProxyPassParams string `json:"proxy-pass-params"`

// Sets the original text that should be changed in the "Location" and "Refresh" header fields of a proxied server response.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
// Default: off
ProxyRedirectFrom string `json:"proxy-redirect-from"`

// Sets the replacement text that should be changed in the "Location" and "Refresh" header fields of a proxied server response.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
// Default: ""
ProxyRedirectTo string `json:"proxy-redirect-to"`

// Enables or disables buffering of a client request body.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_request_buffering
ProxyRequestBuffering string `json:"proxy-request-buffering"`
Expand Down
6 changes: 5 additions & 1 deletion rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,11 @@ stream {
proxy_send_timeout {{ $location.Proxy.SendTimeout }}s;
proxy_read_timeout {{ $location.Proxy.ReadTimeout }}s;

proxy_redirect off;
{{ if (or (eq $location.Proxy.ProxyRedirectFrom "default") (eq $location.Proxy.ProxyRedirectFrom "off")) }}
proxy_redirect {{ $location.Proxy.ProxyRedirectFrom }};
{{ else }}
proxy_redirect {{ $location.Proxy.ProxyRedirectFrom }} {{ $location.Proxy.ProxyRedirectTo }};
{{ end }}
proxy_buffering off;
proxy_buffer_size "{{ $location.Proxy.BufferSize }}";
proxy_buffers 4 "{{ $location.Proxy.BufferSize }}";
Expand Down

0 comments on commit 90eff7d

Please sign in to comment.