🌐 How to Deploy on a Custom Subdomain (Step-by-Step)
This guide explains how to make your API accessible via a custom subdomain (e.g., api.yourwebsite.com) instead of the default provider URL (e.g., huggingface.co/spaces/...).
✅ Prerequisites
- A Domain Name: You must own a domain (e.g.,
yourwebsite.com) purchased from a registrar like GoDaddy, Namecheap, Hostinger, or managed via Cloudflare. - Access to DNS Settings: You must be able to add records (A, CNAME, TXT) in your domain's dashboard.
- A Deploy Service: Where your code currently lives (e.g., Hugging Face Spaces, Render, DigitalOcean, etc.).
🚀 Scenario A: Using Hugging Face Spaces (Your Current Setup)
Since you are currently deploying to Hugging Face Spaces (ubuntu593/alt-scraper-api), follow these steps to use a custom subdomain.
1. Configure the Space
- Go to your Space: https://huggingface.co/spaces/ubuntu593/alt-scraper-api.
- Click on the Settings tab.
- Scroll down to the "Custom Domain" section.
- Enter your desired subdomain:
api.yourwebsite.com. - Click "Add domain".
2. Update DNS Records
Hugging Face will display a "Target" or "Value" that you need to point your domain to. Usually, it requires a CNAME record.
- Log in to your Domain Registrar (GoDaddy, Namecheap, Cloudflare, etc.).
- Go to DNS Management or Name Server Settings.
- Add a new record:
- Type:
CNAME - Name (or Host):
api(Using justapicreatesapi.yourwebsite.com) - Value (or Target):
ubuntu593-alt-scraper-api.hf.space(or whatever specific target HF provides in the settings). - TTL:
Automaticor3600.
- Type:
3. Verify
- Wait for DNS propagation (can take 5 mins to 24 hours, usually fast with Cloudflare).
- Hugging Face will verify the connection. Once verified, the status in Settings will change to "Active" (green).
- You can now access your API at:
https://api.yourwebsite.com/api/seo-report.
💻 Scenario B: Using a Virtual Private Server (VPS)
If you decide to host this on a VPS (DigitalOcean, AWS, Linode) for full control, follow these steps.
1. Get the Server IP
Assume your server's Public IP is 192.0.2.123.
2. Update DNS Records
- Log in to your Domain Registrar.
- Add a new record:
- Type:
A - Name (or Host):
api - Value:
192.0.2.123 - TTL:
Automaticor3600.
- Type:
3. Configure the Server (Nginx Reverse Proxy)
Since your Docker container runs on port 7860 (or 5050), you don't want users typing the port. You use Nginx to forward port 80/443 to your app.
Install Nginx:
sudo apt update sudo apt install nginx -yCreate a Config File:
sudo nano /etc/nginx/sites-available/api.yourwebsite.comPaste this Configuration:
server { server_name api.yourwebsite.com; location / { proxy_pass http://127.0.0.1:7860; # Forward requests to your Docker app proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }Enable the Site:
sudo ln -s /etc/nginx/sites-available/api.yourwebsite.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx
4. Enable HTTPS (SSL)
Use Certbot to get a free SSL certificate so https:// works.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d api.yourwebsite.com
❓ Common Questions
Q: Does SSL (HTTPS) work automatically?
- Hugging Face: Yes, they handle SSL for you automatically.
- VPS: No, you must run Certbot (Step 4 above).
Q: How long does it take?
- DNS changes can take up to 48 hours globally, but usually update within 15 minutes.
Q: Can I use www.api.yourwebsite.com?
- Yes, but that is a sub-subdomain. Unless necessary, keep it simple with just
api.yourwebsite.com.