1.
概述与准备事项
(1)目标:在
美国VPS(独享IP)上用Nginx做反向代理,绑定域名并用Let's Encrypt签发SSL证书。
(2)前提:VPS已开通公网独享IP并可修改DNS A记录指向该IP。
(3)系统:本文示例基于 Ubuntu 22.04 LTS,Nginx 1.22,Certbot。
(4)端口:需要开放 TCP 80 和 443;若后端服务在本地端口如3000也需确保本地访问权限。
(5)备份:配置前备份 /etc/nginx/ 与 /etc/letsencrypt/,并确认有root或sudo权限。
2.
VPS规格示例与成本对比
(1)选择美国机房时考虑延迟、带宽与流量计费。
(2)以下为示例机型配置便于参考与成本估算:
| CPU | 内存 | 盘 | 流量 | 独享IP | 位置 | 价格/月 |
| 2 vCPU | 4 GB | 80 GB NVMe | 4 TB | 1 | 纽约(US) | $10 |
(3)真实采购中注意是否支持IPv4独享与反向DNS绑定。
(4)测试IP示例:203.0.113.45(示例地址,真实使用请替换为VPS提供商分配的IP)。
(5)备案与合规:美国机房一般无需中国大陆备案,但访问稳定性受国际链路影响。
3.
Nginx反向代理配置步骤(示例)
(1)安装:sudo apt update && sudo apt install -y nginx certbot python3-certbot-nginx。
(2)修改DNS:在域名控制面板新增A记录 example.com -> 203.0.113.45。
(3)示例 Nginx 虚拟主机配置(将下列内容写入 /etc/nginx/sites-available/example.com):
server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
(4)启用并测试:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ && sudo nginx -t && sudo systemctl reload nginx。
(5)注意:若后端是https,要在proxy_pass使用 https:// 后端并配置 proxy_ssl_* 选项。
4.
Let's Encrypt 申请与自动续期
(1)使用 Certbot 一键申请:sudo certbot --nginx -d example.com -d www.example.com --email admin@example.com --agree-tos --non-interactive。
(2)手动方式:certbot certonly --webroot -w /var/www/html -d example.com。
(3)成功后查看证书路径:/etc/letsencrypt/live/example.com/fullchain.pem 与 privkey.pem。
(4)自动续期:系统默认安装了 /etc/cron.d/certbot 或 systemd timer,可用 sudo certbot renew --dry-run 测试。
(5)在续期后自动重载 Nginx:在 /etc/letsencrypt/renewal-hooks/deploy/ 添加脚本,内容:systemctl reload nginx;并赋可执行权限。
5.
防火墙、DDoS 与 CDN 加固策略
(1)UFW 基本规则:sudo ufw allow OpenSSH && sudo ufw allow 'Nginx Full' && sudo ufw enable。
(2)Fail2ban:安装 fail2ban,启用 nginx-http-auth 和 ssh 监控规则,减少暴力登录尝试。
(3)Nginx 限流:示例配置在 http {} 中添加 limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s,然后在 server 中使用 limit_req zone=one burst=20 nodelay。
(4)使用 CDN(如 Cloudflare):将 DNS 指向 Cloudflare,开启其 DDoS 与 WAF 功能,前端屏蔽大量恶意流量再转发到独享IP。
(5)TCP 层防护:对大流量攻击建议使用云厂商的带宽清洗或第三方清洗服务,结合速率限制与黑名单策略。
6.
实战案例:完整配置与输出示例
(1)VPS:Ubuntu 22.04 | IP 203.0.113.45 | 后端服务 Node.js 127.0.0.1:3000。
(2)/etc/nginx/sites-available/example.com 示例片段:
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; } }
(3)certbot 输出示例:Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem。
(4)验证证书:openssl s_client -connect example.com:443 -servername example.com 可以看到 Certificate chain 和 Verify return code: 0。
(5)重启状态检查:systemctl status nginx 显示 active (running),netstat -tulnp 可见 0.0.0.0:80 与 0.0.0.0:443 被 nginx 占用。
7.
常见故障与排错建议
(1)证书无法签发:检查域名A记录是否已生效(dig +short example.com),确保80端口能被外网访问。
(2)Nginx 502/504:确认后端服务监听地址与端口是否正确(ss -lntp | grep 3000)。
(3】TLS 握手失败:查看 /var/log/letsencrypt/ 与 /var/log/nginx/error.log,确认 fullchain 与 privkey 权限。
(4)续期失败:执行 certbot renew --dry-run 查看错误,常见是 DNS 解析或 Webroot 权限问题。
(5)被动防护不足:结合 CDN 日志、nginx access.log 分析攻击模式并在防火墙或 CDN 侧进行规则调整。
来源:技术指南教你在美国vps独享ip上配置反向代理与SSL证书