查看: 94|回复: 0

【教程7】Nginx + HTTPS 域名配置 — 用专业方式对外提供服务

[复制链接]

18

主题

0

回帖

58

积分

管理员

积分
58
发表于 2026-5-14 17:05:37 | 显示全部楼层 |阅读模式
教程6中,New API 跑在 3000 端口上,用 IP:3000 访问。这不够专业——本文教你用 Nginx 反向代理 + Let's Encrypt 免费 SSL 证书,实现通过域名用 HTTPS 访问你的中转站。

一、架构说明

最终效果:用户访问 https://api.yourdomain.com → Nginx(处理HTTPS)→ 转发到本机 http://127.0.0.1:3000(New API)

Nginx 是高性能的 Web 服务器和反向代理,负责:SSL 加密/解密、静态文件服务、请求转发、限流和安全防护。

二、安装 Nginx
  1. # Ubuntu/Debian
  2. sudo apt update && sudo apt install -y nginx
  3. # CentOS/Rocky Linux
  4. sudo yum install -y nginx
  5. # 启动并设置开机自启
  6. sudo systemctl start nginx
  7. sudo systemctl enable nginx
  8. # 验证(浏览器访问 http://你的服务器IP,应该看到 Nginx 欢迎页)
  9. curl -I http://localhost
复制代码

三、配置 Nginx 反向代理

创建站点配置文件:
  1. sudo nano /etc/nginx/sites-available/api.yourdomain.com
复制代码

写入以下配置:
  1. server {
  2.     listen 80;
  3.     server_name api.yourdomain.com;  # 改成你的域名
  4.     # 客户端请求大小限制(API调用可能较大)
  5.     client_max_body_size 50m;
  6.     client_body_timeout 300s;
  7.     location / {
  8.         proxy_pass http://127.0.0.1:3000;
  9.         proxy_http_version 1.1;
  10.         # 必要的代理头
  11.         proxy_set_header Host $host;
  12.         proxy_set_header X-Real-IP $remote_addr;
  13.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14.         proxy_set_header X-Forwarded-Proto $scheme;
  15.         # WebSocket 支持
  16.         proxy_set_header Upgrade $http_upgrade;
  17.         proxy_set_header Connection "upgrade";
  18.         # 超时时间(AI模型调用可能较慢)
  19.         proxy_read_timeout 300s;
  20.         proxy_connect_timeout 75s;
  21.         # 缓冲设置
  22.         proxy_buffering off;
  23.     }
  24. }
复制代码

启用站点:
  1. # 创建软链接
  2. sudo ln -s /etc/nginx/sites-available/api.yourdomain.com /etc/nginx/sites-enabled/
  3. # 测试配置文件语法
  4. sudo nginx -t
  5. # 重载 Nginx
  6. sudo systemctl reload nginx
复制代码

四、Let's Encrypt 免费 SSL 证书(Certbot)
  1. # 安装 Certbot
  2. sudo apt install -y certbot python3-certbot-nginx
  3. # 自动配置 SSL(Certbot 会自动修改 Nginx 配置)
  4. sudo certbot --nginx -d api.yourdomain.com
  5. # 按提示输入邮箱(用于证书到期提醒),同意协议
  6. # Certbot 会自动重定向 HTTP → HTTPS
  7. # 验证自动续期
  8. sudo certbot renew --dry-run
复制代码

成功后,你的 Nginx 配置会被 Certbot 自动更新,添加 HTTPS 监听和证书路径。

五、完整配置(HTTP 重定向 + HTTPS)

