multiple IP addresses for moodle site config file problem

multiple IP addresses for moodle site config file problem

by Jeanette Barron -
Number of replies: 23

I have installed moodle and it is working fine.  The server we are using for moodle has 2 network cards one for external use outside our network and one for internal use inside our network and there for  2 different IP addresses.

I am trying to edit the config.php file to allow both IP addresses to work, but I am not able to get both working only one or the other.

$CFG->wwwroot   = 'http://10.1.0.10:8080/moodle';  (internal)

or

$CFG->wwwroot   = 'http://76.216.128.203:8080/moodle';   (external)

Can you help me with a way to get both of these sites working at the same time.

is there a way to say and and include both IP's on the same line, or something in PHP.

Average of ratings: -
In reply to Jeanette Barron

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Not the way to do it.

* Get a hostname for the box if you don't have one
* Set your internal dns to point the hostname to the internal IP
* Set your external dns to point the hostname to the external IP
* Set wwwroot to the hostname in config.php

....really the only sensible way to do it.
Average of ratings: Useful (1)
In reply to Jeanette Barron

Re: multiple IP addresses for moodle site config file problem

by Adhi Nugraha -

A tricky programming is using PHP Server Variable

Its works for me when I have multiple NIC for internal and external access.

Change the $CFG->wwwroot value with :

$CFG->wwwroot = 'http://'.$_SERVER["HTTP_HOST"];

Average of ratings: Useful (1)
In reply to Adhi Nugraha

Re: multiple IP addresses for moodle site config file problem

by Leonid Auslender -

Perfect solution! and if you want to use domain name


$CFG->wwwroot   = 'http://' . $_SERVER['SERVER_NAME'];

works for http://www.domain.com and http://domain.com 


In reply to Leonid Auslender

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Digging up a 4 year old post from the grave??

AND... I still can't bring my self to recommend this 'dodge'. Do it properly with DNS so that Moodle really does have one, fixed URL. You know you want to wink

Average of ratings: Useful (1)
In reply to Howard Miller

Re: multiple IP addresses for moodle site config file problem

by Leonid Auslender -

you probably right, however - my problem in www.mydomain.com and same address but not with www


I can use htaccess to redirect, but it will be slower then just use $_SERVER and actually moodle able to do it, but showing error first

In reply to Leonid Auslender

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ok - but I just want to be clear to people stumbling over this post that it is absolutely not recommended. 

The issue comes from a mistaken belief that www.mydomain and mydomain.com are somehow the same thing. They are expressly not - they are two different hostnames. Pick one and advertise it to your users. There's rarely any real need to use both and an htaccess rewrite can pick up the 'mistakes'.

In reply to Howard Miller

Re: multiple IP addresses for moodle site config file problem

by Leonid Auslender -

I agree - they are different, however I face the problem when half users prefer to use www while others not


In reply to Howard Miller

Re: multiple IP addresses for moodle site config file problem

by Francesco Meago -

Real use:

In our case we would like to be able to reach the same moodle server from two different public IP addresses and hostnames in order to have a backup on the basic internet line of the school in order to save some money as an internet line might occasionally go down...

it seems to be impossible... sad

even posted solutions wont fully work, they give some access from second hostname, but it's slow and something is not showing up properly.

In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Again, Moodle can only be configured with ONE hostname. If you want to resolve to multiple IP addresses (in any way) then this is not a Moodle issue. It's a DNS one as that's what maps hostnames to IP addresses.

Everybody please go and read about DNS smile


In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Leonid Auslender -

http://support.microsoft.com/kb/168321

just to get the idea for DNS


More complicated way - to replicate your DB http://dev.mysql.com/doc/refman/5.6/en/replication.html

or if you have not a lot of users - have one MySQL dedicated server and connect both hosts to the same DB

There are always several ways to do something



In reply to Leonid Auslender

Re: multiple IP addresses for moodle site config file problem

by Francesco Meago -

Thanks Howard and Leonid  for your answers,

I would stick with the conclusion that Moodle is limited to only one hostname by design with all the potential drawbacks of it. I dont think you can always find workarounds through DNS as in my case: high availability through multiple A records isnt that well known to work afaik.

cheers, Francesco.

In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I don't really understand your concern. There's no reason in my view to have multiple hostnames for the same basic site. 

What you are talking about just sounds to me like load balancing but I might be missing the point. There are many of us who deal with this sort of high-availability stuff every day but we're going to need a detailed description of your requirements/solution to comment further.

In reply to Howard Miller

Re: multiple IP addresses for moodle site config file problem

by Francesco Meago -

No concern at all. Moodle is limited to only one hostname by design, ok.

I didnt talk about load balancing...  I wrote about a very basic form of backup. We have a server in a school behind a firewall with two cheap internet connections and two respective public IPs through which it could be useful to reach moodle from the outside (in case one line goes down), but by moodle's design we will not be able to do it. The only practical thing we could do I believe is to switch IP manually on the external DNS and wait up to 24 hours for DNS convergence, which is not a good solution. It's a pity but we'll live with it.

