Nginx 反向代理与 HTTPS:certbot 签证书完整流程
反向代理让 nginx 站在应用前面;HTTPS 交给 certbot 自动签续。两件事实测串一遍。
sudo dnf install -y nginx certbot python3-certbot-nginx实测版本:nginx 1.30.5、certbot 5.8.0。python3-certbot-nginx 是让 certbot 能自动改 nginx 配置的插件,必装。
反向代理:把 8009 的请求转给本机 8081
Section titled “反向代理:把 8009 的请求转给本机 8081”先起一个假后端:
mkdir -p /srv/test && echo proxy-backend-ok | sudo tee /srv/test/index.htmlcd /srv/test && python3 -m http.server 8081 --bind 127.0.0.1/etc/nginx/conf.d/proxy-demo.conf:
server { listen 8009; server_name _;
location / { proxy_pass http://127.0.0.1:8081; 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; }}sudo nginx -t && sudo systemctl reload nginxcurl -s http://127.0.0.1:8009/实测返回 proxy-backend-ok——链路通。proxy_set_header 四件套别省:后端应用靠 X-Forwarded-* 还原真实访客地址与协议,缺了会把自己的日志和限流全部记成 127.0.0.1。
生产式反代模板(带缓存与超时)
Section titled “生产式反代模板(带缓存与超时)”真实业务通常还要这几行:
location / { proxy_pass http://127.0.0.1:8081; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; # WebSocket 握手 proxy_set_header Connection "upgrade"; proxy_read_timeout 60s; # 慢接口别 504 proxy_buffering off; # SSE/流式响应必须关}proxy_buffering off 对 LLM 流式输出、Server-Sent Events 是必需的,否则 nginx 会攒够缓冲才吐给客户端,表现为「一直转圈然后一次性出全文」。
HTTPS:certbot 签证书
Section titled “HTTPS:certbot 签证书”前提:域名 A 记录已指到本机、80 端口公网可达(Let’s Encrypt 的 HTTP-01 校验要从外网连进来)。
sudo certbot --nginx -d docs.example.com交互式问邮箱与是否重定向,答完自动:签发证书 → 改 nginx 配置加 443 server → 装 reload 钩子。证书落在 /etc/letsencrypt/live/域名/。
续期是自动的(systemd timer certbot-renew.timer),手动验证:
sudo certbot renew --dry-run实测可复现的验证链(无公网时)
Section titled “实测可复现的验证链(无公网时)”没有公网域名也能验证到「差一步」的程度:
sudo certbot certonly --nginx --dry-run -n -d test.invalid实测会走到 Let’s Encrypt 的域名校验阶段并报:
Invalid identifiers requested :: Cannot issue for "test.invalid":Domain name does not end with a valid public suffix (TLD)这条报错说明插件加载、账号注册、请求构造全部正常,只差一个真域名。公网环境把 test.invalid 换成真实域名即可签成。
| 现象 | 原因 |
|---|---|
certbot 报 No module named 'pyparsing' | 插件依赖缺失,dnf install python3-pyparsing |
--dry-run 被拒 | 只能配 certonly/renew,run 子命令不支持 |
| 反代后日志全是 127.0.0.1 | 没传 X-Forwarded-For,后端读的是直连地址 |
| 流式接口一直转圈 | proxy_buffering 没关 |
| WebSocket 连不上 | 缺 Upgrade/Connection 头 |
| 8009 起不来 bind Permission denied | SELinux 端口标签,见上文 |
签证书报 Domain name does not end with a valid public suffix | 域名不存在或没指到本机,LE 校验前置失败 |
- nginx + PHP 站点 — SELinux 端口标签的完整说明
- 防火墙 firewalld — 放行 80/443
- journalctl 日志 —
journalctl -u nginx -p err排障 - 官方:certbot 指南