FastAPI应用域名绑定配置指南

前提条件

  1. 已拥有一个域名(如:yourdomain.com)
  2. 域名已解析到服务器IP地址(175.27.253.177)
  3. 服务器已开放80/443端口
  4. FastAPI应用正在端口8000上运行

配置步骤

步骤1:安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 更新系统包
sudo apt update && sudo apt upgrade -y

# 安装Nginx
sudo apt install nginx -y

# 启动Nginx服务
sudo systemctl start nginx

# 设置Nginx开机自启
sudo systemctl enable nginx

# 检查Nginx状态
sudo systemctl status nginx

步骤2:创建Nginx反向代理配置

创建Nginx配置文件:

1
sudo nano /etc/nginx/sites-available/yourdomain.com

将以下内容粘贴到配置文件中(替换yourdomain.com为实际域名):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name api.ddda.cc;

location / {
proxy_pass http://127.0.0.1:8000;
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:启用Nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 创建符号链接启用配置sites-enabled/是启用的配置,sites-available/是可启用的配置
sudo ln -s /etc/nginx/sites-available/api.ddda.cc /etc/nginx/sites-enabled/

符号链接类似快捷方式,该命令会在/etc/nginx/sites-enabled/目录下新建一个api.ddda.cc的文件,该文件就是快捷方式,因为nginx启动会加载/etc/nginx/sites-enabled/目录下的配置,然后通过配置找到源文件,源文件有nginx的配置
1. 权限问题(核心风险)
Nginx进程权限:Nginx通常以www-data或nginx用户运行,默认无权访问用户主目录(如/home/ubuntu/)。若源文件权限为600(仅所有者可读),Nginx将无法读取配置,导致启动失败或配置不生效。
解决方案:
需确保/home/ubuntu/api.ddda.cc对Nginx进程用户可读(如通过chmod a+r赋予全局读权限),或调整目录权限(不推荐,可能降低安全性)。
2. 路径规范与可维护性
标准实践:Linux系统管理员通常遵循“配置文件存放在/etc/nginx/sites-available/,通过软链接到sites-enabled/启用”的规范。这种结构清晰分离“可用配置”和“启用配置”,便于批量管理(如禁用时删除链接即可,无需修改源文件)。
自定义路径风险:将配置文件放在用户主目录(如/home/ubuntu/)可能引发以下问题:
其他管理员难以快速定位配置文件;
系统备份/迁移时易遗漏用户目录下的配置;
用户误删文件导致配置丢失



#查看软链接的详细信息
ls -l /etc/nginx/sites-enabled/api.ddda.cc

# 测试Nginx配置
sudo nginx -t

# 重启Nginx服务
sudo systemctl restart nginx

步骤4:配置SSL证书(可选但推荐)

使用Let’s Encrypt申请免费SSL证书:

1
2
3
4
5
# 安装Certbot和Nginx插件
sudo apt install certbot python3-certbot-nginx -y

# 为域名申请SSL证书
sudo certbot --nginx -d api.ddda.cc

执行后1.输入邮箱 回车 2.同意协议 3.邮箱地址共享给基金会 4.选择2则是重定向域名的http到https

根据提示完成证书申请流程。Certbot会自动更新Nginx配置以启用HTTPS。

步骤4.1:SSL证书自动续期配置

Let’s Encrypt证书有效期为90天,Certbot默认会自动设置续期任务。以下是详细说明:

1. 检查自动续期是否已配置

1
2
3
4
5
# 检查systemd定时器(Ubuntu 20.04+推荐方式)
sudo systemctl status certbot.timer

# 或检查cron作业
ls -la /etc/cron.d/certbot

2. 手动设置自动续期(如果未自动配置)

方式1:使用systemd定时器

1
2
3
4
5
# 启用Certbot定时器
sudo systemctl enable --now certbot.timer

# 验证定时器状态
sudo systemctl status certbot.timer

方式2:使用cron作业

1
2
# 创建cron作业文件
sudo nano /etc/cron.d/certbot

添加以下内容(每天凌晨2点运行续期检查):

1
0 2 * * * root certbot -q renew

3. 测试自动续期功能

1
2
# 测试续期(--dry-run模拟续期过程)
sudo certbot renew --dry-run

如果输出显示”Congratulations, all renewals succeeded.”,则自动续期配置正确。

4. 查看续期日志

1
2
3
4
# 查看Certbot续期日志
sudo journalctl -u certbot.service
# 或
sudo tail -f /var/log/letsencrypt/letsencrypt.log

5. 续期后自动重载Nginx

Certbot默认会在续期成功后自动重载Nginx配置。可以通过查看Certbot配置确认:

1
cat /etc/letsencrypt/renewal/api.ddda.cc.conf | grep -i reload

输出为空自动续期但是不会,自动加载新的ssl,
如果没有自动重载配置,可以手动添加:

1
sudo nano /etc/letsencrypt/renewal/yourdomain.com.conf

在文件末尾添加:

1
2
3
4
5
6
7
8
[renewalparams]
post_hook = systemctl reload nginx
或deploy-hook = systemctl reload nginx
```bash
post_hook = systemctl reload nginx 是在续期成功后,通过systemctl reload nginx命令来重新加载Nginx配置。
deploy-hook = systemctl restart nginx 是在续期成功后,通过systemctl restart nginx命令来重启Nginx服务。
两种方式都可以实现自动加载新的SSL证书,但是reload是在不中断服务的情况下重新加载配置,而restart是先停止服务,然后再启动服务,所以restart会有短暂的服务中断。
根据实际情况选择合适的方式。

总结

通过以上步骤,您的FastAPI应用现在应该可以通过域名访问了。配置包括:

  1. Nginx作为反向代理服务器
  2. 域名解析和绑定
  3. SSL证书配置(HTTPS支持)
  4. 系统服务配置(自动重启)

这样可以确保应用的稳定运行和安全访问。