first commit

This commit is contained in:
Michael Dong
2026-02-05 11:24:40 +08:00
commit a98e12f286
144 changed files with 26459 additions and 0 deletions

141
nginx/README.md Normal file
View File

@@ -0,0 +1,141 @@
# Nginx 部署指南
## 前置条件
- 域名已解析到服务器 IP
- 服务器已安装 Docker、docker-compose、nginx 和 certbot
## 完整部署流程
### 0. 配置环境变量
```bash
# 复制示例配置
cp .env.example .env
# 编辑配置(设置密码和域名)
vim .env
```
`.env` 示例:
```
POSTGRES_PASSWORD=your_secure_password
JWT_SECRET=$(openssl rand -base64 32)
BASE_URL=https://notify.example.com
NEXT_PUBLIC_API_URL=https://notify.example.com
```
### 0.1 启动服务
```bash
# 使用生产配置启动
docker-compose -f docker-compose.prod.yml up -d --build
```
## 部署步骤
### 1. 安装 nginx 和 certbot
```bash
# Ubuntu/Debian
sudo apt update
sudo apt install -y nginx certbot python3-certbot-nginx
# CentOS/RHEL
sudo yum install -y nginx certbot python3-certbot-nginx
```
### 2. 复制配置文件
```bash
# 将配置文件复制到 nginx 配置目录
sudo cp notify.conf /etc/nginx/sites-available/notify.conf
# 创建软链接启用配置
sudo ln -s /etc/nginx/sites-available/notify.conf /etc/nginx/sites-enabled/
# 删除默认配置(可选)
sudo rm /etc/nginx/sites-enabled/default
```
### 3. 修改域名
编辑 `/etc/nginx/sites-available/notify.conf`,将 `notify.example.com` 替换为你的实际域名。
### 4. 获取 SSL 证书
**方法一:先用 HTTP 获取证书**
先注释掉 HTTPS server 块,只保留 HTTP 配置:
```bash
sudo nginx -t && sudo systemctl reload nginx
# 获取证书
sudo certbot certonly --webroot -w /var/www/certbot -d notify.example.com
```
然后取消注释 HTTPS 配置并重新加载。
**方法二:使用 certbot nginx 插件(推荐)**
```bash
# 先使用简化的 HTTP 配置
sudo certbot --nginx -d notify.example.com
```
### 5. 测试并启动
```bash
# 测试配置
sudo nginx -t
# 重新加载配置
sudo systemctl reload nginx
# 确保 nginx 开机自启
sudo systemctl enable nginx
```
### 6. 证书自动续期
Certbot 默认会创建定时任务自动续期,可以测试:
```bash
sudo certbot renew --dry-run
```
## 端口说明
| 服务 | 内部端口 | 说明 |
|------|---------|------|
| frontend | 3000 | Next.js 前端 |
| backend | 4000 | Rust API 服务 |
| postgres | 5432 | 数据库(不对外暴露) |
## 防火墙配置
```bash
# Ubuntu (ufw)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# CentOS (firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```
## 故障排查
```bash
# 查看 nginx 错误日志
sudo tail -f /var/log/nginx/error.log
# 查看 nginx 访问日志
sudo tail -f /var/log/nginx/access.log
# 检查服务状态
docker-compose ps
sudo systemctl status nginx
```

81
nginx/notify.conf Normal file
View File

@@ -0,0 +1,81 @@
# HTTP -> HTTPS 重定向
server {
listen 80;
server_name notify.example.com; # 替换为你的域名
# Let's Encrypt 证书验证
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS 主配置
server {
listen 443 ssl http2;
server_name notify.example.com; # 替换为你的域名
# SSL 证书配置Let's Encrypt
ssl_certificate /etc/letsencrypt/live/notify.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/notify.example.com/privkey.pem;
# SSL 安全配置
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;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
# 请求体大小限制(用于文件上传)
client_max_body_size 10M;
# API 请求代理到 backend (4000)
location /api/ {
proxy_pass http://127.0.0.1:4000/api/;
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;
# WebSocket 支持(如果需要)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# 上传文件代理到 backend
location /uploads/ {
proxy_pass http://127.0.0.1:4000/uploads/;
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_cache_valid 200 1d;
expires 1d;
}
# 其他请求代理到 frontend (3000)
location / {
proxy_pass http://127.0.0.1:3000;
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;
# Next.js HMR 支持(开发环境)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}