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"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -71,8 +73,8 @@ func serveError(w http.ResponseWriter, req *http.Request) {
|
|||
w.WriteHeader(httpCode)
|
||||
|
||||
eh := req.Header.Get(EndpointsHeader)
|
||||
|
||||
if eh == "" {
|
||||
glog.Error("no endpoints for default backend")
|
||||
w.Write(de)
|
||||
return
|
||||
}
|
||||
|
@ -81,21 +83,25 @@ func serveError(w http.ResponseWriter, req *http.Request) {
|
|||
|
||||
// TODO: add retries in case of errors
|
||||
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 {
|
||||
glog.Errorf("unexpected error: %v", err)
|
||||
w.Write(de)
|
||||
return
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := client.Do(r)
|
||||
if err != nil {
|
||||
glog.Errorf("unexpected error: %v", err)
|
||||
w.Write(de)
|
||||
return
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
glog.Errorf("unexpected error: %v", err)
|
||||
w.Write(de)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -20,25 +20,55 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"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) {
|
||||
tt := []struct {
|
||||
name string
|
||||
code int
|
||||
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: "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 {
|
||||
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(FormatHeader, tc.format)
|
||||
req.Header.Add(EndpointsHeader, hp)
|
||||
if err != nil {
|
||||
t.Fatalf("could not created request: %v", err)
|
||||
}
|
||||
|
@ -65,6 +95,10 @@ func TestErrorHandler(t *testing.T) {
|
|||
if len(b) == 0 {
|
||||
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 }}
|
||||
|
||||
|
||||
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 }}
|
||||
}
|
||||
{{ if $server.Alias }}
|
||||
|
@ -368,14 +360,6 @@ http {
|
|||
{{ 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 }}
|
||||
}
|
||||
{{ end }}
|
||||
|
@ -407,17 +391,10 @@ http {
|
|||
{{ 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 / {
|
||||
{{ if .CustomErrors }}
|
||||
include /etc/nginx/fastcgi_params;
|
||||
fastcgi_param HTTP_X_Code 404;
|
||||
fastcgi_pass unix:/var/run/go-fastcgi.sock;
|
||||
{{ else }}
|
||||
set $proxy_upstream_name "upstream-default-backend";
|
||||
|
@ -425,14 +402,6 @@ http {
|
|||
{{ 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 }}
|
||||
}
|
||||
}
|
||||
|
@ -510,6 +479,15 @@ stream {
|
|||
location @custom_{{ $errCode }} {
|
||||
internal;
|
||||
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;
|
||||
}
|
||||
{{ end }}
|
||||
|
|
Loading…
Reference in a new issue