2

I have a domain and my landing page is displayed at example.com, but I also need this to be the domain for my DigitalOcean Droplet which hosts the Node.js backend for my React apps. However, I'm having some trouble using the domain with both services. DigitalOcean suggested setting up the nameserver so that the DNS settings can be managed through DigitalOcean, so that's what I did.

I basically transferred all my DNS records from my Google Domains account to the DigitalOcean DNS manager and for the most part everything looks fine. If I go to example.com I can see my Webflow site. But if I go to example.com/api I'm just getting a 404 page not found (which is the same one I designed in Webflow). In other words, it doesn't look like I'm connecting to my Droplet with this domain because if I go to dropletip:port I get Cannot GET /api (which is expected since there's no /api route).

Is it possible to use one domain for DigitalOcean and Webflow? Right now I have an A record that points to Webflow's IP address and my Droplet's IP address, but like I said, only Webflow's is being reached.

2 Answers 2

10

How can I use a different A record for a specific path?

You can't since the name to IP address resolution is an IP level of need, which is far before/below HTTP or said differently the path part is not taken into account.

You need to use either different names (use api.example.com instead of path example.com/api), or have on the name something resolving that is able to further handle the HTTP level per your needs at the path level, which is called a (reverse) proxy.

3

Patrick's answer is entirely correct, but might be slightly difficult to understand if you're unfamiliar with the TCP/IP stack and how it relates to HTTP(S). Another way of thinking about this is that A (and AAAA) records are set per-domain, not per-URL. The domain is the part of the URL after http:// or https://, but before the next slash (i.e. if the URL is https://www.example.com/some/page, then the domain is www.example.com). You can have different A records for https://www.example.com and https://api.example.com, but not for https://www.example.com/api since that has the same domain as anything else under www.example.com.

If you want to have different servers handle different subsets of the same domain, you have a few options:

  • Serve HTTP 301 from https://www.example.com/api with Location: https://api.example.com (and then set an appropriate A record or CNAME on api.example.com).
  • Configure something like Nginx to act as a reverse proxy, which directs traffic for https://www.example.com/api/* to one backend, and all other traffic to a different backend.
  • Some cloud providers will offer reverse proxies as a hosted service. This is similar to the Nginx approach, but may be easier and/or cheaper compared to doing it yourself. It may also be more expensive, depending on the plan and provider, so be sure to actually do the math rather than assuming it's a good deal.
  • Don't use https://www.example.com/api at all, and just call it https://api.example.com in the first place. This would be my recommendation for "greenfield" deployments (i.e. for brand-new services where you don't have backwards compatibility to worry about).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.