域名绑定与 Nginx 反向代理配置指南 #
环境信息 #
- 服务器:华为云 Flexus 应用服务器
- 系统:Ubuntu 22.04 Server 64bit
- 配置:2C4G
- 区域:亚太-新加坡
- 服务:FastGPT (运行在 3000 端口)
- 域名:chat.anarkh.site
- 服务器IP:xxx.xxx.xx.xx
一、域名解析配置 #
1. 腾讯云 DNS 解析设置 #
- 登录腾讯云控制台
- 进入 DNS解析 DNSPod
- 添加解析记录:
- 主机记录:chat (二级域名前缀)
- 记录类型:A
- 记录值:xxx.xxx.xx.xx
- TTL:默认 600
2. 验证域名解析 #
# 本地 PowerShell 执行
nslookup chat.anarkh.site
# 或
ping chat.anarkh.site
二、服务器环境配置 #
1. 连接服务器 #
ssh root@xxx.xxx.xx.xx
2. 安装 Nginx #
# 更新软件包
sudo apt update
# 安装 Nginx
sudo apt install nginx -y
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查状态
sudo systemctl status nginx
3. 配置防火墙 #
# 允许 HTTP 和 HTTPS
sudo ufw allow 'Nginx Full'
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# 检查状态
sudo ufw status
三、华为云安全组配置 #
添加入方向规则 #
- 登录华为云控制台
- 进入 弹性云服务器 ECS
- 找到服务器,点击 安全组 标签
- 添加以下规则:
| 协议端口 | 端口 | 源地址 | 描述 |
|---|---|---|---|
| TCP | 80 | 0.0.0.0/0 | HTTP |
| TCP | 443 | 0.0.0.0/0 | HTTPS |
四、Nginx 反向代理配置 #
1. 创建配置文件 #
sudo nano /etc/nginx/sites-available/fastgpt
2. 添加配置内容 #
server {
listen 80;
server_name chat.anarkh.site;
location = / {
return 301 http://chat.anarkh.site/chat/share?shareId=rJ9d60pfDa1fkxie5SFe3OIu;
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_bypass $http_upgrade;
}
}
3. Nano 编辑器操作 #
- 粘贴:右键或
Shift + Insert - 保存:
Ctrl + O,然后按Enter - 退出:
Ctrl + X
4. 启用配置 #
# 创建软链接
sudo ln -s /etc/nginx/sites-available/fastgpt /etc/nginx/sites-enabled/
# 删除默认配置(可选)
sudo rm /etc/nginx/sites-enabled/default
# 测试配置
sudo nginx -t
# 重载 Nginx
sudo systemctl reload nginx
五、SSL 证书配置 (HTTPS) #
1. 安装 Certbot #
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
2. 申请 SSL 证书 #
sudo certbot --nginx -d chat.anarkh.site
按提示操作:
- 输入邮箱地址(证书过期提醒)
- 同意服务条款:
Y - 是否分享邮箱给 EFF:
N(不影响功能) - 选择 HTTP 重定向到 HTTPS:
2(推荐)
3. 验证证书 #
# 查看证书信息
sudo certbot certificates
# 测试自动续期
sudo certbot renew --dry-run
4. 证书信息 #
- 提供商:Let’s Encrypt(免费)
- 有效期:90 天
- 自动续期:由 Certbot 自动处理
- 证书路径:
/etc/letsencrypt/live/chat.anarkh.site/fullchain.pem - 私钥路径:
/etc/letsencrypt/live/chat.anarkh.site/privkey.pem
六、常用运维命令 #
Nginx 相关 #
# 测试配置
sudo nginx -t
# 重载配置
sudo systemctl reload nginx
# 重启 Nginx
sudo systemctl restart nginx
# 查看状态
sudo systemctl status nginx
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
# 查看访问日志
sudo tail -f /var/log/nginx/access.log
端口检查 #
# 查看 80 端口
sudo netstat -tlnp | grep :80
# 查看 3000 端口(FastGPT)
sudo netstat -tlnp | grep :3000
# 或使用 ss 命令
sudo ss -tlnp | grep :80
证书管理 #
# 手动续期
sudo certbot renew
# 查看所有证书
sudo certbot certificates
# 删除证书
sudo certbot delete --cert-name chat.anarkh.site
七、故障排查 #
1. 无法访问检查清单 #
- 域名解析是否生效(
nslookup chat.anarkh.site) - 华为云安全组是否开放 80/443 端口
- Nginx 配置是否正确(
sudo nginx -t) - Nginx 服务是否运行(
sudo systemctl status nginx) - FastGPT 服务是否运行(
sudo netstat -tlnp | grep 3000) - 防火墙是否允许(
sudo ufw status)
2. 查看日志 #
# Nginx 错误日志
sudo tail -f /var/log/nginx/error.log
# Certbot 日志
sudo tail -f /var/log/letsencrypt/letsencrypt.log
3. 本地测试 #
# 测试 FastGPT 服务
curl http://127.0.0.1:3000/chat/share?shareId=rJ9d60pfDa1fkxie5SFe3OIu
# 测试 Nginx 代理
curl http://localhost
# 测试域名(本地电脑 PowerShell)
Test-NetConnection -ComputerName xxx.xxx.xx.xx -Port 80
Test-NetConnection -ComputerName xxx.xxx.xx.xx -Port 443
八、配置说明 #
反向代理工作原理 #
- 用户访问
https://chat.anarkh.site - Nginx 接收请求
- 根路径
/重定向到/chat/share?shareId=xxx - Nginx 将请求代理到本地 3000 端口(FastGPT)
- 返回响应给用户
优势 #
- 隐藏端口号,使用标准 80/443 端口
- 支持 HTTPS 加密传输
- 自定义域名,提升专业度
- 统一入口,方便管理