Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nginx custom error pages #1315

Merged
merged 1 commit into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions controllers/nginx/pkg/fastcgi/fastcgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"net/http/fcgi"
"strconv"
"strings"

"github.com/golang/glog"
)

const (
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
46 changes: 40 additions & 6 deletions controllers/nginx/pkg/fastcgi/fastcgi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
}
Expand All @@ -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))
}
})
}
}
42 changes: 10 additions & 32 deletions controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand Down Expand Up @@ -407,32 +391,17 @@ 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";
proxy_pass http://upstream-default-backend;
{{ 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 }}
}
}
Expand Down Expand Up @@ -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 }}
Expand Down