logo of Akmatori
02.01.2025

Fixing the "No Name Servers Defined" Error in Nginx

head-image

The "no name servers defined" error in Nginx can cause frustration when managing your web server. This guide explains why the error occurs and how to fix it.

What Does the Error Mean?

This error indicates that Nginx cannot resolve a domain name or connect to an upstream server. It occurs when DNS resolution fails during configuration parsing. Without proper DNS resolution, Nginx won't know where to send requests.

Common Causes

Here are the main reasons for this error:

  • Missing or Misconfigured resolver Directive: Nginx relies on the resolver directive to define DNS servers. Without it, DNS resolution fails.
  • Issues with Upstream Servers: If the upstream server’s domain name is incorrect or unreachable, this error occurs.
  • Network or Firewall Restrictions: Blocked DNS traffic can prevent Nginx from resolving domains.
  • Invalid Configuration: Typographical errors or missing directives in the Nginx configuration can trigger this issue.

Step-by-Step Solutions

1. Add or Update the resolver Directive

The resolver directive specifies which DNS servers Nginx uses for domain resolution. Add it to your configuration:

http {  
    resolver 8.8.8.8 1.1.1.1 valid=300s;  
    resolver_timeout 5s;  
}  
  • Replace 8.8.8.8 and 1.1.1.1 with the IPs of your preferred DNS servers.
  • The valid=300s option caches DNS results for 5 minutes.
  • The resolver_timeout specifies how long Nginx waits for a DNS response.

Reload the configuration:

sudo nginx -s reload  

2. Check the Upstream Server Configuration

If you're using an upstream block, verify the domain name:

upstream backend {  
    server api.example.com;  
}  

server {  
    location / {  
        proxy_pass http://backend;  
    }  
}  

Ensure the domain api.example.com resolves correctly. Test it with the nslookup or dig command:

dig api.example.com  

If the domain does not resolve, correct the issue or use an IP address instead of a domain name.

3. Test DNS Resolution from the Server

Ensure your Nginx server can resolve domains. Run:

ping example.com  

If this fails, update your system's DNS configuration by editing /etc/resolv.conf:

nameserver 8.8.8.8  
nameserver 1.1.1.1  

Save the file and restart Nginx.

4. Debug Configuration

Test the Nginx configuration to ensure there are no syntax errors:

sudo nginx -t  

This command displays any configuration issues, helping you identify and resolve errors.

5. Check Network or Firewall Rules

Ensure that DNS traffic (port 53) is not blocked by firewalls. Allow DNS traffic using these commands (for ufw):

sudo ufw allow out 53  
sudo ufw reload  

6. Monitor Logs

Check the Nginx error logs for more details:

sudo tail -f /var/log/nginx/error.log  

Logs can provide insights into why DNS resolution fails.


Pro Tips for Managing Nginx

  • Use Akmatori to monitor and automate incident response for your servers. Akmatori reduces downtime, simplifies troubleshooting, and prevents on-call burnout.
  • Consider hosting your Nginx servers on Gcore. Gcore offers affordable, reliable servers worldwide with excellent uptime.

Conclusion

The "no name servers defined" error in Nginx is a DNS resolution issue. By configuring the resolver directive and ensuring proper upstream and network settings, you can resolve the error.

Simplify server management with Akmatori and host your servers on Gcore for global performance.

Got questions? Drop them in the comments!

Maximize your website or application's performance and reliability!