Netlify域名绑定Heroku后端遇Custom Domain 'Not Found'问题求助
Let's break down where things might be going wrong and walk through the correct steps for both your desired setups: api.mydomain.com (subdomain) or mydomain.com/api/ (path-based routing).
First: The Critical Misstep in Your Current Setup
You ran heroku domains:add www.mydomain.com to add the www subdomain to Heroku, but you're trying to use api.mydomain.com as your backend address. Heroku only routes traffic to domains you've explicitly added to your app—since api.mydomain.com isn't in your Heroku domain list, the DNS redirect is pointing to a domain Heroku doesn't recognize, hence the "Not Found" error.
Option 1: Use api.mydomain.com (Subdomain)
Follow these corrected steps:
Add the correct subdomain to Heroku
Run this command in Heroku CLI (replaceapi.mydomain.comwith your actual domain):heroku domains:add api.mydomain.comCopy the
*.herokudns.comaddress returned by this command—this is what you'll point your Netlify DNS to.Update Netlify DNS with the correct CNAME record
In your Netlify DNS dashboard:- Add a new CNAME record
- Name:
api(this createsapi.mydomain.com) - Value: The
*.herokudns.comaddress you copied from Heroku - Leave TTL as default (auto or 3600 is fine)
Wait for DNS propagation
DNS changes can take 5-30 minutes (sometimes longer) to take effect. You can verify it's working by runningnslookup api.mydomain.comin your terminal—if it returns the Heroku address, the DNS is set up correctly.Verify your Heroku backend is accessible
Test your backend using its default Heroku domain (e.g.,yourapp.herokuapp.com) to make sure it's running and returning valid responses. If this works,api.mydomain.comshould start working once DNS propagates.
Option 2: Use mydomain.com/api/ (Path-Based Routing)
If you prefer to keep everything under your main domain, use Netlify's redirect rules to forward /api/* traffic to your Heroku backend:
Create a Netlify redirect file
In your front-end project root directory, make a file named_redirects(note the leading underscore). Add this line (replaceyourapp.herokuapp.comwith your Heroku app's default domain):/api/* https://yourapp.herokuapp.com/:splat 200The
:splatwildcard ensures any path after/api/(like/api/users) gets passed through to your Heroku backend (as/users).Deploy the redirect file to Netlify
Push this file to your Git repo (or re-deploy your front-end project via Netlify) so the redirect rule takes effect.Test the route
Visitmydomain.com/api/(or a specific endpoint likemydomain.com/api/health)—Netlify should forward the request to your Heroku backend and return the correct response.
Quick Troubleshooting Checks
- Double-check Heroku domains: Run
heroku domainsin CLI to confirmapi.mydomain.com(if using subdomain) is listed. - Validate DNS records: Use
dig api.mydomain.comto ensure your CNAME points to the correct Heroku address. - Check backend status: Make sure your Heroku app is running (no crashed dynos) and responds to requests via its default domain.
内容的提问来源于stack exchange,提问作者OhSheHereWeGoAgain

