Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Using a reverse proxy with Synapse
  2. It is recommended to put a reverse proxy such as
  3. [nginx](https://nginx.org/en/docs/http/ngx_http_proxy_module.html),
  4. [Apache](https://httpd.apache.org/docs/current/mod/mod_proxy_http.html),
  5. [Caddy](https://caddyserver.com/docs/quick-starts/reverse-proxy),
  6. [HAProxy](https://www.haproxy.org/) or
  7. [relayd](https://man.openbsd.org/relayd.8) in front of Synapse. One advantage
  8. of doing so is that it means that you can expose the default https port
  9. (443) to Matrix clients without needing to run Synapse with root
  10. privileges.
  11. You should configure your reverse proxy to forward requests to `/_matrix` or
  12. `/_synapse/client` to Synapse, and have it set the `X-Forwarded-For` and
  13. `X-Forwarded-Proto` request headers.
  14. You should remember that Matrix clients and other Matrix servers do not
  15. necessarily need to connect to your server via the same server name or
  16. port. Indeed, clients will use port 443 by default, whereas servers default to
  17. port 8448. Where these are different, we refer to the 'client port' and the
  18. 'federation port'. See [the Matrix
  19. specification](https://matrix.org/docs/spec/server_server/latest#resolving-server-names)
  20. for more details of the algorithm used for federation connections, and
  21. [Delegation](delegate.md) for instructions on setting up delegation.
  22. **NOTE**: Your reverse proxy must not `canonicalise` or `normalise`
  23. the requested URI in any way (for example, by decoding `%xx` escapes).
  24. Beware that Apache *will* canonicalise URIs unless you specify
  25. `nocanon`.
  26. Let's assume that we expect clients to connect to our server at
  27. `https://matrix.example.com`, and other servers to connect at
  28. `https://example.com:8448`. The following sections detail the configuration of
  29. the reverse proxy and the homeserver.
  30. ## Homeserver Configuration
  31. The HTTP configuration will need to be updated for Synapse to correctly record
  32. client IP addresses and generate redirect URLs while behind a reverse proxy.
  33. In `homeserver.yaml` set `x_forwarded: true` in the port 8008 section and
  34. consider setting `bind_addresses: ['127.0.0.1']` so that the server only
  35. listens to traffic on localhost. (Do not change `bind_addresses` to `127.0.0.1`
  36. when using a containerized Synapse, as that will prevent it from responding
  37. to proxied traffic.)
  38. Optionally, you can also set
  39. [`request_id_header`](./usage/configuration/config_documentation.md#listeners)
  40. so that the server extracts and re-uses the same request ID format that the
  41. reverse proxy is using.
  42. ## Reverse-proxy configuration examples
  43. **NOTE**: You only need one of these.
  44. ### nginx
  45. ```nginx
  46. server {
  47. listen 443 ssl http2;
  48. listen [::]:443 ssl http2;
  49. # For the federation port
  50. listen 8448 ssl http2 default_server;
  51. listen [::]:8448 ssl http2 default_server;
  52. server_name matrix.example.com;
  53. location ~ ^(/_matrix|/_synapse/client) {
  54. # note: do not add a path (even a single /) after the port in `proxy_pass`,
  55. # otherwise nginx will canonicalise the URI and cause signature verification
  56. # errors.
  57. proxy_pass http://localhost:8008;
  58. proxy_set_header X-Forwarded-For $remote_addr;
  59. proxy_set_header X-Forwarded-Proto $scheme;
  60. proxy_set_header Host $host;
  61. # Nginx by default only allows file uploads up to 1M in size
  62. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  63. client_max_body_size 50M;
  64. # Synapse responses may be chunked, which is an HTTP/1.1 feature.
  65. proxy_http_version 1.1;
  66. }
  67. }
  68. ```
  69. ### Caddy v2
  70. ```
  71. matrix.example.com {
  72. reverse_proxy /_matrix/* localhost:8008
  73. reverse_proxy /_synapse/client/* localhost:8008
  74. }
  75. example.com:8448 {
  76. reverse_proxy /_matrix/* localhost:8008
  77. }
  78. ```
  79. [Delegation](delegate.md) example:
  80. ```
  81. example.com {
  82. header /.well-known/matrix/* Content-Type application/json
  83. header /.well-known/matrix/* Access-Control-Allow-Origin *
  84. respond /.well-known/matrix/server `{"m.server": "matrix.example.com:443"}`
  85. respond /.well-known/matrix/client `{"m.homeserver":{"base_url":"https://matrix.example.com"},"m.identity_server":{"base_url":"https://identity.example.com"}}`
  86. }
  87. matrix.example.com {
  88. reverse_proxy /_matrix/* localhost:8008
  89. reverse_proxy /_synapse/client/* localhost:8008
  90. }
  91. ```
  92. ### Apache
  93. ```apache
  94. <VirtualHost *:443>
  95. SSLEngine on
  96. ServerName matrix.example.com
  97. RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
  98. AllowEncodedSlashes NoDecode
  99. ProxyPreserveHost on
  100. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  101. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  102. ProxyPass /_synapse/client http://127.0.0.1:8008/_synapse/client nocanon
  103. ProxyPassReverse /_synapse/client http://127.0.0.1:8008/_synapse/client
  104. </VirtualHost>
  105. <VirtualHost *:8448>
  106. SSLEngine on
  107. ServerName example.com
  108. RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
  109. AllowEncodedSlashes NoDecode
  110. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  111. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  112. </VirtualHost>
  113. ```
  114. **NOTE**: ensure the `nocanon` options are included.
  115. **NOTE 2**: It appears that Synapse is currently incompatible with the ModSecurity module for Apache (`mod_security2`). If you need it enabled for other services on your web server, you can disable it for Synapse's two VirtualHosts by including the following lines before each of the two `</VirtualHost>` above:
  116. ```apache
  117. <IfModule security2_module>
  118. SecRuleEngine off
  119. </IfModule>
  120. ```
  121. **NOTE 3**: Missing `ProxyPreserveHost on` can lead to a redirect loop.
  122. ### HAProxy
  123. ```
  124. frontend https
  125. bind *:443,[::]:443 ssl crt /etc/ssl/haproxy/ strict-sni alpn h2,http/1.1
  126. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  127. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  128. http-request set-header X-Forwarded-For %[src]
  129. # Matrix client traffic
  130. acl matrix-host hdr(host) -i matrix.example.com matrix.example.com:443
  131. acl matrix-path path_beg /_matrix
  132. acl matrix-path path_beg /_synapse/client
  133. use_backend matrix if matrix-host matrix-path
  134. frontend matrix-federation
  135. bind *:8448,[::]:8448 ssl crt /etc/ssl/haproxy/synapse.pem alpn h2,http/1.1
  136. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  137. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  138. http-request set-header X-Forwarded-For %[src]
  139. default_backend matrix
  140. backend matrix
  141. server matrix 127.0.0.1:8008
  142. ```
  143. Example configuration, if using a UNIX socket. The configuration lines regarding the frontends do not need to be modified.
  144. ```
  145. backend matrix
  146. server matrix unix@/run/synapse/main_public.sock
  147. ```
  148. [Delegation](delegate.md) example:
  149. ```
  150. frontend https
  151. acl matrix-well-known-client-path path /.well-known/matrix/client
  152. acl matrix-well-known-server-path path /.well-known/matrix/server
  153. use_backend matrix-well-known-client if matrix-well-known-client-path
  154. use_backend matrix-well-known-server if matrix-well-known-server-path
  155. backend matrix-well-known-client
  156. http-after-response set-header Access-Control-Allow-Origin "*"
  157. http-after-response set-header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
  158. http-after-response set-header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  159. http-request return status 200 content-type application/json string '{"m.homeserver":{"base_url":"https://matrix.example.com"},"m.identity_server":{"base_url":"https://identity.example.com"}}'
  160. backend matrix-well-known-server
  161. http-after-response set-header Access-Control-Allow-Origin "*"
  162. http-after-response set-header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
  163. http-after-response set-header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  164. http-request return status 200 content-type application/json string '{"m.server":"matrix.example.com:443"}'
  165. ```
  166. ### Relayd
  167. ```
  168. table <webserver> { 127.0.0.1 }
  169. table <matrixserver> { 127.0.0.1 }
  170. http protocol "https" {
  171. tls { no tlsv1.0, ciphers "HIGH" }
  172. tls keypair "example.com"
  173. match header set "X-Forwarded-For" value "$REMOTE_ADDR"
  174. match header set "X-Forwarded-Proto" value "https"
  175. # set CORS header for .well-known/matrix/server, .well-known/matrix/client
  176. # httpd does not support setting headers, so do it here
  177. match request path "/.well-known/matrix/*" tag "matrix-cors"
  178. match response tagged "matrix-cors" header set "Access-Control-Allow-Origin" value "*"
  179. pass quick path "/_matrix/*" forward to <matrixserver>
  180. pass quick path "/_synapse/client/*" forward to <matrixserver>
  181. # pass on non-matrix traffic to webserver
  182. pass forward to <webserver>
  183. }
  184. relay "https_traffic" {
  185. listen on egress port 443 tls
  186. protocol "https"
  187. forward to <matrixserver> port 8008 check tcp
  188. forward to <webserver> port 8080 check tcp
  189. }
  190. http protocol "matrix" {
  191. tls { no tlsv1.0, ciphers "HIGH" }
  192. tls keypair "example.com"
  193. block
  194. pass quick path "/_matrix/*" forward to <matrixserver>
  195. pass quick path "/_synapse/client/*" forward to <matrixserver>
  196. }
  197. relay "matrix_federation" {
  198. listen on egress port 8448 tls
  199. protocol "matrix"
  200. forward to <matrixserver> port 8008 check tcp
  201. }
  202. ```
  203. ## Health check endpoint
  204. Synapse exposes a health check endpoint for use by reverse proxies.
  205. Each configured HTTP listener has a `/health` endpoint which always returns
  206. 200 OK (and doesn't get logged).
  207. ## Synapse administration endpoints
  208. Endpoints for administering your Synapse instance are placed under
  209. `/_synapse/admin`. These require authentication through an access token of an
  210. admin user. However as access to these endpoints grants the caller a lot of power,
  211. we do not recommend exposing them to the public internet without good reason.