1. Caddy version (caddy version
):
v2.0.0
a. System environment:
Running Debian 10 on a Vultr VPS.
Current Caddyfile
toomanycooks.social {
root * /home/mastodon/live/public
encode gzip
header / {
Strict-Transport-Security "max-age=31536000;"
}
header /emoji Cache-Control "public, max-age=31536000, immutable"
header /packs Cache-Control "public, max-age=31536000, immutable"
header /system/accounts/avatars Cache-Control "public, max-age=31536000, immutable"
header /system/media_attachments/files Cache-Control "public, max-age=31536000, immutable"
handle_errors {
root * 500.html
}
reverse_proxy / localhost:3000
reverse_proxy /api/v1/streaming localhost:4000
}
3. The problem I’m having:
I’m trying to use Caddy for running a Mastodon instance. The resources I’ve found include Mastodon’s nginx config file as well as a Caddy config - but that was done for Caddy v1 and I’m having some trouble translating it over. Specifcally, it seems to use the /
path for both static files and proxying to the app at localhost:3000
and I’m unable to figure out how to make this work in Caddy v2. The v1 Caddyfile seems to use some kind of rewrite hacking to get this to work, which I’m unable to figure out the exact replacement for in this case.
I’m also not 100% sure that the handle_errors
block that I’ve written is correct, so any advice on that would be good too.
Mastodon nginx Config File
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream backend {
server 127.0.0.1:3000 fail_timeout=0;
}
upstream streaming {
server 127.0.0.1:4000 fail_timeout=0;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=1g;
server {
listen 80;
listen [::]:80;
server_name example.com;
root /home/mastodon/live/public;
location /.well-known/acme-challenge/ { allow all; }
location / { return 301 https://$host$request_uri; }
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
# Uncomment these lines once you acquire a certificate:
# ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
keepalive_timeout 70;
sendfile on;
client_max_body_size 80m;
root /home/mastodon/live/public;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
add_header Strict-Transport-Security "max-age=31536000";
location / {
try_files $uri @proxy;
}
location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header Strict-Transport-Security "max-age=31536000";
try_files $uri @proxy;
}
location /sw.js {
add_header Cache-Control "public, max-age=0";
add_header Strict-Transport-Security "max-age=31536000";
try_files $uri @proxy;
}
location @proxy {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Proxy "";
proxy_pass_header Server;
proxy_pass http://backend;
proxy_buffering on;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache CACHE;
proxy_cache_valid 200 7d;
proxy_cache_valid 410 24h;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
add_header X-Cached $upstream_cache_status;
add_header Strict-Transport-Security "max-age=31536000";
tcp_nodelay on;
}
location /api/v1/streaming {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Proxy "";
proxy_pass http://streaming;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
error_page 500 501 502 503 504 /500.html;
}
v1 Caddyfile for Mastodon I had found:
https://social.amnesiak.org {
log /var/lib/caddy/data/social.amnesiak.org-access.log
errors /var/lib/caddy/data/social.amnesiak.org-access.err
root /data/mastodon/public
gzip
header / {
Strict-Transport-Security "max-age=31536000;"
}
header /emoji Cache-Control "public, max-age=31536000, immutable"
header /packs Cache-Control "public, max-age=31536000, immutable"
header /system/accounts/avatars Cache-Control "public, max-age=31536000, immutable"
header /system/media_attachments/files Cache-Control "public, max-age=31536000, immutable"
errors {
* 500.html
}
rewrite {
if {path} is /
to /proxy{path}
}
rewrite {
if {path} not_has /api/v1/streaming
to {path} /proxy{path}
}
proxy /proxy localhost:3000 {
without /proxy
transparent
websocket
}
proxy /api/v1/streaming localhost:4000 {
transparent
websocket
}
tls {
protocols tls1.2
}
}
5. What I already tried:
Like I showed above, I already have most of the Caddyfile translated over to V2.
6. Links to relevant resources:
Links to where I got the files. Mastodon’s nginx.conf and the v1 Caddyfile
Any help would be greatly appreciated, let me know if there’s any more information needed.