Files
jcoffey-dev cdfff73f73
ci / test (pull_request) Successful in 1m51s
--proxy snippets: nginx answers 421 for any other name on its TLS port (#48)
A browser reuses an HTTP/2 connection for every name its certificate
covers. With one certificate for the console's, the webmail's and the
mail server's names, a request for the mail server can arrive over the
webmail's connection, past the router, and nginx answered it as the
webmail or the console. A default server on 127.0.0.1:8444 now answers
any name but those two with 421 Misdirected Request, so the browser
opens a connection of its own for it, and the router passes that one
through. It needs no certificate: ssl_reject_handshake refuses a
handshake for any other name, and the router sends none.

The reporter of #48 pointed this out. Checked against nginx 1.26 with
one certificate for all three names: their own names reach the console
and the webmail, anything else gets 421; the e2e case checks it too.
2026-11-08 15:53:00 -07:00

192 lines
6.3 KiB
Cheetah

# Written by the inbuxa installer {{.Version}} for {{.Domain}}.
#
# For the web server already on this machine: nginx. Include it from
# /etc/nginx/conf.d/ (or sites-enabled/), set each ssl_certificate pair to the
# certificate you have for those names, check it with `nginx -t`, and reload.
# The installer loads nothing and restarts nothing; this is yours to adjust.
{{- if .RoutesByName}}
#
# It works with nginx-inbuxa-stream.conf beside it, which holds port 443 and
# routes it by name: that one is included from nginx.conf itself, and says
# how. Port 443 here is that router's, so nothing below listens on it.
#
# Everything is passed to loopback, where the installer put each program:
# 127.0.0.1:8081 the mail server's plain HTTP, for its certificate
# challenges on port 80
# {{.PassThroughBind}} the mail server's own HTTPS, which the name router
# passes its names to untouched
# 127.0.0.1:8082 the console
# 127.0.0.1:8080 the webmail
#
# The console and the webmail arrive from the router on {{.FrontTLSBind}},
# with the PROXY protocol header, so $remote_addr is still the client's.
{{- else}}
#
# Everything is passed to loopback, where the installer put each program:
# 127.0.0.1:8081 the mail server's web side (JMAP, CalDAV, CardDAV,
# autoconfig, MTA-STS)
# 127.0.0.1:8082 the console
# 127.0.0.1:8080 the webmail
{{- if .ServesServer}}
#
# This terminates TLS for the mail server's names with your certificate, so
# a domain added to the server later has to be added here by hand, and the
# server's _443._tcp TLSA records describe its own certificate rather than
# this one. Run the installer again with --key (an administrator's API key)
# to have nginx pass the server's names through to it instead (#48).
{{- end}}
{{- end}}
#
# The mail server trusts X-Forwarded-For from here, so every block sets it to
# the client's own address rather than passing on what the client sent.
{{- define "tls-listen"}}
{{- if .RoutesByName}}
listen {{.FrontTLSBind}} ssl proxy_protocol;
http2 on;
set_real_ip_from 127.0.0.1;
real_ip_header proxy_protocol;
{{- else}}
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
{{- end}}
{{- end}}
{{- if .ServesServer}}
# Port 80 for the server's names: its own certificate challenges, which it
# answers itself once certificates are turned on in the console, and a
# redirect for everything else. The pattern catches the same four names of
# any domain added to the server later; another name the server gets a
# certificate for goes on the server_name line too.
server {
listen 80;
listen [::]:80;
server_name {{join .ServerNames " "}}
~^(autoconfig|autodiscover|mta-sts|ua-auto-config)\.;
location /.well-known/acme-challenge/ {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
return 308 https://$host$request_uri;
}
}
{{- if not .RoutesByName}}
# The mail server's web side. Push arrives as a long-lived stream, so it is
# not buffered and not timed out early.
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name {{join .ServerNames " "}};
ssl_certificate /etc/ssl/inbuxa/{{.MailHost}}.fullchain.pem;
ssl_certificate_key /etc/ssl/inbuxa/{{.MailHost}}.key.pem;
client_max_body_size 100m;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_buffering off;
proxy_read_timeout 1h;
}
}
{{- end}}
{{- end}}
{{- if or .Console .ConsoleFiles}}
# The console: static files. It talks to the mail server from the browser,
# so nothing here proxies the API.
server {
listen 80;
listen [::]:80;
server_name {{.ConsoleHost}};
return 308 https://$host$request_uri;
}
server {
{{- template "tls-listen" .}}
server_name {{.ConsoleHost}};
ssl_certificate /etc/ssl/inbuxa/{{.ConsoleHost}}.fullchain.pem;
ssl_certificate_key /etc/ssl/inbuxa/{{.ConsoleHost}}.key.pem;
{{- if .ConsoleFiles}}
# Installed on this machine as files: served from them. Any path that
# is not a file is the app's own route; hashed assets are cached for a
# year, and index.html, which names them, never.
root {{.ConsoleFiles}};
location / {
try_files $uri $uri/ /index.html;
}
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
location = /index.html {
add_header Cache-Control "no-cache";
}
{{- else}}
location / {
proxy_pass http://127.0.0.1:8082;
proxy_set_header Host $host;
}
{{- end}}
}
{{- end}}
{{- if .ServesWebmail}}
# The webmail. Push arrives as Server-Sent Events: flushed as written, and
# held open.
server {
listen 80;
listen [::]:80;
server_name {{.WebmailHost}};
return 308 https://$host$request_uri;
}
server {
{{- template "tls-listen" .}}
server_name {{.WebmailHost}};
ssl_certificate /etc/ssl/inbuxa/{{.WebmailHost}}.fullchain.pem;
ssl_certificate_key /etc/ssl/inbuxa/{{.WebmailHost}}.key.pem;
client_max_body_size 100m;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_buffering off;
proxy_read_timeout 1h;
}
}
{{- end}}
{{- if .RoutesByName}}
# Any other name asked of {{.FrontTLSBind}}: 421, so the browser opens a new
# connection for it. A browser reuses a connection for every name its
# certificate covers, so with one certificate for the console's, the
# webmail's and the mail server's names, a request for the mail server can
# arrive here over the webmail's connection, without the router seeing its
# name. Answered from here, it would get the console or the webmail. No
# certificate: the router sends this port only the two names above, and a
# handshake for any other is refused.
server {
listen {{.FrontTLSBind}} ssl proxy_protocol default_server;
ssl_reject_handshake on;
return 421;
}
{{- end}}