Update: If you have an Apache version >= 2.4.19, you can use
Require forward-dns yourhost.example.com
This does not do a reverse lookup. And of course, it then requires full host names. See Apache's docs on "Require forward-dns" for details.
For older Apache versions:
Normally, you cannot use Allow From with dynamic hosts (for which the reverse DNS generally points to your ISP). But I wanted that anyway for some pages which were not worth protecting with authentication. So this is my workaround.
My server is configured to first use the /etc/hosts file before DNS. This is done by adding order hosts,bind
to /etc/host.conf
:
# cat /etc/host.conf
order hosts,bind
multi on
I have a cron job updating the hosts file every 15 minutes with my current dynamic external IPs. In my case, I have my own subdomain on my own DNS, and want to update several hosts. The update script I call from cron is:
#!/bin/sh
## Update /etc/hosts with our dynamic hosts
# dig ends the domain name with a dot, so we need it
domain=dyn.example.com.
die() {
echo "$1" 1>&2
exit 1;
}
tmpfile=$(mktemp "/tmp/hosts.XXXXX")
cp -a /etc/hosts $tmpfile || die "Cannot copy /etc/hosts to $tmpfile"
perl -i -ne "print unless /\Q$domain\E\s/" $tmpfile || die "Cannot remove $domain from $tmpfile"
dig -t AXFR $domain | \
perl -lne '/^(\S+)\s+\d+\s+IN\s+A\s+(\S+)/ && print "$2 $1"' >> $tmpfile \
|| die "Cannot add $domain entries to $tmpfile"
mv $tmpfile /etc/hosts || die "Error cannot mv $tmpfile to /etc/hosts"
If you have a single host to update, your script can be much simpler than that.