1

I'm setting up a server (Ubuntu Server 18.04) running Apache2 for my own personal testing and development needs. I can only access it via its local IP (ex. http://10.0.0.125) or its name on the local network (ex. http://jeeves), but that's fine for now.

I wanted to create several subdomains on this server to keep unrelated sites properly isolated, but I don't believe I can use actual subdomains: http://sub.10.0.0.125 doesn't work since an IP isn't a domain, hence can't have a subdomain, and http://sub.jeeves doesn't work since the browser interprets .jeeves as a TLD and not as the server's name.

So I came up with another solution:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /var/www/html
    AliasMatch "^/~(.*)" "/var/www/$1"
</VirtualHost>

Now I can create a directory /var/www/sub and access it via http://jeeves/~sub/.

A very good compromise, I thought ... except, since it's not a subdomain, and doesn't work like one, a link like <a href="/foo/"> will refer to http://jeeves/foo/ and not as desired to http://jeeves/~sub/foo/. I could deal with that in each site's code, but that would be far from ideal.

My question: Is there any way to make Apache treat the /~sub/ part of the URL like a subdomain for all intents and purposes (links in particular)? Some kind of BaseDir directive, maybe? I couldn't find one, but that doesn't mean something like it doesn't exist.

Before you suggest it: Getting an actual domain for this server is a bit beyond the scope of my current needs; running a dedicated DNS server seems like too much added complexity; and changing the hosts file on each system isn't exactly scaleable (also not trivial on an iPhone, say).

I feel like my solution would be ideal for my needs, if only it can work.

  • 1
    You will need to run a dedicated DNS server if you want to have support for features like global domains and subdomains. If its all local, make the changes in the hostfile. – LPChip Oct 2 '18 at 13:13
1

a link like <a href="/foo/"> will refer to http://jeeves/foo/ and not as desired to http://jeeves/~sub/foo/. […] Is there any way to make Apache treat the /~sub/ part of the URL like a subdomain for all intents and purposes (links in particular)? Some kind of BaseDir directive, maybe?

But it's not Apache that cares about the difference here. Relative URLs such as /foo/ are resolved by your web browser, and the web browser cannot be configured by Apache settings. As long as the provided URL is a path that starts with /, the web browser will always treat it as relative to the domain base.

running a dedicated DNS server seems like too much added complexity

You're probably relying on a dedicated DNS server right now in order to resolve the jeeves name – and it is probably running inside your home router, and the full name is probably jevees.lan. or jeeves.home. or something like that. (Client devices learn the suffix via DHCP.)

If you are lucky, the router will allow you to add custom static hostnames such as sub.lan., and if you are very lucky, it might even allow you to add third-level domains such as sub.jeeves.lan.

If the router's internal DNS server is not sufficiently flexible, or if the local name resolution isn't DNS-based at all, then you're mostly out of luck. Run your own DNS.


Some people have set up domain names hosted on 'magic' DNS servers, which translate <anything>.x.y.z.t.<somedomain> to the IP address x.y.z.t. (I don't know what specific domain names currently exist, but xip.io used to be a thing.)

One potential problem with this workaround – as well as with buying your own domain – is that most home routers will simply block DNS responses which come from outside but resolve to an 'internal' IP address. (This is called "DNS rebinding protection.")

  • 1
    The more I think about it myself, the more obvious it is that you're right. My hope was that maybe Apache could be set to prefix every request with the right "subdomain" somehow, maybe based on referer: if the referer starts with /~sub/ and the request doesn't, then Apache would redirect to /~sub/[requested url]. That was the vague line of thought I had, anyway. – S. T. Veje Oct 2 '18 at 14:20
  • You could possibly do that with RewriteRule, but I can see ways of that breaking due to fragile referer. Did I hear someone say "too much added complexity"? – grawity Oct 2 '18 at 14:38
  • Touché. But on the other hand, this is a personal development server, not a production server, so if a referer doesn't conform to expectations it's probably my own damn fault. – S. T. Veje Oct 2 '18 at 14:41
0

You will need to run a dedicated DNS server that can translate your requested domain name back to your testing/dev box IP address.

I've tried this before with mixed results by using a locally installed DNS solution on my dev box and it was a bit of a pain.

I purchased a business-grade router which has NAT reflection and the ability to act as a DNS server which allowed me to enter my dev domain names and point them to my dev box IP. i then configured Apache virtualhosts as one typically would do and I can use both TLD and sub.TLD domain names.

As grawity has said, run your own DNS. It's a lifesaver for a web developer.

  • Are there non-software-based DNS solutions? – grawity Oct 2 '18 at 13:19
  • I've edited to clarify (hopefully) what I mean. I mean I found a Windows-based software that provided a DNS server on my dev box. This turned out to be fairly rubbish and so I used a business-grade router that has the functionality and I simply add the domains I need pointing back to my dev box. Works considerably better. – Kinnectus Oct 2 '18 at 13:23
  • Guess you could call routers a 'hardware solution' then, but I'm somewhat surprised that it actually works well considering that most manufacturers treat it as a tertiary function at best. (After all, it really has nothing to do with routing.) Most domains are hosted on regular PC software, be it PowerDNS or BIND or Windows DNS Service... – grawity Oct 2 '18 at 13:34
  • I guess it depends how complicated one wants their DNS solution to be... a small LAN could use their router, a dedicated DNS box (like you say, a PC or, say, a RPi) or their business server if that's what they choose... the point is, I've personally found DNS managed outside the dev box far simpler than trying to get everything working properly on a dev box. With a dedicated box on my LAN I also have the luxury of being able to easily test my creations on other LAN devices like smartphones etc. with zero change in configuration on the devices themselves. – Kinnectus Oct 2 '18 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.