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

Ubuntu 22.04LTS Apache代理配置:解决Moodle路径跳转循环问题

解决反向代理下Moodle路径映射与NGINX无限循环问题

一、调整NGINX配置(避免无限循环)

编辑Ubuntu虚拟机上对应站点的NGINX配置文件(通常位于/etc/nginx/sites-available/custom-domain-name.com),替换为以下配置:

server {
    listen 80;
    server_name custom-domain-name.com;

    root /var/www/html; # 替换为你的网站根目录实际路径
    index index.php index.html index.htm;

    # 精确匹配根路径请求,内部转发至Moodle目录(无外部跳转,避免循环)
    location = / {
        rewrite ^ /directory-name/ last;
    }

    # 处理Moodle目录的请求路由
    location /directory-name/ {
        try_files $uri $uri/ /directory-name/index.php$is_args$args;

        # PHP解析配置,根据你的PHP版本调整sock路径
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }
    }

    # 根目录PHP请求处理(用于测试)
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

    # 静态资源缓存优化(可选)
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

关键说明:

  • 用rewrite ^ /directory-name/ last做内部重写,不会向浏览器发送跳转响应,彻底避免反向代理闭环引发的无限循环。
  • 不要使用return 301 /directory-name/,这类外部跳转会改变地址栏路径,同时触发反向代理的循环请求。

二、修改Moodle配置适配反向代理

打开Moodle目录下的config.php,更新以下参数:

$CFG->wwwroot = 'https://domain-name.com/directory-name'; // 对应WP Engine的外部访问地址
$CFG->dirroot = '/var/www/html/directory-name'; // 虚拟机上Moodle的实际目录路径
$CFG->reverseproxy = true; // 开启反向代理支持,确保Moodle识别真实请求信息

三、生效配置并验证

  1. 检查NGINX配置语法:
sudo nginx -t
  1. 重启NGINX使配置生效:
sudo systemctl restart nginx
  1. 修复Moodle目录权限(确保NGINX进程可访问):
sudo chown -R www-data:www-data /var/www/html/directory-name
sudo chmod -R 755 /var/www/html/directory-name

排查补充

如果仍出现循环,确认WP Engine的反向代理配置已正确传递Host头,保证虚拟机收到的请求Host为custom-domain-name.com。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 02:15:19