Configure Nginx to Use Same Top Level Domain for Websockets and Website

When setting up a #nostr relay like strfry, you may want to use a top-level domain for your relay and the website about the relay. To be NIP-11 compliant, you need to allow calls with the header accept: application/nostr+json to hit strfry. The below nginx config allows for this.

server {

        server_name tld-relay.com;

location / {
    error_page 418 = @websocket;
# Check for NIP-11 accept to send to strfry 
        if ($http_accept = "application/nostr+json") {
        return 418;
    }

# Dummy entry to send requests to relay or static site
    try_files /nonexistent @$http_upgrade;
  }
# WSS proxy for strfry relay
  location @websocket {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:7777;
            proxy_http_version 1.1;
            proxy_read_timeout 300s;
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            send_timeout 300s;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
  }

# Static relay website
  location @ {
        root /var/www/html;
        index index.html;

  }

Follow me on Twitter