# mx.vish.gg - Primary Matrix server (federation enabled)
server {
    listen 8082;
    listen [::]:8082;
    server_name mx.vish.gg;

    # Element Web client
    root /opt/element/web;
    index index.html;

    # Health check
    location /health {
        proxy_pass http://127.0.0.1:8018;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
    }

    # Client-Server API
    location ~ ^(/_matrix|/_synapse/client) {
        proxy_pass http://127.0.0.1:8018;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
        client_max_body_size 100M;
        proxy_http_version 1.1;
    }

    # Federation API
    location /_matrix/federation {
        proxy_pass http://127.0.0.1:8018;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
        client_max_body_size 100M;
    }

    # Well-known for federation
    location /.well-known/matrix/server {
        default_type application/json;
        return 200 '{"m.server": "mx.vish.gg:443"}';
    }

    location /.well-known/matrix/client {
        default_type application/json;
        add_header Access-Control-Allow-Origin *;
        return 200 '{"m.homeserver": {"base_url": "https://mx.vish.gg"}}';
    }

    # Element static files
    location / {
        try_files $uri $uri/ /index.html;
    }
}
