Skip to content

Commit 3221ce0

Browse files
authored
Bugfix: Caddy and PiHole not working correctly after CloudFlare migration (#2)
1 parent 7eb3dcb commit 3221ce0

4 files changed

Lines changed: 131 additions & 47 deletions

File tree

.github/workflows/deploy-prod.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ jobs:
174174
- name: Pull latest Docker images
175175
run: |
176176
cd "${{ secrets.DOCKER_DIR }}"
177-
sudo docker compose pull
177+
sudo docker compose pull --quiet
178178
179179
- name: Apply Docker Compose changes
180180
run: |

docker/caddy/Caddyfile

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,117 @@
22
# https://caddyserver.com/docs/caddyfile
33

44
{
5-
# Global options
5+
# Global options
66

7-
# Monitoring
8-
admin :2019
9-
metrics {
10-
per_host
11-
}
7+
# Monitoring
8+
admin :2019
9+
metrics {
10+
per_host
11+
}
12+
}
13+
14+
(logging) {
15+
log {
16+
output file /data/logs/{args[0]}.log {
17+
roll_size 10mb
18+
roll_keep 5
19+
roll_local_time
20+
}
21+
format console
22+
level INFO
23+
}
1224
}
1325

1426
# Global Imports
1527
(all) {
16-
handle_errors {
17-
respond "{err.status_code} {err.status_text}"
28+
# Error handling for Caddy-generated errors
29+
handle_errors {
30+
root * /opt/caddy-error-pages
31+
rewrite * /error.html
32+
templates
33+
file_server
1834
}
19-
# handle_errors {
20-
# rewrite * /404.html
21-
# file_server {
22-
# status 404
23-
# }
24-
# }
2535
}
2636

2737
# Default options for internal facing sites
2838
(home) {
29-
import all
30-
tls internal
39+
import all
40+
tls internal
3141
}
3242

3343
grafana.home {
34-
import home
35-
reverse_proxy http://{$IP_GRAFANA}:3000
44+
import home
45+
import logging grafana.home
46+
reverse_proxy http://{$IP_GRAFANA}:3000
3647
}
3748

3849
uptime.home {
39-
import home
40-
reverse_proxy http://{$IP_UPTIME_KUMA}:3001
50+
import home
51+
import logging uptime.home
52+
reverse_proxy http://{$IP_UPTIME_KUMA}:3001
4153
}
4254

4355
# Default options for external facing sites
4456
(external) {
45-
import all
57+
import all
4658

47-
tls {
59+
tls {
4860
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
4961
}
5062

51-
header {
52-
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
53-
X-Xss-Protection "1; mode=block"
54-
X-Content-Type-Options "nosniff"
55-
X-Frame-Options "DENY"
56-
Content-Security-Policy "upgrade-insecure-requests"
57-
Referrer-Policy "strict-origin-when-cross-origin"
58-
Cache-Control "public, max-age=15, must-revalidate"
59-
Permissions-Policy "accelerometer=(), ambient-light-sensor=(), autoplay=(self), camera=(), encrypted-media=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(*), speaker-selection=(), usb=(), xr-spatial-tracking=()"
60-
[defer]
61-
}
62-
file_server
63+
header {
64+
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
65+
X-Xss-Protection "1; mode=block"
66+
X-Content-Type-Options "nosniff"
67+
X-Frame-Options "DENY"
68+
Content-Security-Policy "upgrade-insecure-requests"
69+
Referrer-Policy "strict-origin-when-cross-origin"
70+
Cache-Control "public, max-age=15, must-revalidate"
71+
Permissions-Policy "accelerometer=(), ambient-light-sensor=(), autoplay=(self), camera=(), encrypted-media=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(*), speaker-selection=(), usb=(), xr-spatial-tracking=()"
72+
[defer]
73+
}
74+
file_server
6375
}
6476

6577
(geeksbsmrt) {
66-
import external
78+
import external
6779
}
6880

6981
geeksbsmrt.com {
70-
import geeksbsmrt
71-
82+
import geeksbsmrt
83+
import logging geeksbsmrt.com
7284
root * /srv/geeksbsmrt.com
7385
}
7486

7587
analytics.geeksbsmrt.com {
7688
import geeksbsmrt
77-
89+
import logging analytics.geeksbsmrt.com
7890
reverse_proxy http://{$IP_UMAMI_APP}:3000
7991
}
8092

8193
*.geeksbsmrt.com {
82-
import geeksbsmrt
94+
import geeksbsmrt
95+
import logging wildcard.geeksbsmrt.com
8396
}
8497

8598
(smrtgeekdevs) {
86-
import external
99+
import external
87100
}
88101

89102
smrtgeekdevs.com {
90-
#root * /var/www/smrtgeekdevs
103+
import smrtgeekdevs # Inherit `external` config here
104+
import logging smrtgeekdevs.com
105+
#root * /var/www/smrtgeekdevs
91106
}
92107

93108
pihole.smrtgeekdevs.com {
94-
import smrtgeekdevs
95-
96-
redir / /admin{uri}
97-
reverse_proxy pihole
109+
import smrtgeekdevs
110+
import logging pihole.smrtgeekdevs.com
111+
redir / /admin{uri} 308
112+
reverse_proxy http://pihole
98113
}
99114

100115
*.smrtgeekdevs.com {
101-
import smrtgeekdevs
116+
import smrtgeekdevs
117+
import logging wildcard.smrtgeekdevs.com
102118
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Error {{placeholder "http.error.status_code"}} - {{placeholder "http.error.status_text"}}</title>
7+
<style>
8+
body {
9+
font-family: sans-serif;
10+
text-align: center;
11+
padding: 40px;
12+
background-color: #f8f9fa;
13+
color: #343a40;
14+
}
15+
16+
.container {
17+
max-width: 600px;
18+
margin: auto;
19+
background: white;
20+
padding: 20px;
21+
border-radius: 8px;
22+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
23+
}
24+
25+
h1 {
26+
color: #dc3545;
27+
}
28+
29+
p.error-detail {
30+
font-size: 1.1em;
31+
color: #6c757d;
32+
}
33+
34+
code {
35+
background-color: #e9ecef;
36+
padding: 2px 6px;
37+
border-radius: 4px;
38+
font-family: monospace;
39+
}
40+
</style>
41+
</head>
42+
43+
<body>
44+
<div class="container">
45+
<h1>Error {{placeholder "http.error.status_code"}} - {{placeholder "http.error.status_text"}}</h1>
46+
<p class="error-detail">
47+
{{if ge (atoi (placeholder "http.error.status_code")) 500}}
48+
We’re experiencing some internal trouble. Please try again later.
49+
{{else if ge (atoi (placeholder "http.error.status_code")) 400}}
50+
The page you're looking for doesn't exist or your request was invalid.
51+
{{else}}
52+
An unexpected issue occurred.
53+
{{end}}
54+
</p>
55+
{{ with placeholder "http.error.message" }}
56+
<p><strong>Details:</strong> <code>{{.}}</code></p>
57+
{{ end }}
58+
<p><a href="/">Return to Homepage</a></p>
59+
</div>
60+
</body>
61+
62+
</html>

docker/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ services:
6464
hostname: caddy
6565
image: ghcr.io/caddybuilds/caddy-cloudflare:latest
6666
restart: unless-stopped
67+
logging:
68+
driver: "json-file"
69+
options:
70+
max-size: "10m"
71+
max-file: "5"
6772
networks:
6873
macvlan:
6974
ipv4_address: ${IP_CADDY}
7075
volumes:
7176
- ./caddy/Caddyfile:/etc/caddy/Caddyfile
77+
- ./caddy/error-pages:/opt/caddy-error-pages:ro
7278
- /srv:/srv
7379
- caddy_data:/data
7480
- caddy_config:/config

0 commit comments

Comments
 (0)