Merge pull request #1315 from aledbf/custom-errors
Fix nginx custom error pages
This commit is contained in:
commit
1c727956a5
3 changed files with 59 additions and 41 deletions
|
@ -25,6 +25,8 @@ import (
|
||||||
"net/http/fcgi"
|
"net/http/fcgi"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -71,8 +73,8 @@ func serveError(w http.ResponseWriter, req *http.Request) {
|
||||||
w.WriteHeader(httpCode)
|
w.WriteHeader(httpCode)
|
||||||
|
|
||||||
eh := req.Header.Get(EndpointsHeader)
|
eh := req.Header.Get(EndpointsHeader)
|
||||||
|
|
||||||
if eh == "" {
|
if eh == "" {
|
||||||
|
glog.Error("no endpoints for default backend")
|
||||||
w.Write(de)
|
w.Write(de)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -81,21 +83,25 @@ func serveError(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
// TODO: add retries in case of errors
|
// TODO: add retries in case of errors
|
||||||
ep := eps[rand.Intn(len(eps))]
|
ep := eps[rand.Intn(len(eps))]
|
||||||
req, err = http.NewRequest("GET", fmt.Sprintf("http://%v/", ep), nil)
|
r, err := http.NewRequest("GET", fmt.Sprintf("http://%v/", ep), nil)
|
||||||
|
r.Header = req.Header
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
glog.Errorf("unexpected error: %v", err)
|
||||||
w.Write(de)
|
w.Write(de)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
glog.Errorf("unexpected error: %v", err)
|
||||||
w.Write(de)
|
w.Write(de)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
glog.Errorf("unexpected error: %v", err)
|
||||||
w.Write(de)
|
w.Write(de)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,25 +20,55 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type dummyHandler struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dummyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
code := req.Header.Get(CodeHeader)
|
||||||
|
format := req.Header.Get(FormatHeader)
|
||||||
|
|
||||||
|
if format == "" || format == "*/*" {
|
||||||
|
format = "text/html"
|
||||||
|
}
|
||||||
|
|
||||||
|
httpCode, err := strconv.Atoi(code)
|
||||||
|
if err != nil {
|
||||||
|
httpCode = 404
|
||||||
|
}
|
||||||
|
|
||||||
|
de := []byte(code)
|
||||||
|
w.Header().Set(ContentTypeHeader, format)
|
||||||
|
w.WriteHeader(httpCode)
|
||||||
|
w.Write(de)
|
||||||
|
}
|
||||||
|
|
||||||
func TestErrorHandler(t *testing.T) {
|
func TestErrorHandler(t *testing.T) {
|
||||||
tt := []struct {
|
tt := []struct {
|
||||||
name string
|
name string
|
||||||
code int
|
code int
|
||||||
format string
|
format string
|
||||||
|
endpoints string
|
||||||
}{
|
}{
|
||||||
{name: "404 text/html", code: 404, format: "text/html"},
|
{name: "404 text/html", code: 404, format: "text/html", endpoints: "127.0.0.1:80"},
|
||||||
{name: "503 text/html", code: 503, format: "text/html"},
|
{name: "503 text/html", code: 503, format: "text/html"},
|
||||||
{name: "404 application/json", code: 404, format: "application/json"},
|
{name: "404 application/json", code: 404, format: "application/json", endpoints: "127.0.0.1:80"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server := httptest.NewServer(&dummyHandler{})
|
||||||
|
defer server.Close()
|
||||||
|
hp := strings.Replace(server.URL, "http://", "", -1)
|
||||||
|
|
||||||
for _, tc := range tt {
|
for _, tc := range tt {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
req, err := http.NewRequest("GET", "localhost:8080/", nil)
|
req, err := http.NewRequest("GET", server.URL, nil)
|
||||||
req.Header.Add(CodeHeader, fmt.Sprintf("%v", tc.code))
|
req.Header.Add(CodeHeader, fmt.Sprintf("%v", tc.code))
|
||||||
req.Header.Add(FormatHeader, tc.format)
|
req.Header.Add(FormatHeader, tc.format)
|
||||||
|
req.Header.Add(EndpointsHeader, hp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not created request: %v", err)
|
t.Fatalf("could not created request: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -65,6 +95,10 @@ func TestErrorHandler(t *testing.T) {
|
||||||
if len(b) == 0 {
|
if len(b) == 0 {
|
||||||
t.Fatalf("unexpected empty body")
|
t.Fatalf("unexpected empty body")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if string(b) != strconv.Itoa(tc.code) {
|
||||||
|
t.Fatalf("body: %v", string(b))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,14 +352,6 @@ http {
|
||||||
{{ template "SERVER" serverConfig $all $server }}
|
{{ template "SERVER" serverConfig $all $server }}
|
||||||
|
|
||||||
|
|
||||||
fastcgi_param HTTP_X_Code 503;
|
|
||||||
fastcgi_param HTTP_X_Format $http_accept;
|
|
||||||
fastcgi_param HTTP_X_Original_URI $request_uri;
|
|
||||||
fastcgi_param HTTP_X_Namespace $namespace;
|
|
||||||
fastcgi_param HTTP_X_Ingress_Name $ingress_name;
|
|
||||||
fastcgi_param HTTP_X_Service_Name $service_name;
|
|
||||||
fastcgi_param HTTP_X_Endpoints {{ $all.DefaultBackendEndpoints }};
|
|
||||||
|
|
||||||
{{ template "CUSTOM_ERRORS" $all }}
|
{{ template "CUSTOM_ERRORS" $all }}
|
||||||
}
|
}
|
||||||
{{ if $server.Alias }}
|
{{ if $server.Alias }}
|
||||||
|
@ -368,14 +360,6 @@ http {
|
||||||
{{ template "SERVER" serverConfig $all $server }}
|
{{ template "SERVER" serverConfig $all $server }}
|
||||||
|
|
||||||
|
|
||||||
fastcgi_param HTTP_X_Code 503;
|
|
||||||
fastcgi_param HTTP_X_Format $http_accept;
|
|
||||||
fastcgi_param HTTP_X_Original_URI $request_uri;
|
|
||||||
fastcgi_param HTTP_X_Namespace $namespace;
|
|
||||||
fastcgi_param HTTP_X_Ingress_Name $ingress_name;
|
|
||||||
fastcgi_param HTTP_X_Service_Name $service_name;
|
|
||||||
fastcgi_param HTTP_X_Endpoints {{ $all.DefaultBackendEndpoints }};
|
|
||||||
|
|
||||||
{{ template "CUSTOM_ERRORS" $all }}
|
{{ template "CUSTOM_ERRORS" $all }}
|
||||||
}
|
}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
@ -407,17 +391,10 @@ http {
|
||||||
{{ end }}
|
{{ end }}
|
||||||
}
|
}
|
||||||
|
|
||||||
fastcgi_param HTTP_X_Code 404;
|
|
||||||
fastcgi_param HTTP_X_Format $http_accept;
|
|
||||||
fastcgi_param HTTP_X_Original_URI $request_uri;
|
|
||||||
fastcgi_param HTTP_X_Namespace $namespace;
|
|
||||||
fastcgi_param HTTP_X_Ingress_Name $ingress_name;
|
|
||||||
fastcgi_param HTTP_X_Service_Name $service_name;
|
|
||||||
fastcgi_param HTTP_X_Endpoints {{ $all.DefaultBackendEndpoints }};
|
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
{{ if .CustomErrors }}
|
{{ if .CustomErrors }}
|
||||||
include /etc/nginx/fastcgi_params;
|
include /etc/nginx/fastcgi_params;
|
||||||
|
fastcgi_param HTTP_X_Code 404;
|
||||||
fastcgi_pass unix:/var/run/go-fastcgi.sock;
|
fastcgi_pass unix:/var/run/go-fastcgi.sock;
|
||||||
{{ else }}
|
{{ else }}
|
||||||
set $proxy_upstream_name "upstream-default-backend";
|
set $proxy_upstream_name "upstream-default-backend";
|
||||||
|
@ -425,14 +402,6 @@ http {
|
||||||
{{ end }}
|
{{ end }}
|
||||||
}
|
}
|
||||||
|
|
||||||
fastcgi_param HTTP_X_Code 404;
|
|
||||||
fastcgi_param HTTP_X_Format $http_accept;
|
|
||||||
fastcgi_param HTTP_X_Original_URI $request_uri;
|
|
||||||
fastcgi_param HTTP_X_Namespace $namespace;
|
|
||||||
fastcgi_param HTTP_X_Ingress_Name $ingress_name;
|
|
||||||
fastcgi_param HTTP_X_Service_Name $service_name;
|
|
||||||
fastcgi_param HTTP_X_Endpoints {{ $all.DefaultBackendEndpoints }};
|
|
||||||
|
|
||||||
{{ template "CUSTOM_ERRORS" $all }}
|
{{ template "CUSTOM_ERRORS" $all }}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -510,6 +479,15 @@ stream {
|
||||||
location @custom_{{ $errCode }} {
|
location @custom_{{ $errCode }} {
|
||||||
internal;
|
internal;
|
||||||
include /etc/nginx/fastcgi_params;
|
include /etc/nginx/fastcgi_params;
|
||||||
|
|
||||||
|
fastcgi_param HTTP_X_Code {{ $errCode }};
|
||||||
|
fastcgi_param HTTP_X_Format $http_accept;
|
||||||
|
fastcgi_param HTTP_X_Original_URI $request_uri;
|
||||||
|
fastcgi_param HTTP_X_Namespace $namespace;
|
||||||
|
fastcgi_param HTTP_X_Ingress_Name $ingress_name;
|
||||||
|
fastcgi_param HTTP_X_Service_Name $service_name;
|
||||||
|
fastcgi_param HTTP_X_Endpoints "{{ $defaultBackendEndpoints }}";
|
||||||
|
|
||||||
fastcgi_pass unix:/var/run/go-fastcgi.sock;
|
fastcgi_pass unix:/var/run/go-fastcgi.sock;
|
||||||
}
|
}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
Loading…
Reference in a new issue