This "basic backup" example is a reason to have multiple hostnames for the same basic site. I just wanted to point this out for you. In fact I dont see a reason why to not allow multiple hostnames, but I believe there might be some technical issue behind it that I dont know about.

In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm still not very clear but it sounds like you have two lines into your site which you cannot rely on. Are you suggesting saying to your users, "if you can't get Moodle here, xxxx, try this instead, yyyy". I'm sure where to even start with that wink

One thing that isn't is a "simple backup"!  I don't know what the technology is behind your connections but lines don't have IP addresses. If nothing else, you could start by turning down your DNS TTL to 5 minutes which would at least reduce your down time. You could also use a "round robin" style arrangement. In an ideal world you would be able to park something like haproxy on the remote side of your connections. Properly set up, TCP/IP was specifically designed to work around problems like this. As I say, not remotely simple. 

In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Hi

> I wrote about a very basic form of backup. We have a server in a school behind a firewall with two cheap internet connections

Two unreliable Internet connections don't make one reliable Internet connection!

That said, if you are stuck with this setup, if you are lucky, you might be able to setup DDNS, so that the IP address behind the domain name will be quickly settle for the working Internet connection.
In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Ken Task -
Picture of Particularly helpful Moodlers

Howard has already hinted at this, but I'll be foolish enough to ask ... is your setup like this?

Circuit (you call it a line) A -> upstream provider who uses public IP blocks of 65.x.x.x.

Circuit B -> *different* upstream provider who uses public IP blocks of 205.x.x.x.

One router for both circuits? Or is there a router per circuit?   There are two interfaces on each router or NICS on the router if single router  that are configured for their respective upstream IP blocks.   Your firewall box sits between the routers and all other switches/ servers/workstations you have internally ... including the Moodle server which has a private IP address in it.   Does that firewall box have two NIC's in it? eth0 -> Circuit A router and eth1 -> Circuit B router?

So you are talking about a 'failover' system for your entire network, which, if not automated and setup ahead a time will take a few minutes, or longer, to establish connectivity.

If that's the case, you'll need to use a single FQDN for your Moodle ... or Joomla, or WP box internally.  Internal DNS stays the same ... no reason to change server IP addresses they are private IP's anyway, but you will need an *external* failover DNS solution.

Google for that ... failover DNS.

'spirit of sharing', Ken

In reply to Ken Task

Re: multiple IP addresses for moodle site config file problem

by Francesco Meago -

Hi all,

thanks for sharing your thoughts, foolish is good they say,

yes the setup is basically like Ken explained again (firewall in the middle with many NICs, two routers, two public IPs, two separate ISPs, one internal network with the moodle server), and the server is actually reachable at the same time from the two different ISPs (no need to wait any switching time). The whole thing works pretty well, trust me.

I know what DNS is and I dont like in this case DDNS or DNS failover solutions. No-ip.com was blocked for an entire week last year and also I would like to use the school's domain name. DNS failover would be ok but it's yet another third party service... Let me say it again, I just wanted to tell you that being able to have more that one hostname could be handy to me. Sending students an email saying to use hostname2 would be totally doable in case an ISP goes down for a day or a router burns on a bank holiday. No shame, no issue, and also totally in line with ISP's service level agreements, specially in the middle of the Alps where we live..

Let's please concentrate on moodle and not on my setup. Let's be explicit if this single hostname design limitation is a technical limitation, a security choice, or whatever else. I have many non-moodle webservers responding from multiple hostnames without a glitch and definitely I'm not the only one... I cannot see a problem with this. Can I have the same with moodle? Is there something I need to disable or that I can do within the configuration of the actual moodle server? I think this is what people looking at these threads would like to know or are trying to find workarounds on. If it is not possible... let's just be explicit, accept it as a design limitation, possibly give some references, mention the possible workarounds, accepting that somebody may have a use in having more than one hostname for a single site. Saying that it is useless or not the way to do, is simply not an answer. It may not be a moodle big issue, but it is a moodle limitation that not every webserver has.

kind regards, Francesco
In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Ken Task -
Picture of Particularly helpful Moodlers

Cannot speak officially for Moodle.org nor the folks that influence security decisions concerning the software restriction ...  you are indeed correct, it is a software restriction. 

You sound like you know what you are doing but sometimes ... other thoughts/ideas might spark a solution not yet contemplated or discussed.

While it's true that other packages run just fine, it means they use 'relative addressing' when linking to anything internal to the app and thus one can use 'transversal' links ../../../ (IF apache configured to allow - which most are by default and which I've had to do with an AllVideos Plugin to Joomla).   And therein lies the issue (possibly).   If the AV Plugin (example given) can be remote manipulated to transverse to other areas NOT under apache root then there is a security hole ... not in Joomla, but in the plugin.   Joomla, however, does have a configuration item like that of Moodle and if set, the relative addressing is no longer possible ... can't go ../../../passwords or other location outside of Apache document root.

