0

I think this is becoming an eternal problem which may need a good solution, as Docker is being more and more popular.

My problem comes when trying to reverse Proxy to a Docker container which exposes a port (8080) with a bitnami/moodle image. I've tried for days all the possible solutions on the internet and nothing helps...

The better approaches are close to the solution, but I still get the page without static files or a message telling

'reverse proxy enabled so the server cannot be accessed directly. please contact the server administrator.'

I've tried both NGINX and Apache2, as I don't mind which one to use. Here are both configurations and also the config.php for the Moodle instance in the container.

Apache2

    <VirtualHost *:80>
          ServerName my.domain.com

          ProxyRequests Off
          ProxyPreserveHost On

          ProxyPass / http://127.0.0.1:8080/
          ProxyPassReverse / http://127.0.0.1:8080/ 
    </VirtualHost>

NGINX

  server {
          listen        443;
          server_name   my.domain.com;
          access_log    /var/log/nginx/domain.com_access.log;
          error_log     /var/log/nginx/domain.com_error.log;
          location / {
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              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;
              proxy_cache_bypass $http_upgrade;
              proxy_pass_request_headers   on;
              proxy_pass http://127.0.0.1:8080;
          }
}

config.php (Moodle)

<?php  // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype    = 'mariadb';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'mariadb';
$CFG->dbname    = 'moodle';
$CFG->dbuser    = 'root';
$CFG->dbpass    = 'moodle';
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
  'dbpersist' => 0,
  'dbport' => 3306,
  'dbsocket' => '',
  'dbcollation' => 'utf8mb4_general_ci',
);

if (empty($_SERVER['HTTP_HOST'])) {
  $_SERVER['HTTP_HOST'] = 'my.domain.com';
}
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  $CFG->wwwroot   = 'https://' . $_SERVER['HTTP_HOST'];
} else {
  $CFG->wwwroot   = 'http://' . $_SERVER['HTTP_HOST'];
}
$CFG->dataroot  = '/bitnami/moodledata';
$CFG->admin     = 'admin';

$CFG->reverseproxy = true;
$CFG->directorypermissions = 02775;

require_once(__DIR__ . '/lib/setup.php');

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

Any idea? I think i've followed all the hints out there right...

h4b1f
  • 1
  • 1
  • If you're running the proxy in a container, then you shouldn't pass the request on to 127.0.0.1, since that's the proxy container itself. – Hans Kilian Mar 05 '22 at 18:30
  • The proxy is not running on a container, should it? Also, as I said, I can access the page but it won't load the static or display an erorr. – h4b1f Mar 05 '22 at 18:35
  • I just assumed that it was, since you've used the 'docker' tag on your question. – Hans Kilian Mar 05 '22 at 18:46
  • Docker is used for Moodle. As I explain, I want to reverse proxy TO a Docker Container. – h4b1f Mar 05 '22 at 20:33

1 Answers1

0

You should use in Nginx config ip-address of docker container of container's name. All containers must to be in the one network. It is important!

For example:

NGINX

location / {
    proxy_pass "http://name_of_docker_container:8080";
    ...
}

The name of container you can know by the commands docker-compose ps or docker ps and the ip-address of container you can know by docker inspect container_name