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

Nginx反向代理Moodle时出现Too Many Redirects错误的解决求助

Nginx反向代理Moodle时出现Too Many Redirects错误的解决求助

最近我在学习Nginx的代理缓存和负载均衡,结果在给我的Moodle配置反向代理时遇到了问题——只要启用代理,访问Moodle就会提示“too many redirects”,但服务器上其他类型的站点用这套代理配置却完全正常。

我先说说我的配置思路:没有修改Nginx默认配置文件,而是新建了一个cache.conf作为代理配置块,同时把原站点的配置文件mysite.conf改成监听8000端口,让代理指向这个端口。

我的代理配置文件(cache.conf)

upstream origin_server {
    server 127.0.0.1:8000;
}

server {
    server_name mysite.com www.mysite.com;

    location / {
        include proxy_params;
        proxy_pass http://origin_server;
        proxy_cache custom_cache;
        proxy_cache_valid any 10m;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_cache_key "$host$request_uri$cookie_user";
        proxy_cache_min_uses 3;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/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
}

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

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

    listen 80;
    server_name mysite.com www.mysite.com;
    return 404; # managed by Certbot
}

原站点配置文件(mysite.conf)

server {
    listen 8000;
    listen [::]:8000;

    root /var/www/mysite.com/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ [^/]\.php(/|$) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }
}

补充细节

  • 静态的.html文件用这套配置访问完全正常,只有Moodle会出现重定向死循环
  • 我已经确认Moodle的config.php里$CFG->wwwroot设置的是代理用的域名(https://mysite.com)
  • 代理和原站点在同一台服务器上,我只是先在本地做实验,之后打算扩展到多服务器负载均衡
    一个关键现象:如果把代理配置里的SSL部分去掉,改成监听80端口的纯HTTP代理,Moodle就能正常访问,所以我怀疑是SSL相关的配置导致的重定向循环

有没有大佬能帮我分析一下问题出在哪,怎么让Moodle配合带SSL的Nginx反向代理正常工作?

备注:内容来源于stack exchange,提问作者user1012130

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 10:15:30