D: 🚀 Introduction
Supabase is an open-source Firebase alternative that provides a PostgreSQL database, authentication, and real-time capabilities. While Supabase offers a managed cloud solution, many enterprises prefer self-hosting for better control, compliance, and cost efficiency. One critical component in a self-hosted Supabase setup is the API Gateway, which acts as the entry point for all client requests.
In this guide, we’ll walk through:
✅ Setting up a custom domain for your Supabase API Gateway
✅ Configuring HTTPS for secure communication
✅ Optimizing security with rate limiting, CORS, and JWT validation
✅ Performance tuning for high availability
🔧 Step 1: Setting Up a Custom Domain
By default, Supabase self-hosted instances use a local or IP-based URL (e.g., http://localhost:3000
or http://192.168.1.100:3000
). For production, you need a custom domain (e.g., api.yourdomain.com
).
Example: Configuring Nginx as a Reverse Proxy
- Install Nginx (if not already installed):
sudo apt update && sudo apt install nginx -y
-
Configure Nginx (
/etc/nginx/sites-available/supabase-api
):server { listen 80; server_name api.yourdomain.com; location / { proxy_pass http://localhost:3000; # Supabase default port proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
- Enable the configuration:
sudo ln -s /etc/nginx/sites-available/supabase-api /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl restart nginx
📌 Pro Tip: Use Cloudflare or AWS Route 53 for DNS management to improve reliability.
🔒 Step 2: Enabling HTTPS (SSL/TLS)
A secure API gateway requires HTTPS. Let’s use Let’s Encrypt for free SSL certificates.
Using Certbot for SSL
- Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
- Obtain a certificate:
sudo certbot --nginx -d api.yourdomain.com
- Certbot auto-configures Nginx to use HTTPS. Test with:
curl https://api.yourdomain.com/rest/v1/
🔐 Security Note:
- Enable HTTP/2 in Nginx for better performance.
- Set up automatic certificate renewal:
sudo certbot renew --dry-run
🛡️ Step 3: Security Optimization
A self-hosted API gateway must be hardened against attacks.
1. Rate Limiting (Prevent DDoS)
Add to Nginx config:
limit_req_zone $binary_remote_addr zone=supabase_limit:10m rate=10r/s;
server {
...
location / {
limit_req zone=supabase_limit burst=20 nodelay;
proxy_pass http://localhost:3000;
}
}
2. JWT Validation (Block Unauthorized Requests)
Supabase uses JWT for auth. Validate tokens at the gateway:
location /auth/v1/ {
if ($http_authorization = "") {
return 401;
}
proxy_pass http://localhost:3000;
}
3. CORS Restrictions
Only allow trusted domains:
add_header 'Access-Control-Allow-Origin' 'https://yourfrontend.com';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
⚡ Step 4: Performance Tuning
To handle high traffic:
✅ Enable Gzip Compression (Nginx):
gzip on;
gzip_types application/json text/css;
✅ Database Connection Pooling:
Configure Supabase config.toml
:
[api]
max_connections = 200
✅ Load Balancing (For HA):
Use multiple Supabase instances behind Nginx:
upstream supabase_servers {
server 192.168.1.100:3000;
server 192.168.1.101:3000;
}
server {
location / {
proxy_pass http://supabase_servers;
}
}
🎯 Conclusion
A well-configured Supabase self-hosted API Gateway ensures:
✔ Security (HTTPS, rate limiting, JWT checks)
✔ Performance (Gzip, load balancing)
✔ Reliability (Custom domain, automated SSL)
By following this guide, you’ll have a production-ready Supabase setup! 🚀
🔗 Further Reading:
Got questions? Drop them below! 👇