Nginx proxy on moodle error - too many redirects

Nginx proxy on moodle error - too many redirects

by Jose Pablo Pedrosa Dantas -
Number of replies: 5

I'm learning about proxy cache and load balancing on Nginx lately and been having some problems trying to make a proxy work for a moodle installation that i have, it says "too many redirects" whenever i put the proxy on moodle, but it works just fine for other types of sites that i have on the server.

For what i could find, to create a proxy i needed to create a new block on nginx, put my domain information on it, then put the proxy configuration on it, like upstream before the server tag and proxy_pass on the location tag (i did not edit the default file on nginx, i created a new onw called cache.conf, the default is not changed):

#nano 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
}

And then on my main site i remove the domain information, leaving just a _ for the server name and the make it listen to port 8000 (like in the upstream tag on the proxy block file) so the proxy will point to the main site:

#nano 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;
    }

 }

With this configuration any .html file works just fine, it loads normally, however for moodle it did not work at all, it aways return the "too many redirects" error, even through on my config.php file on moodle the $CFG->wwwroot has the same domain as the proxy, also, the proxy and the main site are on the same server, i'm just trying to learn about proxy before buying a new server to make the proxy point to another server and start to load balancing between the two.

How can i make moodle work with proxy?

Edit: I forgot to mention that moodle works whenever i remove the SSL configuration from the proxy, if i put the configuration on cache.conf file like this:

#nano cache.conf

upstream origin_server {
    server 127.0.0.1:8000;
}

server {

    listen 80;
    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;
    }


}

Moodle works just fine, it could be the a SSL configuration problem.

Average of ratings: -
In reply to Jose Pablo Pedrosa Dantas

Re: Nginx proxy on moodle error - too many redirects

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

You probably need $CFG->reverseproxy = true or $CFG->sslproxy (or both) in your Moodle's config.php.

Average of ratings: Useful (1)
In reply to Jose Pablo Pedrosa Dantas

Re: Nginx proxy on moodle error - too many redirects

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Try this in your config.php (don't use the ssl ones)-
$CFG->reverseproxy=1;
Average of ratings: Useful (1)
In reply to Emma Richardson

Re: Nginx proxy on moodle error - too many redirects

by Jose Pablo Pedrosa Dantas -
I tried $CFG->reverseproxy=1; but it blocked my access, it showed a message:


Translation: Reverse proxy enabled. Direct access to the server is not possible. Please contact the administrator.

However, when i enabled the $CFG->sslproxy, it started working normally, it could be that moodle and the proxy are on the same server and i am supposed to use 
$CFG->reverseproxy=1 if moodle was in a different server from the proxy.

In reply to Jose Pablo Pedrosa Dantas

Re: [Solved] Nginx proxy on moodle error - too many redirects

by Jose Pablo Pedrosa Dantas -
Go to config.php file, verify if $CFG->wwwroot has https and then add $CFG->sslproxy = true; in the end of the file.


It only works if you have both moodle and proxy on the same server, if they are on different servers, you probably need to add $CFG->reverseproxy=1; as well