You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现用户配置CNAME记录,使其域名指向专属状态页面?

Hey there! Let's break down how to build this CNAME-based status page routing system for your Authstatus product. I'll walk through each component based on your tech stack (Cloudflare DNS, Gandi domain hosting, Node.js/Express) step by step:

1. Core Architecture Overview

First, let's align on the end-to-end flow:

  • User enters their domain (e.g., status.example.com) in your Authstatus dashboard
  • They configure a CNAME record for that domain pointing to status.authbase.co
  • Cloudflare (your DNS provider) routes requests for that domain to your Express backend
  • Your backend checks which user owns the requested domain, then serves their custom status page
2. DNS Configuration Setup (Gandi + Cloudflare)

Since your domains are hosted on Gandi but you use Cloudflare for DNS, you'll need to guide users through two key steps:

Gandi Side: Transfer DNS Control to Cloudflare

  • Ask users to log into their Gandi domain dashboard, navigate to DNS Settings
  • Replace Gandi's default DNS servers with Cloudflare's nameservers (you can find these in your Cloudflare account under the domain's DNS tab)
  • Remind them DNS propagation can take 10-60 minutes, so verification might not be immediate

Cloudflare Side: Enable Proxy & SSL

For each user's domain added to your Cloudflare account:

  • Ensure the Proxy status (orange cloud) is enabled for their CNAME record pointing to status.authbase.co—this ensures requests go through Cloudflare to your backend
  • Enable Full SSL mode in Cloudflare's SSL/TLS settings to encrypt traffic between Cloudflare and your Express server
  • Add a page rule to enforce HTTPS: *.<user-domain>/* with the setting Always Use HTTPS
3. Express Backend: Domain-Based Routing Logic

Your core logic will revolve around checking the incoming request's Host header to map to the correct user's status page. Here's a simplified implementation:

const express = require('express');
const dns = require('dns').promises;
const { UserDomain } = require('./models'); // Your Sequelize/Mongoose model
const app = express();
app.set('view engine', 'ejs'); // Or your preferred template engine

// Middleware to parse form data for onboarding
app.use(express.urlencoded({ extended: true }));

// Onboarding: User submits their domain
app.get('/onboard', (req, res) => {
  res.render('onboard', { cnameTarget: 'status.authbase.co' });
});

// Verify domain ownership (check CNAME record)
app.post('/verify-domain', async (req, res) => {
  const { domain } = req.body;
  
  try {
    // Resolve CNAME records for the user's domain
    const cnameRecords = await dns.resolveCname(domain);
    const isVerified = cnameRecords.some(record => record.includes('status.authbase.co'));
    
    if (isVerified) {
      // Save verified domain to your database
      await UserDomain.create({ domain, statusPageId: 'user-specific-id' });
      return res.send(`Success! Your status page is now live at ${domain}`);
    }
    
    res.status(400).send('Error: CNAME record not pointing to status.authbase.co');
  } catch (err) {
    res.status(400).send(`Failed to verify domain: ${err.message}`);
  }
});

// Route all incoming requests to the correct status page
app.get('*', async (req, res) => {
  const requestedDomain = req.get('Host');
  
  // Look up the domain in your database
  const userDomain = await UserDomain.findOne({ where: { domain: requestedDomain } });
  
  if (!userDomain) {
    return res.status(404).render('404', { domain: requestedDomain });
  }
  
  // Render the user's custom status page (pull data from your DB)
  res.render('status-page', {
    status: userDomain.status,
    incidents: userDomain.incidents,
    domain: requestedDomain
  });
});

app.listen(3000, () => {
  console.log('Authstatus routing service running on port 3000');
});
4. Key Optimizations & Considerations
  • Database Indexing: Add an index on the domain field in your UserDomain table to speed up lookups as you scale
  • Cache Control: Use Cloudflare page rules to cache static assets (CSS, JS, images) from your status pages, but exclude dynamic content (like incident updates)
  • Subdomain Flexibility: Allow users to use any subdomain (e.g., status.example.com, updates.example.com)—your logic only cares about the full domain in the Host header
  • Error Handling: Add fallback pages for domains that aren't verified, or for when your backend is down (you can use Cloudflare's custom error pages for this)
5. Scaling Notes

As your user base grows, you might want to:

  • Use Cloudflare Workers to offload domain lookup logic to the edge, reducing load on your Express backend
  • Implement a caching layer (like Redis) for frequent domain lookups to speed up response times
  • Add rate limiting to your verification endpoint to prevent abuse

内容的提问来源于stack exchange,提问作者William

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:25:22