Skip to content

Commit

Permalink
chore: Update README
Browse files Browse the repository at this point in the history
This commit updates the README to include the findings in #137, because it is useful to have this information directly in the README rather than having to search for it.
  • Loading branch information
fardin01 committed Aug 2, 2024
1 parent 75d8677 commit 59a591a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,27 @@ go run mage.go buildCaddy
# in terminal 3
curl -i localhost:8080/
```

## Respond with custom message or HTML page

In order to respond with a custom message or HTML page, you can take advantage of [handle_errors](https://caddyserver.com/docs/caddyfile/directives/handle_errors) directive:

```caddy
handle_errors 403 {
header X-Blocked "true"
respond "Your request was blocked. Request ID: {http.request.header.x-request-id}"
}
```
or
```caddy
handle_errors {
@block_codes `{err.status_code} in [403]`
handle @block_codes {
root * /path/to/html/dir
rewrite * /{err.status_code}.html
file_server
}
}
```

It is possible to use the [templates](https://caddyserver.com/docs/caddyfile/directives/templates) directive to render data dynamically. Take a look at [`example/403.html`](./example/403.html) file.
38 changes: 38 additions & 0 deletions example/403.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Access Denied</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: 'Arial', sans-serif;
background-color: #328ba8;
color: #ffffff;
}

.access-denied-container {
text-align: center;
padding: 50px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.7);
}

h1 {
font-size: 5em;
margin: 0;
}
</style>
</head>
<body>
<div class="access-denied-container">
<h1>Access Denied</h1>
<p>Host: {{.Host | stripHTML}}</p>
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions example/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@
SecRule RESPONSE_BODY "@contains responsebodycode" "id:104,phase:4,t:lowercase,deny,status:403"
`
}

handle_errors 403 {
header X-Blocked "true"
root * /etc/caddy/custom-pages
rewrite * /{err.status_code}.html
file_server
templates
}

reverse_proxy {$HTTPBIN_HOST:localhost}:8081
}
1 change: 1 addition & 0 deletions example/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ services:
dockerfile: ./example/Dockerfile
volumes:
- logs:/home/caddy/logs:rw
- ./403.html:/etc/caddy/custom-pages/403.html:ro
ports:
- 8080:8080
environment:
Expand Down

0 comments on commit 59a591a

Please sign in to comment.