Monthly Archives: July 2022

NGINX to resolve domain names using /etc/hosts

NGINX will always use a resolver to resolve a domain name to its corresponding IP address. Sometimes, especially for testing, it would be great if we can ask NGINX to resolve a domain name based on some temporary entries added to the /etc/hosts file. To do that, we need the help of dnsmasq application.

First, let’s install it.

apt-get install dnsmasq
 

Edit the domain name to IP address mapping.

vi /etc/hosts
 

Restart dnsmasq.

service dnsmasq restart
 

Now, we have a resolver set up. It will first resolve the domain name using the /etc/hosts file. If not found, it will forward the request to the system-wide resolver stated in the /etc/resolv.conf.

Now, let’s configure the NGINX to make use of the dnsmasq.

vi /etc/nginx/nginx.conf
 

Add the following entry.

resolver 127.0.0.1;

Restart NGINX.

service nginx restart
 

Now, the NGINX will look up the entries in the /etc/hosts before using the system-wide resolver.