How I Saved a 24‑Hour Deployment Disaster: Fixing the ‘Unknown Host’ ConnectionError in NestJS on a Shared VPS Without SSH‑Keys or Docker
Imagine watching the clock tick down to a client launch, only to be hit with a cryptic “Unknown host” error from your NestJS app. No SSH keys, no Docker, a shared VPS that refuses to cooperate – and 24 hours left to go live. Sound familiar? That was my reality last week.
Why This Matters
Every developer on a tight deadline knows the cost of a deployment snag:
- Lost revenue – a delayed launch can mean thousands of dollars.
- Client trust erodes the moment you miss a promised time‑slot.
- Team morale takes a hit when the same error repeats.
Fixing the ConnectionError: getaddrinfo ENOTFOUND in a NestJS micro‑service isn’t just a line of code; it’s a safeguard for your business and reputation.
Step‑by‑Step Rescue Plan
-
Confirm the Error Source
Run the failing service locally. If it connects to
database.example.comwithout issue, the problem lives on the VPS.npm run start:dev // Output: Error: getaddrinfo ENOTFOUND database.example.com at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) -
Log into the VPS with Password Auth
Since SSH keys aren’t an option, use the password you received from your host. If you can’t, reset it via the provider’s control panel.
ssh root@123.45.67.89 # Enter password when prompted -
Check DNS Resolution Directly
Run
nslookupordigon the VPS. If it returns “**server can't find**”, the server’s DNS resolver is broken.# nslookup database.example.com ;; connection timed out; no servers could be reached -
Update the Resolver Configuration
Most shared VPSes use
/etc/resolv.conf. Edit it to point to reliable public DNS servers (Google or Cloudflare).# sudo nano /etc/resolv.conf nameserver 1.1.1.1 # Cloudflare nameserver 8.8.8.8 # Google # Save and exitWarning: Some VPS providers overwriteresolv.confon reboot. You’ll need to make the change persistent (see the “Bonus tips” section). -
Verify the Fix
Run
nslookupagain. You should now see an IP address.# nslookup database.example.com Server: 1.1.1.1 Address: 1.1.1.1#53 Non-authoritative answer: Name: database.example.com Address: 203.0.113.42 -
Restart Your NestJS App
If you’re using
pm2, simply reload. Otherwise, kill the process and start it again.# pm2 restart my-nest-app # or # kill $(cat tmp/pids/nest.pid) && npm run start:prod -
Confirm Connectivity from Within NestJS
Hit an endpoint that queries the database. A 200 response means you’re back in business.
curl -i https://api.example.com/users HTTP/2 200 ... [{"id":1,"name":"Alice"}]
Real‑World Use Case: Multi‑Tenant SaaS on a $5 Shared VPS
My client runs a SaaS platform that spins up a new NestJS micro‑service for each tenant. The DNS entries (e.g., tenant123.api.example.com) are created dynamically via Cloudflare API. When the host’s resolver goes down, every tenant’s service throws the same “Unknown host” error, effectively freezing the entire business.
By fixing resolv.conf once and adding a fallback DNS check in the app’s bootstrap, we turned a potential $10 k revenue loss into a quick 10‑minute fix.
Outcome: Deployment succeeded at 02:15 AM, the client launched on schedule, and the “unknown host” bug was eliminated for all future releases.
Bonus Tips & Best Practices
- Persist DNS changes. Add the resolver lines to
/etc/network/interfacesor use arc.localscript that rewritesresolv.confon boot. - Enable fallback in code. Wrap your DB client in a retry block that logs DNS failures and optionally falls back to a secondary host.
- Monitor DNS health. Use a simple cron job that pings a known domain and alerts you via Slack if resolution fails.
- Avoid SSH‑key dependence. Store a one‑time password in a secure vault (e.g., 1Password) and rotate it monthly. It’s cheaper than upgrading to a VPS that forces key‑based auth.
- Container‑free automation. If Docker isn’t an option, use
pm2 ecosystem.config.jsto manage multiple NestJS services with zero‑downtime reloads.
Monetize the Knowledge
If you found this rescue guide useful, consider the following:
- Subscribe to my newsletter for weekly “Deploy‑in‑5‑Minutes” scripts.
- Grab the Quick NestJS Deploy Kit – a ready‑to‑run bundle that auto‑configures DNS, health checks, and PM2 on any VPS.
- Hire me for a 30‑minute emergency audit. I’ll harden your server, set up alerts, and guarantee zero downtime for your next launch.
Deploy smarter, not harder. Fix the DNS, keep the code flowing, and turn disaster into a showcase for your reliability.
No comments:
Post a Comment