Files

246 lines
5.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 云端只跑 frps 时的 HTTPS 配置
适用于:云端服务器只做 frps 转发,没有云端 Nginx;所有端口可用。
## 结论
需要改三处:
1. **云端 frps.toml**:增加 `vhostHTTPSPort = 443`
2. **本地 frpc.toml**:`blog_sit` 从 `type = "http"` 改为 `type = "https"`
3. **本地 Nginx 9001**:从 HTTP 改为 HTTPS,使用 `an.xiaoan.xyz` 证书
Django 不需要改:项目已经设置了:
```python
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
CSRF_TRUSTED_ORIGINS = ["https://an.xiaoan.xyz", ...]
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
```
> frp 的 HTTPS 代理**不会在 frps 终止 TLS**,证书必须放在本地 Nginx。
> 云端 frps 不需要证书,也不需要云端 Nginx。
## 最终链路
```text
Browser
│ https://an.xiaoan.xyz:443
▼
云服务器 frps:443(vhostHTTPSPort,按 SNI 路由,不解密)
│ frp tunnel
▼
本地 frpc(type="https", localPort=9001)
│ https://127.0.0.1:9001
▼
本地 Nginx 9001(TLS 终止,证书 an.xiaoan.xyz)
│ http://127.0.0.1:8000
▼
Gunicorn 8000 -> Django
```
git 代理保持原样:
```toml
[[proxies]]
name = "Git_sit_"
type = "http"
localPort = 3000
customDomains = ["git.xiaoan.xyz"]
```
## 1. 云端 frps
云服务器 `/etc/frp/frps.toml`(或实际路径):
```toml
bindPort = 7000
vhostHTTPPort = 80 # 保留,给 git 等 HTTP 代理
vhostHTTPSPort = 443 # 新增,给 an.xiaoan.xyz 的 HTTPS
```
重启:
```bash
sudo systemctl restart frps
# 或实际服务名:systemctl restart frps.service
```
确认 443 已监听:
```bash
sudo ss -lntp | grep ':443'
```
> 云安全组/防火墙放行 443/tcp;你说所有端口可用,通常不需要额外操作。
## 2. 本地 Nginx 9001 改为 HTTPS
把:
```nginx
listen 9001;
```
改成:
```nginx
listen 9001 ssl http2;
```
并在 `server {` 内加入:
```nginx
ssl_certificate /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem;
ssl_certificate_key /etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
```
完整文件已放在:
```text
deploy/nginx/an.xiaoan.xyz.https.conf
```
安装并 reload:
```bash
sudo cp deploy/nginx/an.xiaoan.xyz.https.conf /etc/nginx/sites-available/an.xiaoan.xyz
sudo ln -sfn /etc/nginx/sites-available/an.xiaoan.xyz /etc/nginx/sites-enabled/an.xiaoan.xyz
sudo nginx -t
sudo systemctl reload nginx
```
证书已在本机:
```text
/etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz_bundle.pem
/etc/nginx/ssl/an.xiaoan.xyz/an.xiaoan.xyz.key (600)
```
验证本地 TLS:
```bash
curl -k -I -H 'Host: an.xiaoan.xyz' https://127.0.0.1:9001/
```
## 3. 本地 frpc
把 `blog_sit` 代理改成:
```toml
[[proxies]]
name = "blog_sit"
type = "https"
localIP = "127.0.0.1"
localPort = 9001
customDomains = ["an.xiaoan.xyz"]
```
完整片段:`deploy/frpc-blog-https.toml`。
重启 frpc。**frpc 必须做成 systemd**,不要再用终端里的 `./frpc`,否则关闭终端会断掉隧道:
```bash
sudo install -m 644 deploy/systemd/frpc.service /etc/systemd/system/frpc.service
sudo systemctl daemon-reload
sudo systemctl enable --now frpc
sudo systemctl status frpc
sudo journalctl -u frpc -n 20 --no-pager
```
如果已经装了 frpc.service,改完配置后:
```bash
sudo systemctl restart frpc
```
确认 frpc 日志没有 `connect to local service error` 或 TLS 相关错误。
## 4. 验证
```bash
# 云端 443 是否开着
nc -vz 110.42.136.46 443
# 本地 Nginx TLS
curl -k -I -H 'Host: an.xiaoan.xyz' https://127.0.0.1:9001/
# 公网 HTTPS
curl -I https://an.xiaoan.xyz/
# 证书
echo | openssl s_client -servername an.xiaoan.xyz -connect an.xiaoan.xyz:443 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates
```
预期:公网返回 200,证书 subject 为 `CN = an.xiaoan.xyz`。
## 5. 可选:HTTP 跳转 HTTPS
改完后 `http://an.xiaoan.xyz` 不再有代理。如果想保留跳转:
本地新增一个 HTTP server(9002):
```nginx
server {
listen 9002;
server_name an.xiaoan.xyz;
return 301 https://$host$request_uri;
}
```
文件:`deploy/nginx/an.xiaoan.xyz.http-redirect.conf`。
frpc 增加:
```toml
[[proxies]]
name = "blog_http"
type = "http"
localIP = "127.0.0.1"
localPort = 9002
customDomains = ["an.xiaoan.xyz"]
```
frps 继续使用 `vhostHTTPPort = 80`;git 和 an 的 HTTP 会按域名分流。
## 6. 回滚
如果 HTTPS 切换失败:
```toml
# frpc
type = "http"
```
```nginx
# 本地 nginx
listen 9001;
```
```bash
sudo nginx -t && sudo systemctl reload nginx
# 重启 frpc
```
云端 frps 的 `vhostHTTPSPort` 可以保留,不影响 HTTP 代理。
## 7. 注意事项
- **frps 不终止 TLS**,证书在本地 Nginx;云端不需要证书。
- `type="https"` 时本地服务必须是 HTTPS;如果本地 Nginx 仍是 HTTP,frpc 会连接失败。
- `vhostHTTPSPort = 443` 与云端已有的 443 服务不能冲突;你说云端只有 frp,通常没有冲突。
- 切换期间会短暂中断:建议顺序为 **云端 frps 先加 443 → 本地 Nginx 改 TLS → 本地 frpc 改 https**。
- 当前本地 Nginx 9001 还是 HTTP、frpc 还是 `type="http"`;按本文切换后才是 HTTPS。
- 证书续期后只需替换本地两个文件并 `systemctl reload nginx`,frps/frpc 不用动。