Mastodon discoverability on your domain using the nginx location directive

tl;dr: Puts the .well-known/webfinger content in your server config instead of the filesystem.

This setup is inspired by Maarten Balliauw‘s Mastodon on your own domain without hosting a server. Please read the linked post for details. The general idea is you can create a special file on your website that allows people to find you on Mastodon by searching for @you@yourdomain.tld even though you don’t run an instance on your domain.

Here’s my twist: What if you can’t or don’t want to add this file directly to the filesystem where your web content lives? Maybe your site is running a CMS that precludes adding arbitrary files. Or you just don’t want to forget to preserve the file between software upgrades and reinstalls. Whatever your reason, you can do this by putting the file content inside of a location directive in your nginx config instead.

These instructions assume you have a working knowledge of nginx configuration and server administration.

  1. In a browser navigate to https://<your mastodon server>/.well-known/webfinger?resource=acct:<your account>@<your mastodon server>
  2. Select and copy the json text that appears on the page.
  3. Edit the nginx config file that contains the server block for your domain.
  4. Add a new location directive like below, replacing your-code-here with the json that was copied in step 2.
    location /.well-known/webfinger {
         return 200 'your-code-here'; 
    }
  5. Save your config and restart nginx.