Tuesday, October 8, 2024

How to Fix Moodle Error "Reverse proxy enabled so the server cannot be accessed directly"

The error message "Reverse proxy enabled so the server cannot be accessed directly. Please contact the server administrator." in Moodle is typically associated with a server configuration that utilizes a reverse proxy. This error arises when the Moodle server detects direct access to it without going through the configured reverse proxy.

Here are some potential causes and configuration points that need to be examined:

wwwroot Configuration in config.php:

The Moodle config.php file usually contains a variable $CFG->wwwroot that defines the base URL of the Moodle site. Ensure that the URL specified is the one configured through the reverse proxy and not the local server URL or IP address.

Example (Correct):

      $CFG->wwwroot = 'https://moodle.example.com';
    

If accessed directly through the IP or a URL that bypasses the reverse proxy, Moodle will reject the access.

Reverse Proxy Configuration on the Server:

Make sure that the reverse proxy, such as Nginx or Apache, is properly configured to forward HTTP requests to the Moodle server.

Example (Nginx):

      server {
    listen 80;
    server_name moodle.example.com;

    location / {
        proxy_pass http://localhost:8080;  # Moodle running on port 8080
        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 $scheme;
    }
}
    

This configuration ensures that Nginx forwards the necessary headers to the Moodle server.

Trusted Proxy Settings in Moodle:

Moodle requires additional configuration to accept connections coming through a reverse proxy. In config.php, ensure that your reverse proxy is marked as a trusted proxy:

      $CFG->reverseproxy = true;
$CFG->sslproxy = true; // If using SSL on the reverse proxy
    

If Moodle doesn't detect the reverse proxy as a trusted proxy, it will throw the error message mentioned earlier.

X-Forwarded-For Header:

The reverse proxy must send the X-Forwarded-For and X-Forwarded-Proto headers. This is necessary for Moodle to understand that the request is coming through a proxy and not consider direct access an error.

DNS and Firewall Check:

Verify that the DNS is correctly pointed to the reverse proxy and the server firewall isn't blocking access that should come through the reverse proxy.

Solutions

  1. Check the config.php file: Ensure the wwwroot value is correct and matches the reverse proxy URL.

  2. Review the reverse proxy configuration (Nginx or Apache): Make sure the proxy header settings are accurate.

  3. Add trusted proxy to Moodle configuration: Specifically, set $CFG->reverseproxy = true.

  4. Investigate server logs (Moodle or web server): Search for detailed error messages that might indicate other configuration issues.

Once the configurations are correct, Moodle should accept requests through the reverse proxy without throwing the error message.

0 comments:

Post a Comment