Skip to content

Instantly share code, notes, and snippets.

@yukimochi
Last active June 30, 2024 03:03
Show Gist options
  • Save yukimochi/bb7c90cbe628f216f821e835df1aeac1 to your computer and use it in GitHub Desktop.
Save yukimochi/bb7c90cbe628f216f821e835df1aeac1 to your computer and use it in GitHub Desktop.
For Mastodon Caddy Setting file. (Based on official Nginx configuration example) [Last Update: Mar 07, 2021]
https://yourdomain.example.com/ {
log / /var/log/caddy/mastodon.log "{combined}" {
rotate_age 90
}
root /home/mastodon/live/public
gzip
header / {
Strict-Transport-Security "max-age=31536000"
}
header /sw.js Cache-Control "public, max-age=0"
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 [email protected]
}
{
email [email protected]
}
https://yourdomain.example.com {
@local {
file
not path /
}
@local_media {
path_regexp /system/(.*)
}
@streaming {
path /api/v1/streaming/*
}
@cache_control {
path_regexp ^/(emoji|packs|/system/accounts/avatars|/system/media_attachments/files)
}
root * /home/mastodon/live/public
log {
output file /var/log/caddy/mastodon.log
}
encode zstd gzip
handle_errors {
rewrite 500.html
file_server
}
header {
Strict-Transport-Security "max-age=31536000"
}
header /sw.js Cache-Control "public, max-age=0"
header @cache_control Cache-Control "public, max-age=31536000, immutable"
handle @local {
file_server
}
## If you've been migrated media from local to object storage, this navigate old URL to new one.
# redir @local_media https://yourobjectstorage.example.com/{http.regexp.1} permanent
reverse_proxy @streaming {
to http://localhost:4000
transport http {
keepalive 5s
keepalive_idle_conns 10
}
}
reverse_proxy {
to http://localhost:3000
header_up X-Forwarded-Port 443
header_up X-Forwarded-Proto https
transport http {
keepalive 5s
keepalive_idle_conns 10
}
}
}
@Fastidious
Copy link

Will this work for the latest version of Mastodon? Thanks!

@xeruf
Copy link

xeruf commented Jan 16, 2021

Which of the two is current? Do they have different use-cases?

@yukimochi
Copy link
Author

.caddy1 is for Caddy v1.x.x, .caddy2 is for Caddy v2.x.x.

@xeruf
Copy link

xeruf commented Jan 16, 2021

Thanks! I have set up my instance as per https://docs.joinmastodon.org/admin/install and added your caddy2 configuration. Unfortunately I get an error screen and am not sure how to get more useful debug output to find out whether the issue is the caddy config or in my setup.

@xeruf
Copy link

xeruf commented Jan 25, 2021

https://caddy.community/t/trying-to-use-caddy-for-mastodon-having-trouble-translating-caddyfile-from-v1-to-v2/8256/10
Seems like a rewrite to the proxy is necessary - if I visit /about it works, but there is no automatic redirection :/

@xeruf
Copy link

xeruf commented Jan 25, 2021

@mdszy if you happen to be the same person who posted on the caddy forum - do you still have an active working config?

@szyszyszyszy
Copy link

szyszyszyszy commented Jan 25, 2021

@mdszy if you happen to be the same person who posted on the caddy forum - do you still have an active working config?

hi it's me (that's my old github but apparently the email still got sent, whodathunk)

this is the config that i currently use that works, obv with a different domain but ya know

example.com {
        @try_masto {
                file
        }

        root * /home/mastodon/glitch/public

        handle @try_masto {
                file_server *
        }

        handle /api/v1/streaming* {
                reverse_proxy * localhost:4000
        }

        handle {
                reverse_proxy localhost:3000
        }

        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 * /home/mastodon/glitch/public
                rewrite 500.html
                file_server
        }
}

media.example.com {
        rewrite * /examplecom{uri}
        reverse_proxy https://cdn.xyz.abc
}

@xeruf
Copy link

xeruf commented Jan 26, 2021

@szyszyszyszy thanks for chiming in :)

Is the media-domain related to mastodon as well?

Otherwise the config seems to be the same as in that thread. Do you use the latest version of Mastodon (v3.3.0)?
With either your file or the one from the gist I can login and visit subpages like about, explore, even my account settings. But the homepage, the most important part, does not work appropriately.

@xeruf
Copy link

xeruf commented Feb 22, 2021

@szyszyszyszy @yukimochi can anyone help me? I'm kinda stuck :/

@xeruf
Copy link

xeruf commented Mar 3, 2021

Please, I'd really like to use caddy ☹

@yukimochi
Copy link
Author

Dear @xerus2000 , sorry for late response.
I checked new Caddyfile specification and behavior.
Caddy version 2.2.0>= has different behavior for file matcher with / path.

So, I added not path / in line 8.
Please use this instead. Have a good caddy life.

@paulo-roger
Copy link

@yukimochi would you mind to post your complete mastodon config? I am unable to set up this correctly (I already use caddy with other docker containers in my server, but mastodon is particularly hard for me)

@xeruf
Copy link

xeruf commented Jun 29, 2022

Woah thank you, works perfectly out of the box for me now :)

@rnaff
Copy link

rnaff commented Nov 19, 2022

@yukimochi would you mind to post your complete mastodon config? I am unable to set up this correctly (I already use caddy with other docker containers in my server, but mastodon is particularly hard for me)

Did you ever get this working correctly?

@rriemann
Copy link

@rnaff
Copy link

rnaff commented Nov 21, 2022 via email

@rnaff
Copy link

rnaff commented Nov 21, 2022 via email

@peterc
Copy link

peterc commented Nov 21, 2022

One huge pro tip here is to run something like usermod -aG mastodon caddy .. mine was refusing to load assets and static files and it took me ages to figure out it was a permissions issue :-)

@rnaff
Copy link

rnaff commented Nov 22, 2022 via email

@peterc
Copy link

peterc commented Nov 22, 2022

@rnaff I've put it up at https://gist.github.com/peterc/fef53e6a308d412ab793148a1808977a - I think it's pretty much the same as the one here.

@electricduck
Copy link

electricduck commented May 20, 2023

FYI, line 14 on v2, seems incorrect. Its working for most clients, but certain libraries seem to be throwing strange errors (as they sometimes lack the last / on the end of the URL).

Needs changing from path /api/v1/streaming/* to path /api/v1/streaming*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment