Skip to content

Commit 30e2ec7

Browse files
committed
Use Cloudflare origin certificate for API TLS
Cloudflare now proxies api.activity-coaching.com in Full strict mode, so nginx should validate with a Cloudflare Origin Certificate mounted from server-local certificate material. Constraint: Origin private key must remain server-local and out of rsync/git. Rejected: Keep Let's Encrypt as the active nginx certificate | it keeps an unnecessary renewal path for the proxied API origin. Confidence: high Scope-risk: narrow Directive: Do not commit cloudflare-origin certificate material; keep it provisioned on the Lightsail host. Tested: docker compose -f compose.prod.yml config; ./gradlew test --no-daemon Not-tested: Live Cloudflare proxied request before redeploy.
1 parent 7539817 commit 30e2ec7

5 files changed

Lines changed: 28 additions & 15 deletions

File tree

‎.github/workflows/deploy-backend.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ jobs:
7070
--exclude '.env' \
7171
--exclude 'letsencrypt' \
7272
--exclude 'certbot-www' \
73+
--exclude 'cloudflare-origin' \
7374
-e "ssh -i ~/.ssh/lightsail_key" \
7475
./ "${{ secrets.LIGHTSAIL_USER }}@${{ secrets.LIGHTSAIL_HOST }}:/opt/chatbot-backend/"
7576

‎.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ out/
4444
# Production-only certificate material
4545
/letsencrypt/
4646
/certbot-www/
47+
/cloudflare-origin/
48+
49+
# macOS metadata
50+
.DS_Store
51+
**/.DS_Store

‎compose.prod.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ services:
6565
- ./deploy/nginx/conf.d:/etc/nginx/conf.d:ro
6666
- ./certbot-www:/var/www/certbot:ro
6767
- ./letsencrypt:/etc/letsencrypt:ro
68+
- ./cloudflare-origin:/etc/cloudflare-origin:ro
6869
networks:
6970
- chatbot
7071

‎deploy/README.md‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
This backend is intended to run on one Lightsail instance at:
44

5-
- API domain: `api.chatbot.한국`
6-
- TLS/domain value for nginx/certbot: `api.chatbot.xn--3e0b707e`
5+
- Frontend domain: `activity-coaching.com`
6+
- API domain: `api.activity-coaching.com`
77

88
## 1. Lightsail firewall
99

1010
In the Lightsail instance networking tab, allow:
1111

1212
- TCP `22` from your IP for SSH
13-
- TCP `80` from anywhere for HTTP/Let's Encrypt
13+
- TCP `80` from anywhere for HTTP redirects
1414
- TCP `443` from anywhere for HTTPS
1515

1616
Do not expose PostgreSQL `5432` or Redis `6379` publicly.
@@ -32,18 +32,25 @@ sudo chown -R "$USER":"$USER" /opt/chatbot-backend
3232

3333
The GitHub Actions workflow writes `/opt/chatbot-backend/.env` automatically from repository secrets.
3434

35-
## 3. First TLS certificate
35+
## 3. Cloudflare Origin Certificate
3636

37-
After DNS `api.chatbot.한국` points to this instance, run once on the server:
37+
The public DNS records are managed by Cloudflare. The recommended production
38+
TLS mode is:
39+
40+
- DNS proxy: enabled for `api.activity-coaching.com`
41+
- SSL/TLS mode: `Full (strict)`
42+
- Origin certificate: Cloudflare Origin Certificate installed on Lightsail
43+
44+
The certificate files must exist on the server before starting nginx:
3845

3946
```bash
40-
cd /opt/chatbot-backend
41-
set -a
42-
source .env
43-
set +a
44-
bash deploy/init-letsencrypt.sh
47+
/opt/chatbot-backend/cloudflare-origin/api.activity-coaching.com.pem
48+
/opt/chatbot-backend/cloudflare-origin/api.activity-coaching.com.key
4549
```
4650

51+
Generate the private key and CSR on the server, submit only the CSR to
52+
Cloudflare, then save the issued certificate as the `.pem` file above.
53+
4754
## 4. GitHub Actions secrets
4855

4956
Add these repository secrets:
@@ -54,13 +61,12 @@ Add these repository secrets:
5461
- `POSTGRES_PASSWORD`: production PostgreSQL password
5562
- `JWT_SECRET`: long random JWT signing secret
5663
- `OPENAI_API_KEY`: OpenAI API key
57-
- `LETSENCRYPT_EMAIL`: email for Let's Encrypt notices
5864

5965
Optional repository secrets:
6066

6167
- `POSTGRES_DB`: defaults to `ai_rag_db`
6268
- `POSTGRES_USER`: defaults to `admin`
63-
- `APP_CORS_ALLOWED_ORIGINS`: defaults to `https://chatbot.xn--3e0b707e,https://chatbot.한국`
69+
- `APP_CORS_ALLOWED_ORIGINS`: defaults to `https://activity-coaching.com`
6470

6571
Push to `main` or run the workflow manually. The workflow uploads the backend source to `/opt/chatbot-backend` and runs:
6672

@@ -73,5 +79,5 @@ docker compose -f compose.prod.yml up -d --build
7379
```bash
7480
docker compose -f compose.prod.yml ps
7581
docker compose -f compose.prod.yml logs -f app
76-
curl -i https://api.chatbot.xn--3e0b707e/api/v1/auth/reissue
82+
curl -i https://api.activity-coaching.com/api/v1/auth/login
7783
```

‎deploy/nginx/conf.d/api.conf‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ server {
1515
listen 443 ssl http2;
1616
server_name api.activity-coaching.com;
1717

18-
ssl_certificate /etc/letsencrypt/live/api.activity-coaching.com/fullchain.pem;
19-
ssl_certificate_key /etc/letsencrypt/live/api.activity-coaching.com/privkey.pem;
18+
ssl_certificate /etc/cloudflare-origin/api.activity-coaching.com.pem;
19+
ssl_certificate_key /etc/cloudflare-origin/api.activity-coaching.com.key;
2020

2121
client_max_body_size 100m;
2222

0 commit comments

Comments
 (0)