Skip to content

Instantly share code, notes, and snippets.

@aidik
Last active July 20, 2023 14:53
Show Gist options
  • Save aidik/e8eaacf9032708fa0d4fb64de2e28fd0 to your computer and use it in GitHub Desktop.
Save aidik/e8eaacf9032708fa0d4fb64de2e28fd0 to your computer and use it in GitHub Desktop.
A simple way how to serve multiple NIP-05 Identifiers using only Nginx

Nostr NIP-05 specification makes it a bit harder to serve identifiers from a simple webserver without CGI to run an external program handling the name URL query parameter. I really didn't want to do all that, so instead I focused on how to handle everything just in Nginx itself.

Prerequisite

  • A server with Nginx responding to basic requests. (minimal configuration should be enough)

Config

Inside the http block insert map and map_hash_bucket_size directives:

map_hash_bucket_size 256;
map $arg_name $nostr_key {
  default     'error - handle not found';
  'handle1'   'handle1's nostr pub key in HEX';
  'handle2'   'handle2's nostr pub key in HEX';
}

Notice that the map_hash_bucket_size is set to 256. It is my guesstimation of what should be enough for holding identities for you, your family, and friends. Consult Nginx documentation for more details.


Then add the following Location directive to the server block:

location /.well-known/nostr.json {
  add_header Access-Control-Allow-Origin *;
    if ($arg_name) {
      return 200 "{\"names\":{\"$arg_name\":\"$nostr_key\"}}";
    }
}

Again notice the addition of header allowing cross-domain requests.

Relays

If you want to, NIP-05 offers the optional relays attribute. To return it as well we will need another map module:

map $arg_name $nostr_relays {
  default     '"error - handle not found"';
  'handle1'   '["wss://example.test"]';
  'handle2'   '["wss://example.test", "wss://example2.test"]';
}

The Location then needs to be adjusted to look like to this:

location /.well-known/nostr.json {
  add_header Access-Control-Allow-Origin *;
    if ($arg_name) {
      return 200 "{\"names\":{\"$arg_name\":\"$nostr_key\"}, \"relays\": {\"$nostr_key\": $nostr_relays} }";
    }
}

License

The above text and configuration snippets are released as Public Domain.

If you like it feel free to zap away at npub1ajdaw3j4g6aqv86alhn3df8jpulj0mxz3jjgwpm4uh598hc348gqthdt20

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