All that to say, Moodle takes that stance as default and makes it difficult to circumvent - period.  While config.php hacks are the approach taken, there could be other files that also need hacking to make that work ... something one should be discouraged from doing ... cause Moodle will need to be upgraded - sooner or later.

In Moodle, anything created uses the wwwroot variable in the URL.   So let's say you do use the workaround in config.php.   Circuit A is down but you've a fall back/whatever configured to reach the same server on Circuit B ... the ONLY way all those internal links in Moodle can work is by using the SAME FQDN.

Ok, we know have a geographic picture of your situation now and can say that it sounds similar to geographic areas in West and South Texas.

Sorry, but I have to ask one more 'stupid question' ... this is all about accessing when students are away from school.   So how much 'territory' does you school cover?   And is that territory covered by one ISP for home connections?   Is it typically that the circuit to the school (where Moodle server is located) goes down, but the same IP's upstsream and thus connectivity for home users/other customers is still up?

Maybe colocating your Moodle server one hop upstream from the school on the schools provider POP could be arranged.

'spirit of 'thinkering', Ken


In reply to Francesco Meago

Re: multiple IP addresses for moodle site config file problem

by Ken Task -
Picture of Particularly helpful Moodlers

Ok one other ... have I done this?   No, but then again, am not in your situation either! 

Qualifications for this idea ... used to run core services for 30+ school districts ... core DNS/EMail Spam/AV gateways/EMail services and boundary firewall ... entities would have web/moodle servers either internal to their network or hosted at the core for the WAN.

All of it on Linux ... which cost $0 for software and just my time/expertise in setting up/getting it to work and then maintaining.   Ok, now that we know how crazy I was ... and still am .... here's the idea ...

Circuit A -> Moodle Server A -> this server has moodle code and the DB and the data directory
config.php has http://site/ as per FQDN for the Circuit A provider DNS.  DB is localhost.  Data directory is /var/www/moodledata

Circuit B -> Moodle Server B -> this has a different private IP but same FQDN (the name ... not the IP).    The code on Moodle Server B comes from rsync from Moodle A Sesrver - same exact version and addons.

External DNS upstream has one record for your zone ... that of the Moodle server by it's allocated by that ISP IP addresses.  Moodle Server B uses the 10. address of the DB on Moodle A.  Moodle Server B also uses a mount point which maps to the moodledata directory on Server A.

Firewall ...
Circuit A comes in as say 205.x.x.x and it gets directed to 10.10.10.10 (private IP for Moodle Server A).

Circuit B comes in as say 65.x.x.x and it get's directed to 10.10.10.9 for Moodle Server B.

Internal DNS has two A records by private IP and point to the SAME FQDN .. one for Moodle Server A (10.10.10.10).  one for Moodle Server B (10.10.10.9).  Forwarders in internal DNS should point to resolvers that are NOT authoritative for your schools domain.   Windows machines should probably NOT be using Root hints.

Here's the 'trick' ....
Moodle A is the primary for DB and for moodledata.
So in config.php of Moodle A server: DB = localhost data directory = /var/www/moodledata.

Moodle B server has an rsync of the code directory and the only thing different is the config.php file.   In that config file, the DB server is the private IP address of Moodle Server A and the /var/www/moodledata is really a mount point for /var/www/moodledata on Server A.

In Server B the only differeces is the DB server ... host is the DB of Moodle A by it's private IP address.
And /var/www/moodledata is a symlink for /mnt/moodledata which is an NFS mount to moodledata on Server A.

Server A & B have the same FQDN - no need to change.

Server B will be slower due to remote DB and remote moodledata but there are somethings one an do on Moodle B to speed up moodledata access.

Moodle Server B doesn't have to be as powerful as Moodle Server A.   B is apache only but using DB on Server A and using filedir of moodledata via NFS on Server A.

That's confusing as all get out ... isn't it!

Wouldn't recommend to anyone that they do this, but in your case, it might be the ONLY way you can ... without having to change config's/DNS'/Firewalls/routers, etc..   We do what we must in "lower" education.

'spirit of thinkering', Ken

In reply to Ken Task

Re: multiple IP addresses for moodle site config file problem

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ken- I think you are probably wasting your time. The OP now sees his rather weird set up as somehow 'normal' and is determined that Moodle is the barrier. I don't wish to sound so combative, but that is the case. 

Moodle works by constantly rewriting destination URLs and needs to understand the base web root. This is sensible and provides a degree of security (it's very hard to hack a fixed web root). 

In a production environment, you shouldn't really be using IP addresses for your destinations anyway. There's not much anybody can say to convince me that this is anything other than a DNS/Routing issue. I appreciate it's a tricky one, but please don't blame Moodle because your configuration is overly complex wink