Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 your@mail-address.example.com
}
{
email your@mail-address.example.com
}
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

This comment has been minimized.

Copy link

@Fastidious Fastidious commented Nov 15, 2018

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

@xeruf

This comment has been minimized.

Copy link

@xeruf xeruf commented Jan 16, 2021

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

@yukimochi

This comment has been minimized.

Copy link
Owner Author

@yukimochi yukimochi commented Jan 16, 2021

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

@xeruf

This comment has been minimized.

Copy link

@xeruf 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

This comment has been minimized.

Copy link

@xeruf 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

This comment has been minimized.

Copy link

@xeruf 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

This comment has been minimized.

Copy link

@szyszyszyszy 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

This comment has been minimized.

Copy link

@xeruf 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

This comment has been minimized.

Copy link

@xeruf xeruf commented Feb 22, 2021

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

@xeruf

This comment has been minimized.

Copy link

@xeruf xeruf commented Mar 3, 2021

Please, I'd really like to use caddy

@yukimochi

This comment has been minimized.

Copy link
Owner Author

@yukimochi yukimochi commented Mar 7, 2021

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.

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