Score:-1

Wildcard sub-domain apache vhost doesn't load my website folder

gw flag

I'm having some issues getting my apache virtual host to load the correct folder on my server. I'm building a public status pages feature for my platform, and one feature is allowing users to specify a custom sub-domain of their own domain, that loads the status page on my platform.

I run everything through one server, my website, back-end and my public status pages, all of which use apache virtual hosts to load these folders.

For the purposes of this, I have two cloudflare accounts, we'll call them Client and Company (where I'm the company).

My current status pages exist on status.company.com, it runs through Cloudflare, it's an A record pointed to my server IP.

I'd like to dish out a different URL for customers though in case things change, so I've created a psp.company.com CNAME record (no proxy) that points to status.company.com, and have created the following virtual host:

<VirtualHost *:80>
  ServerName psp.company.com
  ServerAlias *.psp.company.com
  DocumentRoot /var/www/company-status-pages/dist

  ErrorLog /var/log/httpd/psp.company-error.log
  CustomLog /var/log/httpd/psp.company.com-access.log combined
</VirtualHost>

When I visit in my browser either status.company.com or psp.company.com I see the intended result, my status pages website, all be it non-HTTPs when I visit psp.company.com for some reason.

Next, the Client then has a website which is separate from my system, their DNS runs through their own cloduflare account, which, I've instructed to create a CNAME record of their choice, they created status.client.com which points to psp.company.com, no proxy.

When they load up status.client.com, they do make it to my server, but it doesn't load the status pages, it loads the apache default webserver page which is not the intended result given that I've created the * wildcard sub-domain part.

What am I missing, and how could I get SSL to work too? I'm using Apache 2.4

ws flag
I’m voting to close this question because there's too big a gap between your current abilities and your aspirations to address in an SO answer post.
dave_thompson_085 avatar
jp flag
Your wildcard *.psp.company.com is only for *.psp.company.com. The client is sending a request to status.client.com, and that doesn't match *.psp.company.com. If you want your vhost to handle status.client.com you must add status.client.com (or maybe *.client.com) as an alias. And for SSL you also need to have a certificate (&key) _for_ status.client.com; if there's only one or few client(s) you can probably manage that if the CA supports http-01-style verification, otherwise you'll need the client(s) to obtain the cert(s) for you.
Ryan H avatar
gw flag
Okay, but this doesn’t resolve my original post. How can I make my virtual host config accept the wildcard domain? I can’t manually create and remove virtual host configs every time someone makes a change. The spec is that users can have their own, I thought this is the idea of CNAME records
Paul avatar
cn flag
For SF, I recommend removing all the stuff that doesn't directly pertain to your issue. Many people don't really want to wade through a wall of text to distill out your specific issues.
Score:1
si flag

It looks to me like you misunderstood the meaning of CNAME (Canonical Name) DNS records.

You can think of a CNAME record as an alias to another existing DNS entry. Let's look into what happens when a customer enters the URL http://status.client.com into their browser:

  • The DNS name status.client.com is resolved by the DNS server which returns the CNAME record leading to psp.company.com.
  • The DNS name psp.company.com is resolved by the DNS server which returns an A record leading to the IP address of your web server.
  • The browser sends an HTTP request to your web server with the host of the HTTP request header set to status.client.com.
  • The web server does not have a ServerName nor ServerAlias that matches the host field of the request, which is still status.client.com.

I hope this explains why the request does not work like you expected. Basically this is just a verbose explanation on what @dave_thompson_085 wrote in his comment. He answered this question in his comment.

You can only configure your web server to react on requests where you know the domain name of the request. The only exception is the default catch all entry without ServerName and ServerAlias and you can only have one of these.

But you can react on a subfolder of a request like so:

<VirtualHost *:80>
    ServerName client.com
    ServerAlias *.client.com
    DocumentRoot /var/www/html

    <Location "/status">
        Options FollowSymLinks
        # Add more directives specific to the subfolder
    </Location>
</VirtualHost>

Maybe this is the better option if you don't want to adapt your configuration for a specific subdomain of your customers.

Ryan H avatar
gw flag
I guess this means I'd probably need a separate server just for status pages since I can't do a wildcard everything as I already have domains going to a folder. I thought they'd be a more streamlined way though than manually adding people's status page domains everytime they add them to their account. It would be a lot of work for me. Wonder how Uptime Robot does this.
Andreas Piening avatar
si flag
@RyanH I don't think you'll need a seperate server. In fact, in my last apache config example I showed you how you can react on the subfolder **/status** on all domains, you just have to put it in the default http config so that it is available for all *ServerName*s. You just need to make sure, that there's no *overlapping* between *Location* names and real subfolders. Maybe you can choose a name like *web-status* or something like that to avoid that.
Ryan H avatar
gw flag
Okay, but your example assumes "client.com". I do not know what domains clients will use. I am "example.com" and provide a public status pages feature, so I need to have a way of routing any domain that users point to my platform. In plain english, if I have a domain "status.example.com", what apache config do I need to make "status.client.com" or even "test.company.com" point to my "status.example.com"
Ryan H avatar
gw flag
I have thousands of customers, I don't have know what domains they'll point to my system
Andreas Piening avatar
si flag
The idea of using a subfolder like `/status` in the default vhost (the one that does not have a **ServerName** nor **ServerAlias**) is that it does work on every domain without a name based virtual host configured. You don't have to know the domain names of your customers.
Score:1
in flag

Adding on what @Andreas just explained in a very simple terms, you can configure your virtual host in a separate file (also known as the default Apache virtual host), just make sure to rename the file as 000-default.conf and add your DocumentRoot there. Whenever your webserver is unable to find a matching ServerName or ServerAlias, it will fall to this particular configuration as a default and will serve your status page

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.