如果 Certbot 自动配置后你想手动优化,这里是完整的推荐配置:
  1. # HTTP → HTTPS 重定向
  2. server {
  3.     listen 80;
  4.     server_name api.yourdomain.com;
  5.     return 301 https://$host$request_uri;
  6. }
  7. # HTTPS 主配置
  8. server {
  9.     listen 443 ssl http2;
  10.     server_name api.yourdomain.com;
  11.     # SSL 证书路径(Certbot 自动生成)
  12.     ssl_certificate /etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
  13.     ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/privkey.pem;
  14.     # SSL 配置优化(Mozilla 推荐)
  15.     ssl_protocols TLSv1.2 TLSv1.3;
  16.     ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
  17.     ssl_prefer_server_ciphers off;
  18.     ssl_session_cache shared:SSL:10m;
  19.     ssl_session_timeout 1d;
  20.     client_max_body_size 50m;
  21.     client_body_timeout 300s;
  22.     location / {
  23.         proxy_pass http://127.0.0.1:3000;
  24.         proxy_http_version 1.1;
  25.         proxy_set_header Host $host;
  26.         proxy_set_header X-Real-IP $remote_addr;
  27.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28.         proxy_set_header X-Forwarded-Proto https;
  29.         proxy_set_header Upgrade $http_upgrade;
  30.         proxy_set_header Connection "upgrade";
  31.         proxy_read_timeout 300s;
  32.         proxy_buffering off;
  33.     }
  34. }
复制代码

六、Nginx 限流配置(防止滥用)

http 块或 server 块中添加:
  1. # 在 http 块中定义限流规则
  2. # 编辑 /etc/nginx/nginx.conf,在 http {} 中添加:
  3. limit_req_zone $binary_remote_addr zone=api_limit:10m rate=60r/m;
  4. limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
  5. # 在 server 块或 location / 中使用:
  6. limit_req zone=api_limit burst=20 nodelay;
  7. limit_conn conn_limit 10;
复制代码

七、配置 Cloudflare + Nginx

如果你用了 Cloudflare(教程2),需要注意获取真实客户端 IP:
  1. # 在 http {} 块中添加:
  2. # Cloudflare IP 范围(设置真实IP)
  3. set_real_ip_from 173.245.48.0/20;
  4. set_real_ip_from 103.21.244.0/22;
  5. set_real_ip_from 103.22.200.0/22;
  6. set_real_ip_from 103.31.4.0/22;
  7. set_real_ip_from 141.101.64.0/18;
  8. set_real_ip_from 108.162.192.0/18;
  9. set_real_ip_from 190.93.240.0/20;
  10. set_real_ip_from 188.114.96.0/20;
  11. set_real_ip_from 197.234.240.0/22;
  12. set_real_ip_from 198.41.128.0/17;
  13. set_real_ip_from 162.158.0.0/15;
  14. set_real_ip_from 172.64.0.0/13;
  15. set_real_ip_from 131.0.72.0/22;
  16. set_real_ip_from 2400:cb00::/32;
  17. set_real_ip_from 2606:4700::/32;
  18. set_real_ip_from 2803:f800::/32;
  19. set_real_ip_from 2405:b500::/32;
  20. set_real_ip_from 2405:8100::/32;
  21. set_real_ip_from 2a06:98c0::/29;
  22. set_real_ip_from 2c0f:f248::/32;
  23. real_ip_header CF-Connecting-IP;
复制代码

八、UFW 防火墙设置
  1. # 启用 UFW
  2. sudo ufw enable
  3. # 只开放必要端口
  4. sudo ufw allow 22/tcp   # SSH
  5. sudo ufw allow 80/tcp   # HTTP
  6. sudo ufw allow 443/tcp  # HTTPS
  7. # 不要开放 3000 端口(New API 只通过本机 Nginx 访问)
  8. # 不要开放 3306 端口(MySQL 只本机访问)
  9. sudo ufw status verbose
复制代码

九、验证配置



  • 浏览器访问 https://你的域名,应该看到 New API 页面
  • 检查地址栏左侧的锁图标,确认 SSL 证书有效
  • 测试 https://你的域名/api/status 看是否返回正常响应


十、常见问题

问题解决
502 Bad GatewayNew API 没启动:docker compose ps 检查容器状态
域名无法访问检查 DNS 解析、安全组/防火墙是否开放 80/443 端口
SSL 证书申请失败确保域名已正确解析到服务器IP;80端口对外可达
HTTPS 页面部分内容加载不了New API 配置中的站点地址要改成 https:// 开头


参考来源:Nginx 官方文档 (nginx.org);Let's Encrypt/Certbot 文档 (certbot.eff.org);Cloudflare IP 列表 (cloudflare.com/ips);Mozilla SSL Configuration Generator
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

扫一扫浏览手机版
Archiver|手机版|小黑屋|星海拾贝 - API中转站上下游信息论坛

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖
扫一扫浏览手机版
返回顶部