Skip to content

Commit 04c27f7

Browse files
committed
use sni-based routing with nginx for web,api,caldav, and carddav
1 parent 8ad70d5 commit 04c27f7

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

self-hosting/docker-compose-self-hosted.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
services:
2+
nginx:
3+
image: nginx:stable
4+
container_name: sni-router
5+
network_mode: host
6+
volumes:
7+
- ./nginx.conf:/etc/nginx/nginx.conf:ro
8+
restart: unless-stopped
29
web:
310
image: ghcr.io/forwardemail/forwardemail.net-selfhosted:latest
411
container_name: web

self-hosting/nginx.conf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
worker_rlimit_nofile 65535;
2+
worker_processes auto;
3+
4+
events {
5+
worker_connections 8192;
6+
multi_accept on;
7+
}
8+
9+
# This stream block is acting as a pass-through proxy based on SNI.
10+
# - Each upstream service (web, api, caldav, carddav) still handles TLS.
11+
stream {
12+
# Fail fast. This applies to all upstreams (web, api, caldav, carddav).
13+
proxy_connect_timeout 5s;
14+
15+
# Accommodate large headers/cookies in the initial response chunk
16+
proxy_buffer_size 16k;
17+
18+
# Enable TCP keepalive to detect dead connections and prevent timeouts
19+
proxy_socket_keepalive on;
20+
21+
map $ssl_preread_server_name $backend {
22+
# carddav.example.com, caldav.example.com, api.example.com
23+
~^carddav\. carddav;
24+
~^caldav\. caldav;
25+
~^api\. api;
26+
# example.com and any other hostname
27+
default web;
28+
}
29+
30+
server {
31+
listen 443;
32+
listen [::]:443;
33+
34+
ssl_preread on; # Use SNI information to route to correct backend
35+
proxy_pass $backend;
36+
}
37+
38+
upstream web {
39+
server 127.0.0.1:3000;
40+
}
41+
42+
upstream api {
43+
server 127.0.0.1:4000;
44+
}
45+
46+
upstream caldav {
47+
server 127.0.0.1:5000;
48+
}
49+
50+
upstream carddav {
51+
server 127.0.0.1:6000;
52+
}
53+
}

self-hosting/setup.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ update_env_file() {
473473
update_default_env() {
474474
update_env_file NODE_ENV production
475475
update_env_file HTTP_PROTOCOL https
476-
update_env_file SQLITE_HOST sqlite.{{DOMAIN}}
476+
update_env_file WEB_URL https://{{DOMAIN}}
477477
update_env_file WEB_HOST {{DOMAIN}}
478-
update_env_file WEB_PORT 443
478+
update_env_file SQLITE_HOST sqlite.{{DOMAIN}}
479479
update_env_file CALDAV_HOST caldav.{{DOMAIN}}
480480
update_env_file CARDDAV_HOST carddav.{{DOMAIN}}
481481
update_env_file API_HOST api.{{DOMAIN}}

0 commit comments

Comments
 (0)