Hagency 不要求你把自己的域名指向我们的 DNS、边缘节点或 workspace 主机。
如果你希望用户以 @alice:example.com 这样的形式登录,你只需要在自己的域名下发布 Matrix 标准发现文件:
/.well-known/matrix/server/.well-known/matrix/clientexample.com 当前的网站、DNS 和托管方式都可以保持不变。唯一要求是这两个 HTTPS 地址能返回控制台生成的 JSON。
@alice:example.com/.well-known,再跳转到 JSON 里声明的 homeserver URLexample.com。注意:
server_name/.well-known #https://example.com/.well-known/matrix/serverhttps://example.com/.well-known/matrix/client这个流程里,你的域名 不需要 增加任何指向 Hagency 的 A、CNAME 或 TXT 记录。
你可以把这两个文件放在:
如果你用的是 https://app.element.io 这类浏览器里的 Matrix 客户端,那就不是“能打开 JSON 就行”。
浏览器会把 https://example.com/.well-known/matrix/client 当成跨域请求读取。你的地址栏里即使能看到 JSON,只要响应头不对,客户端仍然可能把这次自动发现视为失败。
至少要保证:
/.well-known/matrix/client 返回 Content-Type: application/json/.well-known/matrix/client 返回 Access-Control-Allow-Origin: */.well-known/matrix/server 也一起返回 Content-Type: application/json 和 Access-Control-Allow-Origin: *常见误区:
https://example.com/_matrix/client/versions,说明 base_url 应该改成 https://example.com。” 通常不是。更常见的原因是客户端没能成功读取 /.well-known/matrix/client,于是退回去把 example.com 当成直连 homeserver 试探。m.homeserver.base_url 应该继续使用控制台生成的真实 workspace homeserver URL,通常是 https://<workspace>.hagency.cloud 这一类地址。除非你真的把 example.com/_matrix/client/* 这一整套 Matrix Client-Server API 正式反代出来,否则不要手工把 base_url 改成你的自定义域名。
发布后,建议用命令行直接检查响应头:
curl -i https://example.com/.well-known/matrix/client curl -i https://example.com/.well-known/matrix/server
至少应看到:
HTTP/1.1 200 或 HTTP/2 200Content-Type: application/jsonAccess-Control-Allow-Origin: *下面的示例都假设:
/.well-known/matrix/client 和 /.well-known/matrix/server 这两个路径被单独拦出来example.com { root * /srv/well-known @matrix_well_known path /.well-known/matrix/client /.well-known/matrix/server handle @matrix_well_known { header Content-Type application/json header Access-Control-Allow-Origin * file_server } reverse_proxy 127.0.0.1:8080 }
把两个文件放到:
/srv/well-known/.well-known/matrix/client/srv/well-known/.well-known/matrix/serverserver { listen 443 ssl http2; server_name example.com; root /var/www/example.com; location = /.well-known/matrix/client { default_type application/json; add_header Access-Control-Allow-Origin * always; try_files $uri =404; } location = /.well-known/matrix/server { default_type application/json; add_header Access-Control-Allow-Origin * always; try_files $uri =404; } location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
这里的两个文件路径是:
/var/www/example.com/.well-known/matrix/client/var/www/example.com/.well-known/matrix/server启用 mod_proxy、mod_proxy_http、mod_alias、mod_headers 后,可以这样写:
<VirtualHost *:443> ServerName example.com ProxyPreserveHost On ProxyPass /.well-known/matrix/ ! Alias /.well-known/matrix/ "/var/www/example.com/.well-known/matrix/" <Directory "/var/www/example.com/.well-known/matrix"> Require all granted </Directory> <LocationMatch "^/\.well-known/matrix/(client|server)$"> ForceType application/json Header always set Access-Control-Allow-Origin "*" </LocationMatch> ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ </VirtualHost>
Traefik 本身只负责路由和加头;你仍然需要一个实际提供这两个 JSON 文件的后端服务,例如一个很小的静态 Nginx 容器。下面是给这个静态服务挂的 labels 示例:
labels: - "traefik.enable=true" - "traefik.http.routers.example-matrix-wellknown.rule=Host(`example.com`) && (Path(`/.well-known/matrix/client`) || Path(`/.well-known/matrix/server`))" - "traefik.http.routers.example-matrix-wellknown.priority=100" - "traefik.http.routers.example-matrix-wellknown.entrypoints=websecure" - "traefik.http.routers.example-matrix-wellknown.tls=true" - "traefik.http.routers.example-matrix-wellknown.middlewares=matrix-wellknown-headers" - "traefik.http.routers.example-matrix-wellknown.service=matrix-wellknown" - "traefik.http.middlewares.matrix-wellknown-headers.headers.customresponseheaders.Content-Type=application/json" - "traefik.http.middlewares.matrix-wellknown-headers.headers.customresponseheaders.Access-Control-Allow-Origin=*" - "traefik.http.services.matrix-wellknown.loadbalancer.server.port=80"
你的主应用继续用原来的 router 处理其余路径即可;/.well-known/matrix/* 这两个请求会优先落到这个静态服务。
发布后,回到控制台点击 Validate Published Files。
系统会检查:
/.well-known/matrix/server 是否返回预期值/.well-known/matrix/client 是否返回预期值example.comhttps://example.com/.well-known/matrix/server 告诉客户端真正该连到哪里https://example.com/.well-known/matrix/client 告诉客户端真正的 base_url换句话说,你的域名只负责 Matrix 身份和发现入口,不需要把普通 Web 流量转发给 Hagency。
/.well-known 校验失败:确认两个文件发布在准确路径,且内容与控制台当前显示的 JSON 完全一致。app.element.io 仍然失败:优先检查 /.well-known/matrix/client 是否真的返回了 Content-Type: application/json 和 Access-Control-Allow-Origin: *。https://example.com/_matrix/client/versions:这通常说明浏览器侧自动发现失败了,不要先去改 m.homeserver.base_url;先检查 /.well-known/matrix/client 的 CORS 和 Content-Type。/.well-known:直接把 Matrix 文件合并进同一个 /.well-known 目录即可,不需要迁移整个站点。A、CNAME 或 TXT 记录。