Skip to content

Commit

Permalink
Log when page not found (TykTechnologies#2800)
Browse files Browse the repository at this point in the history
Example log:

``` 
time="Jan 13 16:07:06" level=error msg="404 page not found" request="GET /non-existing-api/get HTTP/1.1" 
```

Fixes TykTechnologies#2468
  • Loading branch information
furkansenharputlu authored and buger committed Jan 16, 2020
1 parent 86dc451 commit 2cdec53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions gateway/api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ func loadApps(specs []*APISpec) {

muxer := &proxyMux{}
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(muxer.handle404)
loadAPIEndpoints(router)
muxer.setRouter(port, "", router)

Expand Down
12 changes: 10 additions & 2 deletions gateway/proxy_muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/TykTechnologies/again"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/tcp"
proxyproto "github.com/pires/go-proxyproto"
cache "github.com/pmylund/go-cache"
"github.com/pires/go-proxyproto"
"github.com/pmylund/go-cache"

"golang.org/x/net/http2"

Expand Down Expand Up @@ -148,6 +148,14 @@ func (m *proxyMux) setRouter(port int, protocol string, router *mux.Router) {
}
}

func (m *proxyMux) handle404(w http.ResponseWriter, r *http.Request) {
requestMeta := fmt.Sprintf("%s %s %s", r.Method, r.URL.Path, r.Proto)
log.WithField("request", requestMeta).Error(http.StatusText(http.StatusNotFound))

w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprint(w, http.StatusText(http.StatusNotFound))
}

func (m *proxyMux) addTCPService(spec *APISpec, modifier *tcp.Modifier) {
hostname := spec.GlobalConfig.HostName
if spec.GlobalConfig.EnableCustomDomains {
Expand Down
17 changes: 17 additions & 0 deletions gateway/proxy_muxer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync/atomic"
"testing"

"github.com/TykTechnologies/tyk/test"

"github.com/TykTechnologies/tyk/config"
)

Expand Down Expand Up @@ -289,3 +291,18 @@ func TestHTTP_custom_ports(t *testing.T) {
t.Errorf("expected %s to %s", echo, bs)
}
}

func TestHandle404(t *testing.T) {
g := StartTest()
defer g.Close()

BuildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/existing"
spec.UseKeylessAccess = true
})

_, _ = g.Run(t, []test.TestCase{
{Path: "/existing", Code: http.StatusOK},
{Path: "/nonexisting", Code: http.StatusNotFound, BodyMatch: http.StatusText(http.StatusNotFound)},
}...)
}

0 comments on commit 2cdec53

Please sign in to comment.