142 lines
2.7 KiB
Markdown
142 lines
2.7 KiB
Markdown
# 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
|
|
```
|