You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx与Docker Compose配置HTTPS失效问题求助

解决Nginx + Docker Compose的HTTPS配置问题

你的HTTP服务运行正常,但HTTPS无法工作,我帮你梳理下配置里的核心问题,以及对应的修复方案:

核心问题定位

你的HTTPS版本nginx.conf存在一个关键疏漏:HTTPS的server块没有指定server_name,这会导致Nginx无法将针对domain.com的HTTPS请求路由到正确的配置块,这是HTTPS失效的主要原因。

除此之外,我们也需要确保Nginx容器能正常读取Certbot生成的证书文件,避免权限类问题。

修正后的nginx.conf配置

upstream web {
  server web_gunicorn:8000;
}
# HTTPS服务配置块
server {
  listen 443 ssl;
  # 必须指定server_name,让Nginx匹配对应域名的HTTPS请求
  server_name domain.com;

  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

  location / {
    resolver 127.0.0.11;
    proxy_pass http://web;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
  }
  location /static/ {
    alias /static/;
  }
  location /media/ {
    alias /media/;
  }
}
# HTTP自动跳转HTTPS配置块
server {
  listen 80;
  server_name domain.com;
  # 简化跳转逻辑,去掉不必要的if判断
  return 301 https://$host$request_uri;
}

额外排查步骤

  1. 验证证书文件可用性:
    进入Nginx容器,检查证书文件是否可读:

    docker-compose exec nginx ls -l /etc/letsencrypt/live/domain.com/
    docker-compose exec nginx cat /etc/letsencrypt/live/domain.com/fullchain.pem
    

    如果出现权限错误,可调整Certbot本地目录的权限(比如设置为755),确保容器内的Nginx进程能正常读取。

  2. 检查端口与防火墙:
    确认服务器的443端口已在防火墙/安全组中开放,可用curl -v https://domain.com或telnet domain.com 443测试连接是否通畅。

  3. 查看Nginx错误日志:
    启动容器后,实时查看错误日志定位潜在问题:

    docker-compose exec nginx tail -f /var/log/nginx/error.log
    

关于Certbot自动续期

你的Certbot配置逻辑没问题,可以手动测试一次续期命令,确认续期流程正常:

docker-compose run --rm certbot renew --dry-run

内容的提问来源于stack exchange,提问作者Alexis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:23:13