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

Netlify域名绑定Heroku后端遇Custom Domain 'Not Found'问题求助

Fixing "Not Found" When Binding Heroku Backend to Netlify Domain

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:

  1. Add the correct subdomain to Heroku
    Run this command in Heroku CLI (replace api.mydomain.com with your actual domain):

    heroku domains:add api.mydomain.com
    

    Copy the *.herokudns.com address returned by this command—this is what you'll point your Netlify DNS to.

  2. Update Netlify DNS with the correct CNAME record
    In your Netlify DNS dashboard:

    • Add a new CNAME record
    • Name: api (this creates api.mydomain.com)
    • Value: The *.herokudns.com address you copied from Heroku
    • Leave TTL as default (auto or 3600 is fine)
  3. Wait for DNS propagation
    DNS changes can take 5-30 minutes (sometimes longer) to take effect. You can verify it's working by running nslookup api.mydomain.com in your terminal—if it returns the Heroku address, the DNS is set up correctly.

  4. 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.com should 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:

  1. Create a Netlify redirect file
    In your front-end project root directory, make a file named _redirects (note the leading underscore). Add this line (replace yourapp.herokuapp.com with your Heroku app's default domain):

    /api/* https://yourapp.herokuapp.com/:splat 200
    

    The :splat wildcard ensures any path after /api/ (like /api/users) gets passed through to your Heroku backend (as /users).

  2. 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.

  3. Test the route
    Visit mydomain.com/api/ (or a specific endpoint like mydomain.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 domains in CLI to confirm api.mydomain.com (if using subdomain) is listed.
  • Validate DNS records: Use dig api.mydomain.com to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 18:42:41