Config by IP address

I know that I can create different config files that are named with a domain name to provide different configs for supporting things like staging vs production or localhost vs production.

I have a setup where I’d like to apply certain config settings on servers that are accessed by IP address. When I name a config file via IP address, this doesn’t work, so what are my options here, as opposed to setting up subdomains for the servers and accessing them that way?

I use environment based config files frequently but never tried using one with an IP, so I don’t know if there’s a way to make config.192.168.0.0.php work, for example.

One simple solution I think will work is adding this condition on the main site/config/config.php:

if (r::ip() === '192.168.0.0') {
    require(__DIR__ . DS . 'server-config.php');
}

This is strange, because it should work with an IP address as well. I could only test on localhost, but my config file was read without a problem.

Also, have a look at the configure() method in /kirby/kirby.php, you can test this code in template to see if you get the desired results.i

Yeah, I tested with config.IP.Address.php and ended up with it setting the url as defined in the default config, as well as autogit not working I think because the branch was set wrong.

@texnixe This line in kirby/kirby.php should in theory enable what I’m trying to do, no?

'addr' => 'config.' . server::get('SERVER_ADDR') . '.php',

Oh…is this section saying that it will load the config.php and then the IP address specific config? So in other word, if something is set in the config.php and not specifically overwritten in the IP address config it will pull it from the config.php?

If that’s the case, that would explain why the url was set wrong, because I ended up putting that in my config.php instead of just config.www.joyent.com.php. But that wouldn’t explain why autogit broke, unless that’s completely unrelated.

So…turns out the reason URLs weren’t working as expected was that I had the declaration in config.php and didn’t override it in the IP specific config.

I tested making a change in the IP config after fixing that issue, and it worked.

I’m not sure why autogit wasn’t working. Perhaps there was a transient permissions issue. It’s working now.

Yes, exactly. The config.php is always used. Then the host specific config file.

For what it’s worth: I have updated the documentation to make this clearer because it’s not the first time someone stumbled over it.

2 Likes