Skip to content

Commit 5938da2

Browse files
author
Alcides Ramos
committed
feat(Caddyfile): improve Caddy performance
1 parent 2495e25 commit 5938da2

5 files changed

Lines changed: 95 additions & 9 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# STAGE: BASE-IMAGE
55
#----------------------------------------------------------
66

7-
FROM php:8.3.12-fpm-alpine AS base-image
7+
FROM php:8.3.3-fpm-alpine AS base-image
88

99
#----------------------------------------------------------
1010
# STAGE: COMMON

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,4 @@ open-website: ## Application: open the application website
249249
$(call taskDone)
250250

251251
.PHONY: init
252-
init: build install-caddy-certificate ## Application: initializes the application
253-
$(call showInfo,"When ready just execute [ make open-website ] to visit the website with your preferred browser")
254-
$(call taskDone)
252+
init: build install-caddy-certificate open-website ## Application: initializes the application

build/Caddyfile

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,94 @@
1+
{
2+
admin off
3+
}
4+
15
(common) {
2-
encode zstd gzip
6+
encode zstd gzip
7+
38
file_server
9+
10+
respond /healthcheck "UP" 200
411
}
512

613
(ssl) {
7-
tls internal
14+
tls {$TLS} {
15+
on_demand
16+
}
817
}
918

10-
{$HOSTNAME} {
19+
(custom_domain) {
1120
import common
1221
import ssl
1322

14-
respond /healthcheck 200
23+
###
24+
# Domain related
25+
###
1526

16-
root * /var/www/html/public
27+
@strip_www {
28+
header_regexp www Host ^www\.(.*)$
29+
}
30+
redir @strip_www https://{http.regexp.www.1}{uri}
31+
32+
###
33+
# Cache
34+
###
35+
36+
header {
37+
Cache-Control "public, max-age=31536000"
38+
}
39+
40+
@static {
41+
file
42+
path *.avif *.ico *.css *.js *.gz *.eot *.ttf *.otf *.gif *.webp *.avif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *.pdf
43+
}
44+
header @static Cache-Control "max-age=31536000,public,inmutable"
45+
46+
@html {
47+
path *.html *.htm
48+
}
49+
header @html {
50+
Cache-Control "public, max-age=43200"
51+
}
52+
53+
###
54+
# Security
55+
###
56+
57+
header {
58+
Strict-Transport-Security "max-age=31536000;includeSubDomains;preload"
59+
X-Frame-Options "SAMEORIGIN"
60+
X-Xss-Protection "1;mode=block"
61+
Referrer-Policy "no-referrer-when-downgrade"
62+
X-Content-Type-Options "nosniff"
63+
Permissions-Policy "autoplay=(self),camera=(),geolocation=(),microphone=(),payment=(),usb=()"
64+
65+
# Review
66+
#?Content-Security-Policy "default-src 'self';script-src 'self';style-src 'self'"
67+
}
68+
69+
@requestMethodsList {
70+
not method GET HEAD POST OPTIONS
71+
}
72+
respond @requestMethodsList "Not Allowed" 405 {
73+
close
74+
}
75+
76+
###
77+
# 404
78+
###
79+
80+
@static_404 {
81+
path_regexp \.(jpg|jpeg|png|webp|gif|avif|ico|svg|css|js|gz|eot|ttf|otf|woff|woff2|pdf)$
82+
not file
83+
}
84+
85+
respond @static_404 "Not Found" 404 {
86+
close
87+
}
88+
89+
###
90+
# PHP-FPM
91+
###
1792

1893
php_fastcgi {$PHP_FPM_GATEWAYS} {
1994
index index.php
@@ -23,3 +98,13 @@
2398
lb_policy round_robin
2499
}
25100
}
101+
102+
:80 {
103+
import common
104+
import ssl
105+
}
106+
107+
{$HOSTNAME}, www.{$HOSTNAME} {
108+
import custom_domain
109+
root * /var/www/html/public
110+
}

docker-compose.override.prod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
services:
22
caddy:
33
environment:
4+
- TLS=internal
5+
- HOSTNAME=localhost
46
- PHP_FPM_GATEWAYS=app1:9000 app2:9000
57
depends_on:
68
app1:

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
image: caddy:2.8.4-alpine
99
restart: unless-stopped
1010
environment:
11+
- TLS=internal
1112
- HOSTNAME=localhost
1213
- PHP_FPM_GATEWAYS=app1:9000
1314
volumes:

0 commit comments

Comments
 (0)