Repository navigation
Expand file tree
/
Copy pathnginx-https.conf
More file actions
91 lines (82 loc) · 3.81 KB
/
Copy pathnginx-https.conf
File metadata and controls
91 lines (82 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# ── 統一 HTTPS 入口(Tailscale 網域)────────────────────────────────
# 前端 + 後端同源:同一網址、同一 port(443),取代跨來源 CORS。
# TLS 憑證由 tailscale cert 產生,掛載於 /etc/nginx/certs。
# API 前綴有兩個:/api/(auth/user)與 /admin/api/(accounting/stats/budget…),皆代理到後端。
#
# 上游主機名(backend/frontend)以「變數 + resolver」動態解析:
# 字面 proxy_pass http://backend:8080 會在啟動當下解析 DNS,失敗就 [emerg] 拒啟不重試;
# 改用變數會延到「請求時」才透過 Docker 內建 DNS(127.0.0.11) 解析,
# backend 重啟或 DNS 短暫延遲都不會讓 proxy 掛掉。
# HTTP → HTTPS 轉址
server {
listen 80;
server_name ubuntu-server.tail886591.ts.net;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name ubuntu-server.tail886591.ts.net;
ssl_certificate /etc/nginx/certs/ubuntu-server.tail886591.ts.net.crt;
ssl_certificate_key /etc/nginx/certs/ubuntu-server.tail886591.ts.net.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
# 對齊後端 MAX_CONTENT_LENGTH(16MB)
client_max_body_size 16m;
# Docker 內建 DNS;valid=10s 讓解析結果只快取 10 秒(容器換 IP 也能跟上)
resolver 127.0.0.11 ipv6=off valid=10s;
# 共用 proxy 標頭(server 層設定,location 只寫 proxy_pass 即繼承)
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 舊路徑相容:v2 已改到根目錄,/v2/ 與 /test/v2/ 301 導向新位置(保留尾段路徑)
# 供舊書籤/連結用;已安裝的 PWA(scope /v2/)仍建議重裝一次。
location /v2/ {
rewrite ^/v2/(.*)$ /$1 permanent;
}
location /test/v2/ {
rewrite ^/test/v2/(.*)$ /test/$1 permanent;
}
# ── 測試環境(/test/,develop 分支的 *-test 容器)────────────────────────
# 用 rewrite 剝掉 /test 前綴,再以「改寫後的 $uri」轉給後端(後端路由是 /api,不是 /test/api)。
# 注意:這幾條 proxy_pass 不能接 $request_uri(那是未改寫的原始 URI,會把 /test 加回去)。
location /test/api/ {
set $tb accounting-backend-test;
rewrite ^/test(/.*)$ $1 break;
proxy_pass http://$tb:8080;
}
location /test/admin/api/ {
set $tb accounting-backend-test;
rewrite ^/test(/.*)$ $1 break;
proxy_pass http://$tb:8080;
}
location = /test { return 301 /test/; }
location /test/ {
set $tf accounting-frontend-test;
rewrite ^/test(/.*)$ $1 break;
proxy_pass http://$tf:80;
}
# ── 正式環境(/,release 分支的容器)────────────────────────────────────
# 用變數 → 請求時才解析;$request_uri 保留完整路徑與查詢字串(正式不需剝前綴)
location /api/ {
set $backend accounting-backend;
proxy_pass http://$backend:8080$request_uri;
}
location /admin/api/ {
set $backend accounting-backend;
proxy_pass http://$backend:8080$request_uri;
}
location = /health {
set $backend accounting-backend;
proxy_pass http://$backend:8080$request_uri;
}
location / {
set $frontend accounting-frontend;
proxy_pass http://$frontend:80$request_uri;
}
}