Moodle Behind Nginx Reverse Proxy

Moodle Behind Nginx Reverse Proxy

by Peter Dreyer -
Number of replies: 3

Moodle 3.11.1

Hi everyone,

I currently have two Docker containers.  One is an Nginx reverse proxy.  It is configured to send requests to its "/learn" directory to my Moodle container.  Currently, after the installation process completes, Moodle attempts to redirect my web browser to index.php without a domain included. Obviously, this isn't correct.  

I've found that the "/learn" portion of the wwwroot configuration variable causes Moodle to completely remove my website from the redirect request it sends to my browser.  So, instead of getting a location header with my .com included, my browser is trying to use index.php as a domain.  

I am unsure why this is occurring.  If I replace the wwwroot variable with just my websie and remove "/learn", it will keep the domain in the redirect's Location header.  Is there something I'm not understanding about this process?

Thank you!


Average of ratings: -
In reply to Peter Dreyer

Re: Moodle Behind Nginx Reverse Proxy

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

$CFG->wwwroot must be the URL the browser uses to access the page. It will be used by links in the page to load the page's CSS, JavaScript, etc.

If the Moodle web server is behind a reverse proxy then you probably need $CFG->reverseproxy = true in config.php. If the TLS endpoint is on the reverse proxy then you probably also need $CFG->sslproxy = true, i.e. if the reverse proxy is configured with the certificate+private key instead of the Moodle web server.

If it's still not working, make sure you have the latest weekly update – 3.11.1+ build: 20210723 – as this has a fix that may affect this: MDL-63770.

If it's still not working, please can you share more details of the configuration such as 1) $CFG->wwwroot, 2) the reverse proxy configuration entries, and 3) the web server configuration entries. We don't need to see details you don't want to share on the Internet – for example: replace the domain name with example.com – as long as we can understand the configuration.

Average of ratings: Useful (1)
In reply to Leon Stringer

Re: Moodle Behind Nginx Reverse Proxy

by Peter Dreyer -
Hi Leon!

Thanks so much for the help! I went ahead and double-checked all of my settings. I am not currently using TLS on either the reverse proxy nor the Moodle instance as it is running on my laptop.

I went ahead and tried git commit 80f0b1516508bed028eaa6c747c0b8b58eaa87f8. This does not seem to fix the problem.

--------------------------------------------------------------------
My Reverse Proxy's config is as follows:
--------------------------------------------------------------------
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
   worker_connections  1024;
}


http {
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile        on;

   keepalive_timeout  200;

   #include /etc/nginx/conf.d/*.conf;

  server {
    server_name mysite.com;
    # Main Moodle Instance
    location /learn {
      proxy_pass http://moodle/;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Host $host:$server_port;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_read_timeout 1000s;
      proxy_redirect default;
    }
 }
}


--------------------------------------------------------------------
Moodle runs in a separate Nginx container. Here is its nginx.conf
--------------------------------------------------------------------

daemon off;
error_log /dev/stdout info;

events {
 worker_connections 1000;
}

http {
 access_log /dev/stdout;
 error_log /dev/stdout;

 server {
   listen 80;
   root /usr/share/nginx/html;

   location / {
     try_files $uri $uri/index.php;
   }

   location ~ \.php$ {
     fastcgi_pass unix:/var/run/php-fpm.sock;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
     fastcgi_read_timeout 1000;
   }
 }
}



--------------------------------------------------------------------
My Moodle config.php is stock except for the following changes:
--------------------------------------------------------------------

$CFG->wwwroot = "http://mysite.com/learn";
$CFG->reverseproxy = true;



In reply to Peter Dreyer

Re: Moodle Behind Nginx Reverse Proxy

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

I'm not a Docker guru so that's not easy for me to set up, but I got the reverse proxy and web server working on a single node. This is hosting both 1) the reverse proxy listening on port 80, and 2) the back-end web server listening on port 8080.

The reverse proxy:

 server { 
    server_name moodle.example.com;
    location /learn {
      proxy_pass http://192.168.124.220:8080/;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Host $host:$server_port;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_read_timeout 1000s;
      proxy_redirect default;
    }
 }

The back-end web server:

 server { 
   listen 8080;  
   root /usr/share/nginx/moodle;
 
   location / {
     try_files $uri $uri/index.php;
   }

   location ~ \.(php|phar)(/.*)?$ {
       fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
       fastcgi_intercept_errors on;
       fastcgi_index  index.php;
       include        fastcgi_params;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       fastcgi_param  PATH_INFO $fastcgi_path_info;
       fastcgi_pass   php-fpm;
    }
 } 

config.php:

I could then access the site at http://moodle.example.com/learn.

I don't know if this helps move you forward at all. If not, I can look at doing this using containers if I get time.

Average of ratings: Useful (1)