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

Nginx与Cloudflare配置引发过多重定向问题求助

解决Cloudflare代理下Nginx的Too Many Redirections错误

问题根源

  • Cloudflare开启橙色代理后,与后端Nginx的通信默认使用HTTP协议(即使用户访问的是HTTPS),但你的Nginx配置强制所有80端口请求重定向到HTTPS,导致CF收到重定向后再次用HTTP请求Nginx,形成循环。
  • Nginx配置存在重复的80端口重定向规则(nginx.conf和默认站点配置里都有针对mydomain.pl的80端口处理),规则冲突导致逻辑混乱。
  • 主站的SSL证书配置错误,指向了子域名的证书文件,可能引发证书验证问题间接导致重定向。
  • Portainer代理配置缺少必要的头信息,且代理目标用了HTTPS,和CF的代理逻辑冲突。

解决步骤

1. 让Nginx识别Cloudflare的真实请求协议

在Nginx的http块中添加配置,识别CF转发的真实协议和客户端IP:

http {
    # Cloudflare IP段,用于识别真实客户端IP
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 104.16.0.0/13;
    set_real_ip_from 104.24.0.0/14;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2a06:98c0::/29;
    set_real_ip_from 2c0f:f248::/32;
    real_ip_header CF-Connecting-IP;
    real_ip_recursive on;

    # 映射CF传递的真实协议
    map $http_x_forwarded_proto $real_scheme {
        default $scheme;
        https https;
    }

    # 后续server块放在这里
}

2. 清理重复的80端口重定向规则

删除nginx.conf中手动添加的第一个server块,保留Certbot生成的规则,避免冲突:

http {
    # 上面的Cloudflare配置放在这里

    server {
        if ($host = www.mydomain.pl) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        if ($host = mydomain.pl) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80;
        server_name mydomain.pl www.mydomain.pl;
        return 404; # managed by Certbot
    }
}

3. 修正主站的SSL证书配置

默认站点配置中,主站server块错误使用了子域名证书,替换为主域名的证书路径,同时修正域名拼写:

server {
    index index.html index.htm index.nginx-debian.html;
    server_name mydomain.pl; # 修正原配置的domain.pl为正确域名

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    # 替换为主域名的证书路径
    ssl_certificate /etc/letsencrypt/live/mydomain.pl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.pl/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

4. 调整Portainer代理配置

Cloudflare已处理HTTPS,后端Nginx到Portainer改用HTTP通信,同时补充必要代理头:

# Redirect sub.mydomain.pl to Portainer using nginx proxy 
server {
    server_name sub.mydomain.pl;
    location / {
        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 $real_scheme; # 使用真实协议
        proxy_pass http://localhost:9000; # Portainer默认HTTP端口为9000
        proxy_redirect off;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/sub.mydomain.pl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/sub.mydomain.pl/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

5. Cloudflare侧配置检查

  • 进入Cloudflare控制台,将「SSL/TLS」模式设置为**「完全」或「严格」**(不要选「灵活」)。
  • 确认域名的DNS记录处于橙色云代理状态。

最后重启Nginx生效:

sudo systemctl restart nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 23:55:20